JSP(六)JSP状态管理

第六节 JSP状态管理

目录

第六节 JSP状态管理

-http协议的无状态性

-Cookie概述

-jsp中创建与使用Cookie

-Cookie在登录中的应用

-解决Cookie无法保存中文字符串的问题

注:导入项目错误提示

-session与cookie对比

注:解决eclipse中代码提示的问题


-http协议的无状态性

一、http协议的无状态性

1、无状态是指,当浏览器发送请求给服务器的时候,服务器会响应。但当同一个浏览器再次发送请求时,服务器不会知道是刚才那个浏览器。

2、简单说,服务器【不会保存用户状态】,不会记得客户端是否访问过,所以这就是无状态协议

-Cookie概述

1.jsp状态管理

 保存用户状态的两大机制

 1、Session

 2、Cookie

  什么是cookie?

  cookie:是web服务器保存在客户端的一系列文本信息。

典型应用一:判断注册用户是否已经登录网站。

典型应用二:保存用户浏览记录。

 cookie的作用:

   1、对特定对象的追踪。

   2、保存用户网页浏览记录与习惯。

   3、简化登录

安全风险:容易泄露用户信息

-jsp中创建与使用Cookie

1、创建Cookie对象:

Cookie cookie=new Cookie(String key,Object value);

2、写入Cookie:

response.addCookie(cookie);

3、读取Cookie:

Cookie[] cookies=request.getCookies();

常用方法:

1. setMaxAge( expiry ) 设置cookie的有效期,以秒为单位 getMaxAge() 获取cookie的有效时间,以秒为单位

2. setValue(String value) 在cookie创建后,对cookie进行赋值 getValue() 获取cookie的值

3. getName() 获取cookie的名称

4.setValue()和getValue()涉及的都是字符串,对应前面提到的 Cookie是web服务器保存在客户端的文本文件。

-Cookie在登录中的应用

dologin.jsp

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'dologin.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->



  </head>

 

  <body>

    <h1>登录成功</h1>

    <hr>

    <br>

    <br>

    <br>

    <%

       request.setCharacterEncoding("utf-8");

       //首先判断用户是否选择了记住登录状态

       String[] isUseCookies = request.getParameterValues("isUseCookie");

       if(isUseCookies!=null&&isUseCookies.length>0)

       {

          //把用户名和密码保存在Cookie对象里面

          String username = URLEncoder.encode(request.getParameter("username"),"utf-8");

          //使用URLEncoder解决无法在Cookie当中保存中文字符串问题

          String password = URLEncoder.encode(request.getParameter("password"),"utf-8");

          //使用URLEncoder解决无法在Cookie当中保存中文字符串问题



          

          Cookie usernameCookie = new Cookie("username",username);

          Cookie passwordCookie = new Cookie("password",password);

          usernameCookie.setMaxAge(864000);

          passwordCookie.setMaxAge(864000);//设置最大生存期限为10天

          response.addCookie(usernameCookie);

          response.addCookie(passwordCookie);

       }

       else

       {

          Cookie[] cookies = request.getCookies();

          if(cookies!=null&&cookies.length>0)

          {

             for(Cookie c:cookies)

             {

                if(c.getName().equals("username")||c.getName().equals("password"))

                {

                    c.setMaxAge(0); //设置Cookie失效

                    response.addCookie(c); //重新保存。

                }

             }

          }

       }

    %>

    <a href="users.jsp" target="_blank">查看用户信息</a>

   

  </body>

</html>

users.jsp

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'users.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->



  </head>

 

  <body>

    <h1>用户信息</h1>

    <hr>

    <%

      request.setCharacterEncoding("utf-8");

      String username="";

      String password = "";

      Cookie[] cookies = request.getCookies();

      if(cookies!=null&&cookies.length>0)

      {

           for(Cookie c:cookies)

           {

              if(c.getName().equals("username"))

              {

                   username = URLDecoder.decode(c.getValue(),"utf-8");

              }

              if(c.getName().equals("password"))

              {

                   password = URLDecoder.decode(c.getValue(),"utf-8");

              }

           }

      }

    %>

    <BR>

    <BR>

    <BR>

         用户名:<%=username %><br>

         密码:<%=password %><br>

  </body>

</html>

login.jsp

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

  <body>

    <h1>用户登录</h1>

    <hr>

    <%

      request.setCharacterEncoding("utf-8");

      String username="";

      String password = "";

      Cookie[] cookies = request.getCookies();

      if(cookies!=null&&cookies.length>0)

      {

           for(Cookie c:cookies)

           {

              if(c.getName().equals("username"))

              {

                   username =  URLDecoder.decode(c.getValue(),"utf-8");

              }

              if(c.getName().equals("password"))

              {

                   password =  URLDecoder.decode(c.getValue(),"utf-8");

              }

           }

      }

    %>

    <form name="loginForm" action="dologin.jsp" method="post">

       <table>

         <tr>

           <td>用户名:</td>

           <td><input type="text" name="username" value="<%=username %>"/></td>

         </tr>

         <tr>

           <td>密码:</td>

           <td><input type="password" name="password" value="<%=password %>" /></td>

         </tr>

         <tr>

           <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td>

         </tr>

         <tr>

           <td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td>

         </tr>

       </table>

    </form>

  </body>

</html>

 

-解决Cookie无法保存中文字符串的问题

1.java.net包下的URLEncoder类来进行编码

public static encode(String string, String enco);

2.URLDecoder类进行解码

URLDecoder.decode(String string, String enco);

3.如果在登录界面把username和password的值设为了null,那么在登陆框就会出现null

最好是设为""

4.查找Cookie时,是按照k值进行查找的

-----------------------------------

使用URLEncoder(在java.net包下)解决无法在Cookie当中保存中文字符串问题:

 

String username = URLEncoder.encode(request.getParameter("username"),"utf-8");//编码,防止中文乱码

String password = URLEncoder.encode(request.getParameter("password"),"utf-8");//编码,防止中文乱码

注:导入项目错误提示

我们在用Eclipse进行Java web开发时,可能会出现这样的错误:项目目录前出现错误提示或者

The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。我们该怎么解决这个问题呢?

-session与cookie对比

1、保存位置:session在服务器端内存,cookie在客户端文本

2、保存对象:session保存Object类(保存对象大小没有限制),cookie保存String类型(保存对象大小有限制)

3、生存权:session会话结束即销毁,cookie可以长期保存在客户端

4、重要性:session安全性更高,保存重要信息,cookie保存不重要的信息

注:解决eclipse中代码提示的问题

Abcdefghijklmnopqrstuvwxyz

修改Java

修改js

修改web

猜你喜欢

转载自blog.csdn.net/RayMa0305/article/details/81604246
jsp