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.mzph.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;我们需要对这些可执行文件进…

是什么使波西米亚狂想曲成为杰作-数据科学视角

平均“命中率”是什么样的 (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…

流行编程语言_编程语言的流行度排名

流行编程语言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…

使用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> …

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…

10 个深恶痛绝的 Java 异常。。

异常是 Java 程序中经常遇到的问题&#xff0c;我想每一个 Java 程序员都讨厌异常&#xff0c;一 个异常就是一个 BUG&#xff0c;就要花很多时间来定位异常问题。 什么是异常及异常的分类请看这篇文章&#xff1a;一张图搞清楚 Java 异常机制。今天&#xff0c;栈长来列一下 J…

如何实施成功的数据清理流程

干净的数据是发现和洞察力的基础。 如果数据很脏&#xff0c;您的团队为分析&#xff0c;培养和可视化数据而付出的巨大努力完全是在浪费时间。 当然&#xff0c;肮脏的数据并不是新的。 它早在计算机变得普及之前就困扰着决策。 现在&#xff0c;计算机技术已普及到日常生活中…

通才与专家_那么您准备聘请数据科学家了吗? 通才还是专家?

通才与专家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>…

数据科学家 数据工程师_数据科学家实际上赚了多少钱?

数据科学家 数据工程师目录 (Table of Contents) Introduction 介绍 Junior Data Scientist 初级数据科学家 Mid-Level Data Scientist 中级数据科学家 Senior Data Scientist 资深数据科学家 Additional Compensation 额外补偿 Summary 摘要 介绍 (Introduction) The lucrativ…

spotify歌曲下载_使用Spotify数据预测哪些“ Novidades da semana”歌曲会成为热门歌曲

spotify歌曲下载TL; DR (TL;DR) Spotify is my favorite digital music service and I’m very passionate about the potential to extract meaningful insights from data. Therefore, I decided to do this article to consolidate my knowledge of some classification mod…

(第三周)周报

此作业要求https://edu.cnblogs.com/campus/nenu/2018fall/homework/2143 1.本周PSP 总计&#xff1a;1422 min 2.本周进度条 (1)代码累积折线图 (2)博文字数累积折线图 4.PSP饼状图 转载于:https://www.cnblogs.com/gongylx/p/9761852.html

功能测试代码python_如何使您的Python代码更具功能性

功能测试代码pythonFunctional programming has been getting more and more popular in recent years. Not only is it perfectly suited for tasks like data analysis and machine learning. It’s also a powerful way to make code easier to test and maintain.近年来&am…

layou split 属性

layou split&#xff1a;true - 显示侧分栏 转载于:https://www.cnblogs.com/jasonlai2016/p/9764450.html

C#Word转Html的类

C#Word转Html的类/**//******************************************************************** created: 2007/11/02 created: 2:11:2007 23:13 filename: D:C#程序练习WordToChmWordToHtml.cs file path: D:C#程序练习WordToChm file bas…

分库分表的几种常见形式以及可能遇到的难题

前言 在谈论数据库架构和数据库优化的时候&#xff0c;我们经常会听到“分库分表”、“分片”、“Sharding”…这样的关键词。让人感到高兴的是&#xff0c;这些朋友所服务的公司业务量正在&#xff08;或者即将面临&#xff09;高速增长&#xff0c;技术方面也面临着一些挑战。…

线性回归和将线拟合到数据

Linear Regression is the Supervised Machine Learning Algorithm that predicts continuous value outputs. In Linear Regression we generally follow three steps to predict the output.线性回归是一种监督机器学习算法&#xff0c;可预测连续值输出。 在线性回归中&…

小米盒子4 拆解图解_我希望当我开始学习R时会得到的盒子图解指南

小米盒子4 拆解图解Customizing a graph to transform it into a beautiful figure in R isn’t alchemy. Nonetheless, it took me a lot of time (and frustration) to figure out how to make these plots informative and publication-quality. Rather than hoarding this …

蓝牙一段一段_不用担心,它在那里存在了一段时间

蓝牙一段一段You’re sitting in a classroom. You look around and see your friends writing something down. It seems they are taking the exam, and they know all the answers (even Johnny who, how to say it… wasn’t the brilliant one). You realize that your ex…