O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required