jsp的session完成登陆功能

9/6/2015来源:Java教程人气:4124

jspsession完成登陆功能 Posted on 2015-04-04 11:17 思思博士 阅读(...) 评论(...) 编辑 收藏

login.jsp:

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>用户登录</title>13     14     <meta http-equiv="PRagma" content="no-cache">15     <meta http-equiv="cache-control" content="no-cache">16     <meta http-equiv="expires" content="0">    17     <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3">18     <meta http-equiv="description" content="This is my page">19     <!--20     <link rel="stylesheet" type="text/CSS" href="styles.css">21     -->22 23   </head>24   25   <body>26     <h1>用户登录</h1>27     <form action="check" method="post">28         <table border="1" width="250px">29             <tr><td>用户名:</td><td><input type="text" name="userId" /></td></tr>30             <tr><td>密码:</td><td><input type="password" name="passwd" /></td></tr>31             <tr><td></td><td><input type="submit" value="提交" /></td></tr>32         </table>33     </form>34   </body>35 </html>

welcom.jsp

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>My JSP 'welcome.jsp' starting page</title>13     14     <meta http-equiv="pragma" content="no-cache">15     <meta http-equiv="cache-control" content="no-cache">16     <meta http-equiv="expires" content="0">    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18     <meta http-equiv="description" content="This is my page">    19   </head>20      <%21         String user=(String) session.getAttribute("user");22         if(user==null){23         %>24         <jsp:forward page="login.jsp" />25         <%} %>26   <body>27  热烈欢迎您:<%=user %>28   </body>29 </html>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7   <display-name></display-name>     8   <welcome-file-list> 9     <welcome-file>index.jsp</welcome-file>10   </welcome-file-list>11   <servlet>12       <servlet-name>CheckUser</servlet-name>13       <servlet-class>servlet.CheckUser</servlet-class>14   </servlet>15   <servlet-mapping>16       <servlet-name>CheckUser</servlet-name>17       <url-pattern>/check</url-pattern>18   </servlet-mapping>19   20 </web-app>

CheckUser.java

 1 package servlet; 2  3 import java.io.IOException; 4  5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import javax.servlet.http.HttpSession;11 12 public class CheckUser extends HttpServlet {13     @Override14     protected void doPost(HttpServletRequest request,15             HttpServletResponse response) throws ServletException, IOException {16             request.setCharacterEncoding("UTF-8");17             String userId=request.getParameter("userId");18             String passwd=request.getParameter("passwd");19             20             if(userId!=null&&passwd!=null&&userId.equals("gys")&&passwd.equals("gys")){21                 HttpSession session=request.getSession();22                 session.setAttribute("user", userId);23                 RequestDispatcher dispatcher=request.getRequestDispatcher("/welcome.jsp");24                 dispatcher.forward(request, response);25             }26             else{27                 RequestDispatcher dispatcher=request.getRequestDispatcher("/login.jsp");28                 dispatcher.forward(request, response);29             }30     }31 }