国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp的session完成登陸功能

2019-11-14 23:38:05
字體:
來源:轉載
供稿:網友
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="Words" content="keyword1,keyword2,keyword3">18     <meta http-equiv="descrCSS" 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 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海盐县| 勃利县| 乐亭县| 普定县| 泾源县| 澄城县| 都昌县| 东阿县| 丹凤县| 长治市| 徐州市| 阜新市| 庆元县| 大余县| 潜江市| 凤翔县| 恩平市| 赤峰市| 紫阳县| 文登市| 农安县| 定结县| 会昌县| 缙云县| 昌邑市| 松潘县| 汤原县| 绥棱县| 玉龙| 北京市| 夏邑县| 永仁县| 库尔勒市| 德保县| 东明县| 富顺县| 望都县| 江西省| 维西| 徐汇区| 蕲春县|