Servlet URL Rewrite : URL Rewrite « Servlet « Java Tutorial






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

public class MyServlet extends HttpServlet {
   
 public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        String contextPath = request.getContextPath();
        String encodedUrl =  response.encodeURL(contextPath + "/default.jsp");
       
        out.println("<html>");
        out.println("<head>");
        out.println("<title>URL Rewriter</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>This page will use URL rewriting if necessary</h2>");
        out.println("Go to the default.jsp page <a href=\"" + encodedUrl + 
            "\">here</a>.");
        out.println("</body>");
        out.println("</html>");
      
  } 
  
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
      doGet(request,response);
  
  }
}
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
    <servlet><servlet-name>MyServletName</servlet-name>
             <servlet-class>MyServlet</servlet-class>
        <init-param>
            <param-name>
                go
            </param-name>
            <param-value>
                weather
            </param-value>
        </init-param>
             
    </servlet>
    
    <servlet-mapping><servlet-name>MyServletName</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
</web-app>
  Download:  ServletURLRewrite.zip( 88 k)








25.29.URL Rewrite
25.29.1.Servlet URL Rewrite
25.29.2.Servlet Query String
25.29.3.URL rewriting