Javaweb 会话跟踪学习记录(3)—URL重写

Javaweb 会话跟踪学习记录(3)—URL重写

 URL 重写是指服务器程序对接收的 URL 请求重新写成网站可以处理的另一个 URL 的过程,在实际应用中,当不能确定客户端浏览器是否支持 Cookie 的情况下,使用 URL 重写技术可以对请求的 URL 地址追加会话标识,从而实现用户的会话跟踪功能 。
 1.URL重写实现方法

  • URL重写通过HttpServletResponse的encodeURL()方法和encodeRedirectURL()方法实现。
  • encodeURL()方法可以对任意请求的URL进行重写。
  • encodeRedirectURL ()方法主要对使用sendRedirect()方法的URL进行重写。URL重写方法根据请求信息中是否包含“Set-Cookie"请求头来决定是否进行URL重写,若包含该请求头,会将URL原样输出;若不包含,则会将会话标识重写到URL中。
  • 示例:encodeURL方法的使用
out.print("+response.encodeURL("EncodeURLServlet")+"'>连接请求">
response.sendRedirect(response.encodeRedirectURL("EncodeURLServlet"));

 2.演示
创建UrlRewritingServlet

package com;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UrlRewritingServlet
 */
@WebServlet("/UrlRewritingServlet")
public class UrlRewritingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UrlRewritingServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		// 为了确保响应头中有Set-Cookie属性
		request.getSession();

		String link1 = response.encodeURL("CommonServlet");
		String link2 = response.encodeURL("UseRedirectServlet");

		out.print("对普通servlet的请求");
		out.print("对含有重定向servlet的请求");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

创建UseRedirectServlet

package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UseRedirectServlet
 */
@WebServlet("/UseRedirectServlet")
public class UseRedirectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UseRedirectServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect(response.encodeRedirectUrl("CommonServlet"));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

创建CommonServlet

package com;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CommonServlet
 */
@WebServlet("/CommonServlet")
public class CommonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommonServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print(request.getSession().getId());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

你可能感兴趣的:(Javaweb 会话跟踪学习记录(3)—URL重写)