j2ee—框架(2):Servlet+JSP实现基本的登录功能(v2.0)

该部分将逻辑判断在UserBean中进行处理,而且不采用配置的方式去实现,为了区分开两种实现方法的不同,在这里将之前设置的内容只是备注掉,并不会删除,也方便之后将两种方式进行对比。

第一部分 LoginController

 1 //LoginController这部分使用的注解的方法
 2 //因为使用了注解的方法,所以在web.xml中login的acion需要设置为LoginController
 3 //不然找不到用谁来处理login.jsp
 4 /**
 5  * Servlet implementation class LoginController
 6  */
 7 @WebServlet("/LoginController")
 8 public class LoginController extends HttpServlet {
 9     private static final long serialVersionUID = 1L;
10        
11     /**
12      * @see HttpServlet#HttpServlet()
13      */
14     public LoginController() {
15         super();
16         // TODO Auto-generated constructor stub
17     }
18 
19     /**
20      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
21      */
22     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23         // TODO Auto-generated method stub
24         ServletContext servletContext = getServletContext();
25         //将请求转发到UserBean去进行处理
26         RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/UserBean");
27         requestDispatcher.forward(request, response);
28 //        String name = request.getParameter("name");
29 //        System.out.println(name);
30 //        String password = request.getParameter("password");
31         
32         //if("ustc".equals(name)&&"oreo".equals(password))
33 //        if(name.equals(password))
34 //        {
35 //            //System.out.println("123");
36 //            requestDispatcher = servletContext.getRequestDispatcher("/login_success.jsp");
37 //        }
38 //        else
39 //        {
40 //            requestDispatcher = servletContext.getRequestDispatcher("/login_fail.jsp");
41 //        }
42         //response.getWriter().append("Served at: ").append(request.getContextPath());
43     }
44 
45     /**
46      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
47      */
48     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         // TODO Auto-generated method stub
50         doGet(request, response);
51     }
52 
53 }
  • web.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3   <display-name>Controller</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12 
    13 </web-app>

    由于使用的是注解的方式,所以web.xml的内容不会产生实质性的影响,所以在该部分不需要进行相应的设置,只需要默认即可,但是默认的内容不会产生任何的影响。

  • login.jsp
     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="LoginController" method="post">
    11 <!-- <form action="action/a1" method="post"> -->
    12 
    13     <label>账号:</label><input type="text" name="name"></br>
    14     <label>密码:</label><input type="text" name="password"></br>
    15     <input type="submit" value="登录">
    16 </form>
    17 </body>
    18 </html>

    注意在该部分,指定的是LoginController对login.jsp进行处理,所以action的设置和之前是不一样的。

login_success.jsp和login_fail.jsp的内容保持不变

具体的执行效果

能让一个男孩子热血的,不仅有梦想,还有姑娘。
原文地址:https://www.cnblogs.com/Mr24/p/6211104.html