Using Request Data

Example 4-1 shows a servlet that uses many of the HttpServletRequest methods.

Example 4-1. Using HttpServletRequest methods
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
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 i = parameters.keySet( ).iterator( ); while (i.hasNext( ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.