December 2003
Intermediate to advanced
764 pages
24h 58m
English
One of the
arguments
passed to the doGet( ) and doPost(
) methods is an object that implements the
HttpServletRequest
interface. This interface defines methods that provide access to a
wealth of information about the request. Example 19-2
illustrates the use of the most common methods.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloYou extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
if (name == null) {
name = "you";
}
response.setContentType("text/html");
PrintWriter out = response.getWriter( );
out.println("<html><body>");
out.println("<h1>Hello " + name + "</h1>");
out.println("I see that:<ul>");
String userAgent = request.getHeader("User-Agent");
out.println("<li>your browser is: " + userAgent);
String requestURI = request.getRequestURI( );
out.println("<li>the URI for this page is: " +
requestURI);
String contextPath = request.getContextPath( );
out.println("<li>the context path for this app is" +
contextPath);
String servletPath = request.getServletPath( );
out.println("<li>this servlet is mapped to: " +
servletPath);
String pathInfo = request.getPathInfo( );
out.println("<li>the remaining path is: " + pathInfo);
Map parameters = request.getParameterMap( ); out.println("<li>you sent the following params:<ul>"); Iterator ...Read now
Unlock full access