码迷,mamicode.com
首页 > 编程语言 > 详细

java

时间:2015-06-04 22:41:11      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

Login.jsp


[html] view plaincopy01.<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> 
02.<% 
03.String path = request.getContextPath(); 
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
05.%> 
06. 
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
08.<html> 
09.  <head> 
10.    <base href="<%=basePath%>"> 
11.     
12.    <title>My JSP ‘index.jsp‘ starting page</title> 
13.    <meta http-equiv="pragma" content="no-cache"> 
14.    <meta http-equiv="cache-control" content="no-cache"> 
15.    <meta http-equiv="expires" content="0">     
16.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
17.    <meta http-equiv="description" content="This is my page"> 
18.    <!--
19.    <link rel="stylesheet" type="text/css" href="styles.css">
20.    --> 
21.  </head> 
22.   
23.  <body> 
24.    <h1>用户登录</h1> 
25.    <hr> 
26.    <%  
27.      request.setCharacterEncoding("utf-8"); 
28.      String username=""; 
29.      String password = ""; 
30.      Cookie[] cookies = request.getCookies(); 
31.      if(cookies!=null&&cookies.length>0) 
32.      { 
33.           for(Cookie c:cookies) 
34.           { 
35.              if(c.getName().equals("username")) 
36.              { 
37.                   username =  URLDecoder.decode(c.getValue(),"utf-8"); 
38.              } 
39.              if(c.getName().equals("password")) 
40.              { 
41.                   password =  URLDecoder.decode(c.getValue(),"utf-8"); 
42.              } 
43.           } 
44.      } 
45.    %> 
46.    <form name="loginForm" action="dologin.jsp" method="post"> 
47.       <table> 
48.         <tr> 
49.           <td>用户名:</td> 
50.           <td><input type="text" name="username" value="<%=username %>"/></td> 
51.         </tr> 
52.         <tr> 
53.           <td>密码:</td> 
54.           <td><input type="password" name="password" value="<%=password %>" /></td> 
55.         </tr> 
56.         <tr> 
57.           <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td> 
58.         </tr> 
59.         <tr> 
60.           <td colspan="2" align="center"><input type="submit" value="登录"/> 
61.           <input type="reset" value="取消"/></td> 
62.         </tr> 
63.       </table> 
64.    </form> 
65.  </body> 
66.</html> 
doLogin.jsp

[html] view plaincopy01.<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> 
02.<% 
03.String path = request.getContextPath(); 
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
05.%> 
06. 
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
08.<html> 
09.  <head> 
10.    <base href="<%=basePath%>"> 
11.     
12.    <title>My JSP ‘dologin.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.    <!--
20.    <link rel="stylesheet" type="text/css" href="styles.css">
21.    --> 
22. 
23.  </head> 
24.   
25.  <body> 
26.    <h1>登录成功</h1> 
27.    <hr> 
28.    <br> 
29.    <br> 
30.    <br> 
31.    <%  
32.       request.setCharacterEncoding("utf-8"); 
33.       //首先判断用户是否选择了记住登录状态 
34.       String[] isUseCookies = request.getParameterValues("isUseCookie"); 
35.       if(isUseCookies!=null&&isUseCookies.length>0) 
36.       { 
37.          //把用户名和密码保存在Cookie对象里面 
38.          String username = URLEncoder.encode(request.getParameter("username"),"utf-8"); 
39.          //使用URLEncoder解决无法在Cookie当中保存中文字符串问题 
40.          String password = URLEncoder.encode(request.getParameter("password"),"utf-8"); 
41.           
42.          Cookie usernameCookie = new Cookie("username",username); 
43.          Cookie passwordCookie = new Cookie("password",password); 
44.          usernameCookie.setMaxAge(864000); 
45.          passwordCookie.setMaxAge(864000);//设置最大生存期限为10天 
46.          response.addCookie(usernameCookie); 
47.          response.addCookie(passwordCookie); 
48.       } 
49.       else 
50.       { 
51.          Cookie[] cookies = request.getCookies(); 
52.          if(cookies!=null&&cookies.length>0) 
53.          { 
54.             for(Cookie c:cookies) 
55.             { 
56.                if(c.getName().equals("username")||c.getName().equals("password")) 
57.                { 
58.                    c.setMaxAge(0); //设置Cookie失效 
59.                    response.addCookie(c); //重新保存。 
60.                } 
61.             } 
62.          } 
63.       } 
64.    %> 
65.    <a href="users.jsp" target="_blank">查看用户信息</a> 
66.     
67.  </body> 
68.</html> 
user.jsp


[html] view plaincopy01.<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> 
02.<% 
03.String path = request.getContextPath(); 
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
05.%> 
06. 
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
08.<html> 
09.  <head> 
10.    <base href="<%=basePath%>"> 
11.     
12.    <title>My JSP ‘users.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.    <!--
20.    <link rel="stylesheet" type="text/css" href="styles.css">
21.    --> 
22. 
23.  </head> 
24.   
25.  <body> 
26.    <h1>用户信息</h1> 
27.    <hr> 
28.    <%  
29.      request.setCharacterEncoding("utf-8"); 
30.      String username=""; 
31.      String password = ""; 
32.      Cookie[] cookies = request.getCookies(); 
33.      if(cookies!=null&&cookies.length>0) 
34.      { 
35.           for(Cookie c:cookies) 
36.           { 
37.              if(c.getName().equals("username")) 
38.              { 
39.                   username = URLDecoder.decode(c.getValue(),"utf-8"); 
40.              } 
41.              if(c.getName().equals("password")) 
42.              { 
43.                   password = URLDecoder.decode(c.getValue(),"utf-8"); 
44.              } 
45.           } 
46.      } 
47.    %> 
48.    <BR> 
49.    <BR> 
50.    <BR> 
51.         用户名:<%=username %><br> 
52.         密码:<%=password %><br> 
53.  </body> 
54.</html> 

本篇文章来源于 黑基网-中国最大的网络安全站点 原文链接:http://www.hackbase.com/tech/2014-12-29/70233.html

java

标签:

原文地址:http://www.cnblogs.com/ZhuRenWang/p/4553140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!