import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * Servlet that prints the value of a parameter, * optionally in color. */ public class PrintThis extends HttpServlet { /** * Respond to any HTTP GET request with an * page displaying the value of the "arg" * parameter. */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set the HTTP content type in response header. response.setContentType("text/html; charset=\"UTF-8\""); // Obtain a PrintWriter object for creating the body // of the response PrintWriter servletOut = response.getWriter(); // Create the first part of the body of the response servletOut.println( " \n" + " \n" + " \n" + " \n" + " PrintThis.java \n" + " \n" + " \n" + " \n" + "

Query string: " + WebTechUtil.escapeXML(request.getQueryString()) + "

" ); // Decide whether or not to set color String color = request.getParameter("color"); if (color == null) { servletOut.println( "

" ); } else { servletOut.println( "

" ); } // Decide which string to output String arg = request.getParameter("arg"); if (arg == null) { arg = "Hello World!"; } // Create remainder of response body servletOut.println( " " + WebTechUtil.escapeXML(arg) + "\n" + "

\n" + " \n" + " "); servletOut.close(); } }