Servlet URL Rewriting

转载 2011年01月20日 15:16:00

 

7.3. URL Rewriting

URL rewriting is another way to support anonymous session tracking. With URL rewriting, every local URL the user might click on is dynamically modified, or rewritten, to include extra information. The extra information can be in the form of extra path information, added parameters, or some custom, server-specific URL change. Due to the limited space available in rewriting a URL, the extra information is usually limited to a unique session ID. For example, the following URLs have been rewritten to pass the session ID 123:

http://server:port/servlet/Rewritten                 original
http://server:port/servlet/Rewritten/123             extra path information
http://server:port/servlet/Rewritten?sessionid=123   added parameter
http://server:port/servlet/Rewritten;$sessionid$123  custom change

Each rewriting technique has its advantages and disadvantages. Using extra path information works on all servers, and it works as a target for forms that use both the GET and POST methods. It doesn't work well if a servlet has to use the extra path information as true path information, however. Using an added parameter works on all servers too, but it fails as a target for forms that use the POST method, and it can cause parameter naming collisions. Using a custom, server-specific change works under all conditions for servers that support the change. Unfortunately, it doesn't work at all for servers that don't support the change.

Example 7-2 shows a revised version of our shopping cart viewer that uses URL rewriting in the form of extra path information to anonymously track a shopping cart.

Example 7-2. Session tracking using URL rewriting

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShoppingCartViewerRewrite extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>");
    out.println("<BODY>");

    // Get the current session ID, or generate one if necessary
    String sessionid = req.getPathInfo();
    if (sessionid == null) {
      sessionid = generateSessionId();
    }

    // Cart items are associated with the session ID
    String[] items = getItemsFromCart(sessionid);

    // Print the current cart items.
    out.println("You currently have the following items in your cart:<BR>");
    if (items == null) {
      out.println("<B>None</B>");
    }
    else {
      out.println("<UL>");
      for (int i = 0; i < items.length; i++) {
        out.println("<LI>" + items[i]);
      }
      out.println("</UL>");
    }

    // Ask if the user wants to add more items or check out.
    // Include the session ID in the action URL.
    out.println("<FORM ACTION=/"/servlet/ShoppingCart/" + sessionid +
                "/" METHOD=POST>");
    out.println("Would you like to<BR>");
    out.println("<INPUT TYPE=submit VALUE=/" Add More Items /">");
    out.println("<INPUT TYPE=submit VALUE=/" Check Out /">");
    out.println("</FORM>");

    // Offer a help page. Include the session ID in the URL.
    out.println("For help, click <A HREF=/"/servlet/Help/" + sessionid +
                "?topic=ShoppingCartViewerRewrite/">here</A>");

    out.println("</BODY></HTML>");
  }

  private static String generateSessionId() {
    String uid = new java.rmi.server.UID().toString();  // guaranteed unique
    return java.net.URLEncoder.encode(uid);  // encode any special chars
  }

  private static String[] getItemsFromCart(String sessionid) {
    // Not implemented
  }
}

This servlet first tries to retrieve the current session ID using getPathInfo() . If a session ID is not specified, it calls generateSessionId() to generate a new unique session ID using an RMI class designed specifically for this. The session ID is used to fetch and display the current items in the cart. The ID is then added to the form's ACTION attribute, so it can be retrieved by the ShoppingCart servlet. The session ID is also added to a new help URL that invokes the Help servlet. This wasn't possible with hidden form fields because the Help servlet isn't the target of a form submission.

The advantages and disadvantages of URL rewriting closely match those of hidden form fields. The major difference is that URL rewriting works for all dynamically created documents, such as the Help servlet, not just forms. Plus, with the right server support, custom URL rewriting can even work for static documents. Unfortunately, actually performing the URL rewriting can be tedious.

 

伪静态URLRewrite学习笔记

UrlRewrite: UrlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址,类似于Apache的mod_rewrite。将我们的动态网页地址转化为静态的地址,如h...
  • dyllove98
  • dyllove98
  • 2013年07月09日 14:25
  • 32705

纯servlet调用Url传输文件或者字符串

实例:A和B两台服务器之间传输log文件或者其他文件,             如果成功就返回字符串"1";如果失败则返回"0" 1、发送log文件和字符串参数的servlet mport java...
  • high2011
  • high2011
  • 2016年03月04日 17:55
  • 1356

Servlet规范之URL映射

映射请求到Servlet |-- Context Path --|-- Servlet Path -|--Path Info--| http://www....
  • xinluke
  • xinluke
  • 2016年05月19日 00:23
  • 1637

在ASP.NET中实现Url Rewriting (三)

  • 2009年02月26日 11:29
  • 32KB
  • 下载

在ASP.NET中实现Url Rewriting (一)

  • 2009年02月26日 11:26
  • 36KB
  • 下载

在ASP.NET中实现Url Rewriting (五)

  • 2009年02月26日 11:31
  • 135KB
  • 下载

在ASP.NET中实现Url Rewriting (二)

  • 2009年02月26日 11:28
  • 46KB
  • 下载

URL Rewriting in ASP.NET1.1

  • 2008年01月23日 17:30
  • 536KB
  • 下载

Url Rewriting with ASP.NET

People often ask me for guidance on how they can dynamically "re-write" URLs and/or have the ability...
  • DL10129
  • DL10129
  • 2011年08月29日 07:39
  • 1872

在ASP.NET中实现Url Rewriting

1.1. 概要 分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的...
  • huajinx
  • huajinx
  • 2011年12月10日 08:53
  • 320
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Servlet URL Rewriting
举报原因:
原因补充:

(最多只允许输入30个字)