Url rewriting encodeURL()示例?
有谁能告诉我一个简单的代码示例,说明如何使用Url rewriting encodeURL()示例?,url-rewriting,session-cookies,session-management,Url Rewriting,Session Cookies,Session Management,有谁能告诉我一个简单的代码示例,说明如何使用response.encodeURL()? 我所有的搜索(google和stackoverflow)只提供了encodeURL()和encodeRedirectURL()之间的区别。 我正在寻找一些简单的代码,展示如何实现将会话信息嵌入到链接中 谢谢, 杰夫检查这些链接: 一些背景资料: 代码示例: 考虑一下这个问题: public void doGet (HttpServletRequest req, HttpServletResponse res
response.encodeURL()
?
我所有的搜索(google和stackoverflow)只提供了encodeURL()
和encodeRedirectURL()之间的区别。
我正在寻找一些简单的代码,展示如何实现将会话信息嵌入到链接中
谢谢,
杰夫检查这些链接:
一些背景资料:
代码示例:
考虑一下这个问题:
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Get the session object. Create a new one if it doesn't exist.
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<head><title> " + "SessionServlet Output " +
"</title></head><body>");
out.println("<h1> SessionServlet Output </h1>");
// Set up a session hit counter. "sessionservlet.counter" is just the
// conventional way to create a key for the value to be stored in the
// session object "dictionary".
Integer ival =
(Integer) session.getAttribute("sessionservlet.counter");
if (ival == null) {
ival = new Integer(1);
}
else {
ival = new Integer(ival.intValue() + 1);
}
// Save the counter value.
session.setAttribute("sessionservlet.counter", ival);
// Report the counter value.
out.println(" You have hit this page <b>" +
ival + "</b> times.<p>");
// This statement provides a target that the user can click
// to activate URL rewriting. It is not done by default.
out.println("Click <a href=" +
res.encodeURL(HttpUtils.getRequestURL(req).toString()) +
">here</a>");
out.println(" to ensure that session tracking is working even " +
"if cookies aren't supported.<br>");
out.println("Note that by default URL rewriting is not enabled" +
" due to its large overhead.");
// Report data from request.
out.println("<h3>Request and Session Data</h3>");
out.println("Session ID in Request: " +
req.getRequestedSessionId());
out.println("<br>Session ID in Request is from a Cookie: " +
req.isRequestedSessionIdFromCookie());
out.println("<br>Session ID in Request is from the URL: " +
req.isRequestedSessionIdFromURL());
out.println("<br>Valid Session ID: " +
req.isRequestedSessionIdValid());
// Report data from the session object.
out.println("<h3>Session Data</h3>");
out.println("New Session: " + session.isNew());
out.println("<br> Session ID: " + session.getId());
out.println("<br> Creation Time: " + new Date(session.getCreationTime()));
out.println("<br>Last Accessed Time: " +
new Date(session.getLastAccessedTime()));
out.println("</body>");
out.close();
}
public void doGet(HttpServletRequest-req,HttpServletResponse-res)
抛出ServletException、IOException{
//获取会话对象。如果该对象不存在,请创建一个新对象。
HttpSession session=req.getSession(true);
res.setContentType(“文本/html”);
PrintWriter out=res.getWriter();
out.println(“+”SessionServlet输出”+
"");
out.println(“SessionServlet输出”);
//设置会话命中计数器。“sessionservlet.counter”只是
//为要存储在中的值创建键的常规方法
//会话对象“字典”。
整数ival=
(整数)session.getAttribute(“sessionservlet.counter”);
如果(ival==null){
ival=新整数(1);
}
否则{
ival=新整数(ival.intValue()+1);
}
//保存计数器值。
session.setAttribute(“sessionservlet.counter”,ival);
//报告计数器值。
out.println(“您已点击此页面”+
ival+“次,”;
//此语句提供用户可以单击的目标
//激活URL重写。默认情况下不会这样做。
out.println(“点击”);
println(“以确保会话跟踪工作正常”+
“如果不支持cookies。
”;
println(“请注意,默认情况下未启用URL重写”+
“因为它的开销很大。”);
//根据请求报告数据。
out.println(“请求和会话数据”);
println(“请求中的会话ID:”+
req.getRequestedSessionId());
out.println(“
请求中的会话ID来自Cookie:”+
req.isRequestedSessionIdFromCookie());
out.println(“
请求中的会话ID来自URL:”+
req.isRequestedSessionIdFromURL());
out.println(“
有效会话ID:”+
req.isRequestedSessionIdValid());
//从会话对象报告数据。
out.println(“会话数据”);
out.println(“新会话:+Session.isNew());
out.println(“
会话ID:+Session.getId());
out.println(“
创建时间:”+新日期(session.getCreationTime());
out.println(“
上次访问时间:”+
新日期(session.getLastAccessedTime());
out.println(“”);
out.close();
}
谢谢你,拉米。那么,说
encodeURL()
只应在输出链接时使用(例如,在HTML页面或JSP中)正确吗?不客气。没错,如果链接被输出到用户,并且如果cookies被关闭,您需要维护会话状态,那么应该使用encodeURL。感谢您提供了清晰的示例。HttpUtils.getRequestURL()不是一个不受欢迎的方法吗?还有其他替代品吗?欢迎。您可以使用HttpServletRequest#getRequestURL()
实现它,非常感谢。。。。我还有一个小问题。当我第二次刷新页面时,上面定义的链接会自动被点击。原因是什么?