前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >j2ee—框架(1):Servlet+JSP实现基本的登录功能(v1.0)

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

作者头像
Mister24
发布2018-05-14 10:23:11
9350
发布2018-05-14 10:23:11
举报
文章被收录于专栏:java初学java初学

主要分为四个部分: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 在这里,先看<servlet-mapping>中的内容,如果url后半部分是/action/a1,则将该部分交给Controller的LoginController处理。
 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>
  • 第三部分 login.jsp 在这里,声明了action="action/a1",所以会被交给servlet处理,servlet才能用String类型的数据将name和password保存下来,然后用来作为判定条件,从而跳转到相应的页面。
 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>
  • 第四部分 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使用的配置的方法,相对于注解的方式,更加利于后期的管理,尤其是在大项目中会体现出更好的优势。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档