zoukankan      html  css  js  c++  java
  • 用servlet实现用户登录案例

    以下实现登录窗口 Login.jsp

    <!--Login.jsp-->
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <html>
      <head>    <title>登录页面</title>  </head>
      
      <body bgcolor="cccfff">
    
       <form action="Check" method="post">
       <table>
      		<tr><td>用户</td><td><input type="text"name="user"></td></tr>
      		<tr><td>密码</td><td><input type="password"name="password"></td></tr>
      		<tr align="center">
      			<td colspan="2">
      				  <input type="submit"value="登 录">
      				   
      				<input type="reset"value="取 消 ">
      			</td>
      		</tr>
      	</table>
       </form>
      </body>
    </html>
    

      

    处理登录的Servlet

    package ch04;
    
    import java.io.IOException;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Check extends HttpServlet {
    
    	/**
    	 * Constructor of the object.
    	 */
    	public Check() {
    		super();
    	}
    
    	/**
    	 * Destruction of the servlet. <br>
    	 */
    	public void destroy() {
    		super.destroy(); // Just puts "destroy" string in log
    		// Put your code here
    	}
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	/*
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    		out.println("  <BODY>");
    		out.print("    This is ");
    		out.print(this.getClass());
    		out.println(", using the GET method");
    		out.println("  </BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    	*/
    
    	/**
    	 * The doPost method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to post.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		   String name=request.getParameter("user");//获取用户名
    		   String password=request.getParameter("password");//获取密码
    		   
    		 
    		   if(("client".equals(name))&&"123456".equals(password)){//设置用户名和密码
    			   //如果用户名和密码相对应,则跳转到学生体质管理页面
    			   RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
    					   rd.forward(request, response);
    			   
    		   }else{//账户名或密码不正确则跳转登录失败页面
    			   
    			   RequestDispatcher rd=request.getRequestDispatcher("Faile.jsp");
    			   rd.forward(request, response);
    		   }
    		 
    	}
    
    	/**
    	 * Initialization of the servlet. <br>
    	 *
    	 * @throws ServletException if an error occurs
    	 */
    	public void init() throws ServletException {
    		// Put your code here
    	}
    
    }
    

    登录成功页面success.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <html>
      <head>    <title>成功页面</title>  </head>
      
      <body>
       <%String Name=request.getParameter("user"); %>
       欢迎,<%=Name %>成功登陆!
      </body>
    </html>
    

      

    登录失败页面Faile.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <html>
      <head>    <title>失败页面</title>  </head>
      
      <body>
       登陆失败!
       <!--获取用户名  -->
        <%String Name=request.getParameter("user"); %>
        <!--重新跳转到登陆页面  -->
       <br><a href="Login.jsp">请重新登录,<%=Name %>同学!   
      </body>
    </html>
    

      

    配置文件,在web.xml中,添加Check的配置信息,

    (注意jsp页面调用Servlet的方法)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <servlet>
        
        <servlet-name>Check</servlet-name>
        <servlet-class>ch04.Check</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>Check</servlet-name>
        <url-pattern>/Check</url-pattern>
      </servlet-mapping>
    
    </web-app>
    

      

    时间最会骗人,但也能让你明白,这个世界上没有什么是不能失去的,留下的尽力珍惜,得不到的都不重要
  • 相关阅读:
    Kafka事务机制
    RabbitMQ事务机制
    RocketMQ事务消息
    No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
    java + eclipse 工作环境快速配置
    查找.bashrc文件并设置linux快捷命令
    headers参数传值类型
    无需会员将有道云笔记脑图转换xmind
    使用goland调试远程代码
    nginx配置文件使用环境变量
  • 原文地址:https://www.cnblogs.com/www-x/p/7978612.html
Copyright © 2011-2022 走看看