example.servlet.basic
Class URLServlet

java.lang.Object
  extended byHttpServlet
      extended byexample.servlet.basic.URLServlet

public class URLServlet
extends HttpServlet

Shows how to extract the URL components.

 http://localhost:8080/tutorial/servlet/example.URLServlet/foo?a=b
 
getSchemehttp
getServerNamelocalhost
getServerPort8080
getContextPath/tutorial
getServletPath/servlet/example.URLServlet
getPathInfo/foo
getQueryStringa=b

The contextPath, servletPath, and pathInfo are related to how URLs map to servlets.

Each host is composed of one or more web-applications, specified by a URI prefix, the contextPath. This tutorial, for example, is in a web-app named /tutorial. A web-app is a miniature virtual host. The web-apps are protected from each other and the classes are entirely distinct. Because web-apps are often renamed, the servlets should be written not to care what the contextPath is. For example, any absolute URL needs to add the contextPath. If the servlet wanted a link to /tutorial/foo.jsp, it would use:

 out.println("<a href=\"" + request.getContextPath() + "/foo.jsp>");
 

servletPath is the URL that maps directly to the servlet. Examples would be /servlet/example.URLServlet or /test/foo.jsp. (Since *.jsp files are really servlets.)

pathInfo is the part of the URL after the servlet path. Servlets will often use the pathInfo as some sort of database key.

queryString is the query part of the URL. Normally, servlets use the getParameter call instead of parting the query string directly.

See Also:
Serialized Form

Constructor Summary
URLServlet()
           
 
Method Summary
 void doGet(HttpServletRequest request, HttpServletResponse response)
          Handles GET requests.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLServlet

public URLServlet()
Method Detail

doGet

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
           throws java.io.IOException,
                  ServletException
Handles GET requests. Resin will call the doGet method when the browser sends a GET request.

Parameters:
request - the request object contains the data from the browser's request.
response - the response object contains methods to send data back to the browser.
Throws:
java.io.IOException
ServletException