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


    主要分为四个部分:LoginController、web.xml、login.jsp和login_success.jsp(login_fail.jsp)。

    • 第一部分 LoginController
       1     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       2         // TODO Auto-generated method stub
       3         ServletContext servletContext = getServletContext();
       4         RequestDispatcher requestDispatcher = null;
       5         
       6         String name = request.getParameter("name");
       7         System.out.println(name);
       8         String password = request.getParameter("password");
       9         
      10         //if("ustc".equals(name)&&"oreo".equals(password))
      11         if(name.equals(password))
      12         {
      13             System.out.println("123");
      14             requestDispatcher = servletContext.getRequestDispatcher("/login_success.jsp");
      15         }
      16         else
      17         {
      18             requestDispatcher = servletContext.getRequestDispatcher("/login_fail.jsp");
      19         }
      20         
      21         
      22         requestDispatcher.forward(request, response);
      23         
      24         //response.getWriter().append("Served at: ").append(request.getContextPath());
      25     }
      26 
      27     /**
      28      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      29      */
      30     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      31         // TODO Auto-generated method stub
      32         doGet(request, response);
      33     }

    在这一部分,用LoginController实现了跳转不同页面的处理,如果账号和密码是正确的,则跳转到login_success.jsp的页面,否则跳转到login_fail.jsp的页面。

    • 第二部分 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   <servlet>
      14       <servlet-name>Controller</servlet-name>
      15       <servlet-class>hello.LoginController</servlet-class>
      16   </servlet>
      17   
      18   <servlet-mapping>
      19       <servlet-name>Controller</servlet-name>
      20       <url-pattern>/action/a1</url-pattern>
      21   </servlet-mapping>
      22 </web-app>

      在这里,先看<servlet-mapping>中的内容,如果url后半部分是/action/a1,则将该部分交给Controller的LoginController处理。

    • 第三部分 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="action/a1" method="post">
      11     <label>账号:</label><input type="text" name="name"></br>
      12     <label>密码:</label><input type="text" name="password"></br>
      13     <input type="submit" value="登录">
      14 </form>
      15 </body>
      16 </html>

      在这里,声明了action="action/a1",所以会被交给servlet处理,servlet才能用String类型的数据将name和password保存下来,然后用来作为判定条件,从而跳转到相应的页面。

    • 第四部分 login_success.jsp和login_fail.jsp
     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     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 Welcome!
    11 </body>
    12 </html>

    login_success.jsp代码

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     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 Failed!
    11 </body>
    12 </html>

    login_fail.jsp代码

    运行截图

     该部分将逻辑处理都放在了LoginController中完成,这种方式并不好,之后的版本会对这个问题进行改进,而且这里对web.xml使用的配置的方法,相对于注解的方式,更加利于后期的管理,尤其是在大项目中会体现出更好的优势。

    能让一个男孩子热血的,不仅有梦想,还有姑娘。
  • 相关阅读:
    http数据返回值
    刷新 返回
    微信平台上遇到的bug
    iscroll修改
    iscroll
    a标签的herf和click事件
    ios9+xcode7 适配笔记
    xsd、wsdl生成C#类的命令行工具使用方法
    xcode更新,想想也是醉了
    关于#define预处理指令的一个问题
  • 原文地址:https://www.cnblogs.com/Mr24/p/6209356.html
Copyright © 2020-2023  润新知