用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>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM