URL重写是指服务器程序对接收的URL请求重新写成网站可以处理的另一个URL的过程。URL重写技术是实现动态网站会话跟踪的重要保障。在实际应用中,当不能确定客户端浏览器是否支持Cookie的情况下,使用URL重写技术可以对请求的URL地址追加会话标识,从而实现用户的会话跟踪功能。URL重写实现会话跟踪,要保证每个页面都必须使用Servlet或JSP动态页面。因为附加在URL上的SessionID是动态产生的,所以对于静态页面的跳转,URL重写机制无能为力。
可以通过HttpServletResponse 的encodeURL(方 法和encodeRedirectURL()方 法实现URL重写,其中,encodeRedirectURL(方法主要对使用sendRedirect()方法的URL进行重写。URL重写方法根据请求信息中是否包含Set-Cookie 请求头来决定是否进行URL重写。若包含了该请求头,将URL原样输出;若不包含,则将会话标识JSESSIONID重写到URL中。URL重写示例代码如下:
//对重定向的URL进行重写
String encodeURL = response. encodeRedirectURL(“toRedi rectServlet”);
response. sendRedirect (encodeURL);
然后禁用浏览器Cookie;
useSession.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>useSession.jsp</title>
</head>
<body>
<form action="<%=response.encodeURL("sendMyName")%>" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
UseSessionServlet.java代码:
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(name = "useSession", urlPatterns = { "/sendMyName" })
public class UseSessionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("user");
if (null == name || name.trim().length() == 0) {
response.sendRedirect(response.encodeRedirectURL("useSession.jsp"));
} else {
HttpSession session = request.getSession(true);
session.setAttribute("myName", name);
out.println("<htm><body>");
out.println("您请求的servlet对象是:" + getServletName());
out.println("<br>您的会话ID是:" + session.getId());
out.println("<br>请单击请求另一个servlet:");
out.println("<br><a href=" + response.encodeURL("showMyName") + ">请求另一个servlet</a>");
out.println("</body></htm>");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
ShowNameServlet代码:
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(name = "showMyName", urlPatterns = { "/showMyName" })
public class ShowNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
String name = (String)session.getAttribute("myName");
if (null == name || name.trim().length() == 0) {
response.sendRedirect(response.encodeRedirectURL("useSession.jsp"));
}else{
out.println("<htm><body>");
out.println("您请求的servlet对象是:" + getServletName());
out.println("<br>您的会话ID是:" + session.getId());
out.println("<br>您的会话中存储的用户名是:" + name);
out.println("<br><a href=" + response.encodeRedirectURL("useSession.jsp") + ">重新登录</a>");
out.println("</body></htm>");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
之后点击请求另一个servlet之后它就跳回第一个页面了,也不知道为啥?????
望有人能解答一下!!!谢谢!!
今天的文章URL重写技术_长url转短url分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/75947.html