JAVA遇見HTML——JSP篇(JSP狀態管理)

?

?

?

?

?

?

?

?案例:Cookie在登錄中的應用

?

URL編碼與解碼的工具類解決中文亂碼的問題,這個工具類在java.net.*包里

編碼:URLEncoder.encode(String s,String enc)//s:對哪個字符串進行編碼,enc:用的字符集(例:utf-8)

解碼:URLDecoder.decode(String s,String enc)//s:對哪個字符串進行解碼,enc:用哪個字符集解碼(例:utf-8)

login.jsp

 1 <%@ page language="java" import="java.util.*,java.net.*" 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 '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="登錄"/><input type="reset" value="取消"/></td>
61          </tr>
62        </table>
63     </form>
64   </body>
65 </html>

dologin.jsp

 1 <%@ page language="java" import="java.util.*,java.net.*" 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 '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        {//如果用戶沒有勾選記住用戶名、密碼的復選框,則把已經保存的原來的cookie設置失效
51           Cookie[] cookies = request.getCookies();
52        //曾經保存過用戶名、密碼
53           if(cookies!=null&&cookies.length>0)
54           {
55              for(Cookie c:cookies)
56              {
57                 if(c.getName().equals("username")||c.getName().equals("password"))
58                 {
59                     c.setMaxAge(0); //設置Cookie失效
60                     response.addCookie(c); //重新保存。
61                 }
62              }
63           }
64        }
65     %>
66     <a href="users.jsp" target="_blank">查看用戶信息</a>
67     
68   </body>
69 </html>

user.jsp

 1 <%@ page language="java" import="java.util.*,java.net.*" 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 '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>

?

?

?

轉載于:https://www.cnblogs.com/songsongblue/p/9753548.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388295.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388295.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388295.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PE文件講解

我們大家都知道&#xff0c;在Windows 9x、NT、2000下&#xff0c;所有的可執行文件都是基于Microsoft設計的一種新的文件格式Portable Executable File Format&#xff08;可移植的執行體&#xff09;&#xff0c;即PE格式。有一些時候&#xff0c;我們需要對這些可執行文件進…

easyui 布局之window和panel一起使用時,拉動window寬高時panel不跟隨一起變化

項目開發中布局是每一個組件都由最外層的window和內部的至少一個panel組成&#xff0c;其他的細小組件再依次放到panel中。 問題&#xff1a;當拉動外部的window時我們希望內部的panel的寬高也跟著變化&#xff0c;但是并沒有&#xff0c;尤其拉動其高度是更為明顯&#xff0c;…

是什么使波西米亞狂想曲成為杰作-數據科學視角

平均“命中率”是什么樣的 (What an Average ‘Hit’ looks like) Before we break the song down, let us have a brief analysis of what the greatest hits of all time had in common. I have picked 1500 songs ( charting hits ) right from the ’50s to the’10s, spre…

PE文件感染和內存駐留

這次&#xff0c;作者將和大家一起討論病毒的感染技術。另外&#xff0c;從本文開始&#xff0c;我們將陸續接觸到一些病毒的高級編碼技術。例如&#xff0c;內存駐留、EPO&#xff08;入口點模糊&#xff09;技術、加密技術、多態和變形等。通過這些高級技巧&#xff0c;你將進…

Python函數積累

評估函數eval() 去掉參數最外側引號并執行余下語句的函數 fun:將讓任何輸入的字符串轉換為python語句&#xff08;如"12132" -> 12132&#xff09;轉載于:https://www.cnblogs.com/LYluck/p/10376531.html

流行編程語言_編程語言的流行度排名

流行編程語言There has never been a unanimous agreement on what the most popular programming languages are, and probably never will be. Yet we believe that there is merit in trying to come up with ways to rank the popularity of programming languages. It hel…

Attributes.Add用途與用法

Attributes.Add("javascript事件","javascript語句");如&#xff1a;this.TextBox1.Attributes.add("onblue", "window.Label1.style.backgroundColor#000000;");this.TextBox1.Attributes.Add("onblur","this.style.d…

使用UIWebView加載網頁

1、使用UIWebView加載網頁 運行XCode 4.3&#xff0c;新建一個Single View Application&#xff0c;命名為WebViewDemo。 2、加載WebView 在ViewController.h添加WebView成員變量和在ViewController.m添加實現 [cpp] view plaincopyprint?#import <UIKit/UIKit.h> …

Java 開源庫精選(持續更新)

僅記錄親自使用和考慮使用的Apache Commons Commons IO - Commons IO 是一個幫助開發IO功能的實用程序庫 Commons Configuration - Commons Configuration 提供了一個通用配置界面&#xff0c;使Java應用程序可以從各種來源讀取配置數據。查看更多可重用、穩定的 Commons 組件S…

corba的興衰_數據科學薪酬的興衰

corba的興衰意見 (Opinion) 目錄 (Table of Contents) Introduction 介紹 Salary and Growth 薪資與增長 Summary 摘要 介紹 (Introduction) In the past five years, data science salary cumulative growth has varied between 12% in the United States, according to Glass…

hibernate的多表查詢

1.交叉連接 select * from A ,B 2.內連接 可以省略inner join 隱式內連接&#xff1a; select * from A,B where A.id B.aid; 顯式內連接&#xff1a; select * from A inner join B on A.id B.aid; 迫切內連接&#xff1a; 需要加上fetch關鍵字 內連接查詢兩者共有的屬性…

C# 讀取PE

最后分析結果會放在 一個DATASET里 ResourceDirectory這個TABLE 增加了 GUID列 為了好實現數結構 using System; using System.IO; using System.Data; using System.Collections; namespace PETEST { /// <summary> /// PeInfo 的摘要說明。 /// zgkesina.com …

10 個深惡痛絕的 Java 異常。。

異常是 Java 程序中經常遇到的問題&#xff0c;我想每一個 Java 程序員都討厭異常&#xff0c;一 個異常就是一個 BUG&#xff0c;就要花很多時間來定位異常問題。 什么是異常及異常的分類請看這篇文章&#xff1a;一張圖搞清楚 Java 異常機制。今天&#xff0c;棧長來列一下 J…

POJ 2777 - Count Color(線段樹區間更新+狀態壓縮)

題目鏈接 https://cn.vjudge.net/problem/POJ-2777 【題意】 有一個長度為 LLL 的區間 [1,L][1,L][1,L] &#xff0c;有 TTT 種顏色可以涂&#xff0c;有 QQQ 次操作&#xff0c;操作分兩種C A B CC \ A \ B \ CC A B C 把區間 [A,B][A,B][A,B] 涂成第 CCC 種顏色P A BP \ A \ …

如何實施成功的數據清理流程

干凈的數據是發現和洞察力的基礎。 如果數據很臟&#xff0c;您的團隊為分析&#xff0c;培養和可視化數據而付出的巨大努力完全是在浪費時間。 當然&#xff0c;骯臟的數據并不是新的。 它早在計算機變得普及之前就困擾著決策。 現在&#xff0c;計算機技術已普及到日常生活中…

nginx前端代理tomcat取真實客戶端IP

nginx前端代理tomcat取真實客戶端IP2011年12月14日? nginx? 暫無評論? 被圍觀 3,000 次使用Nginx作為反向代理時&#xff0c;Tomcat的日志記錄的客戶端IP就不在是真實的客戶端IP&#xff0c;而是Nginx代理的IP。要解決這個問題可以在Nginx配置一個新的Header&#xff0c;用來…

kubeadm安裝kubernetes 1.13.2多master高可用集群

1. 簡介 Kubernetes v1.13版本發布后&#xff0c;kubeadm才正式進入GA&#xff0c;可以生產使用,用kubeadm部署kubernetes集群也是以后的發展趨勢。目前Kubernetes的對應鏡像倉庫&#xff0c;在國內阿里云也有了鏡像站點&#xff0c;使用kubeadm部署Kubernetes集群變得簡單并且…

通才與專家_那么您準備聘請數據科學家了嗎? 通才還是專家?

通才與專家Throughout my 10-year career, I have seen people often spend their time and energy in passionate debates about what data science can deliver, and what data scientists do or do not do. I submit that these are the wrong questions to focus on when y…

ubuntu opengl 安裝

安裝相應的庫&#xff1a; sudo apt-get install build-essential libgl1-mesa-dev sudo apt-get install freeglut3-dev sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev 實例&#xff1a; #include "GL/glut.h" void…

分享一病毒源代碼,破壞MBR,危險!!僅供學習參考,勿運行(vc++2010已編譯通過)

我在編譯的時候&#xff0c;殺毒軟件提示病毒并將其攔截&#xff0c;所以會導致編譯不成功。 1>D:\c工程\windows\windows\MBR病毒.cpp : fatal error C1083: 無法打開編譯器中間文件:“C:\Users\lenovo\AppData\Local\Temp\_CL_953b34fein”: Permission denied 1> 1>…