以下实现登录窗口 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>