Servlet--jsp实现URL重写解决cookie禁用

Servlet--jsp实现URL重写解决cookie禁用
一、禁用cookie访问原理

1、服务器判断当前用户是否为登录状态,通过服务器的sesion和浏览器的cookie值进行对比,实现判断当前链接用户的状态。

2、当用户禁用了cookie,服务器将不能判断客户端的状态。所以需要使用URL重写的方法来实现会话跟踪。
3、URL重写是通过在url后面添加sessionID作为值包含在链接中。
4、把 session ID 加到一个连接可以使用一对方法来简化:response.encodeURL() 使 URL 包含 session ID,如果你需要使用重定向,可以使用 response.encodeRedirectURL () 来对 URL 进行编码。
5、encodeURL () 及 encodeRedirectedURL () 方法首先判断 cookies 是否被浏览器支持;如果支持,则URL链接后面不添加sessionID,session ID 将通过 cookies 来维持。

二、示例

1、jsp代码实现URL重写

<%@ page language="java" import="java.util.*" pageEncoding="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>
    浏览器的cookie禁用时,jsp实现URL重写表单<br>
    <hr/>
    <!-- 下面是登录按钮encodeURL方法重写url -->
    <form action="<%=response.encodeURL("/test08_1/LoginServlet")%>" method="post">
    	<input type="text" name="name"/><br/>
    	<input type="submit" value="登录"/>
    </form>
    <!-- 下面是GOto按钮encodeURL方法重写url -->
    <a href="<%=response.encodeURL("/test08_1/LoginServlet")%>">GoTo</a>
  </body>
</html>

2、页面显示效果



3、后台LoginServlet处理登录请求

package test08_1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(request.getSession().getAttribute("name")!=null){
			out.print("你好:"+request.getSession().getAttribute("name")+"用户登录成功");
		}else{
			out.print("亲还有登录哦");
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		System.out.println("SessionId=="+request.getSession().getId());
		String name = request.getParameter("name");
		//将用户的名称保存到session属性中
		request.getSession().setAttribute("name",request.getRemoteAddr()+name);	

		//登录成功后,保存名称为JSESSIONID的id的cookie,值为session的id
		Cookie c = new Cookie("JSESSIONID",request.getSession().getId());
			c.setMaxAge(60*100);
			c.setPath("/");
			response.addCookie(c);
		
		//调用doGet请求
		doGet(request,response);

	}

}

4、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>test08_1.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>

</web-app>

5、测试结果

 ①、打开浏览器发送请求   http://127.0.0.1:8080/test08_1/

 ②、在首页输入用户名点击登录,提示登录成功。并且url后面显示jsessionid

 ③、返回首页点击GoTo,显示刚才登录成功的用户账号。并且url后面显示jsessionid和登录请求的一致

 ④、如果没有采用url重写方法,点击GoTo的时候会显示没有登录。

 ⑤、encodeURL () 及 encodeRedirectedURL () URL重写方法会判断客户端浏览器是否开启cookie,如果开启则不采用url重写,且url连接后面页面有jsessionid,如果客户端浏览器禁用cookie,会自动重写url且连接后面有jsessionid

6、结果截图

①、没有禁用cookie



②、禁用cookie


③、设置google浏览器禁用cookie方法



  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值