import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SimpleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello, World"); } }It extends HttpServlet, which just means that it can override some built-in methods that are specific to http. One of these is doGet, which is called in response to a GET request. We'll talk more about the difference between GET and POST requests, but for now just assume that all requests from browsers to web servers are GET requests. Method doGet takes two arguments, an HttpServletRequest (
request
) and an HttpServletResponse
(response
). These provide a convenient way for the servlet
to get information from and send information to the client (e.g., the browser
that made the request). The request
argument has methods that let the servlet find out some things about
the client (e.g., its IP address, the time of the request, what kind
of responses it can handle, and so on, along with any data it chose
to send); the response argument has methods
that the servlet can use
to craft a response. One of these is the getWriter method, which
returns a PrintWriter. What the servlet writes to this PrintWriter
is sent as its response to the client (after some header information
that is set up by the servlet).
<servlet> <servlet-name>AHello</servlet-name> //Name this servlet -- may //or may not match class name <description> Yet another hello world. //Description -- documentation only </description> <servlet-class>HelloWorldServlet</servlet-class> //Give name of class </servlet> <servlet-mapping> <servlet-name>AHello</servlet-name> //Use servlet name from above <url-pattern>/AHello</url-pattern> //How you want to get this //in a url; note leading / //Need not match servlet name //but often does </servlet-mapping>
out.println("<HTML>\n"); out.println("<HEAD><TITLE>Hello</TITLE></HEAD>\n"); out.println("<BODY>"); ...Remember that you should print (to
out
)
exactly what you want in your html
document. The \n's aren't really necessary, but they make the HTML
a whole lot easier to look at if you ever have to (which you often do for
debugging).
As you can imagine, generating html like this gets old fast. One easy thing to do is to create a utility class that helps with the boilerplate stuff like the html header. Write such a class (e.g., MyServletUtils) that contains a static method that takes the title of the document as a string and returns a string containing all the html code between <HTML> and </HEAD>. Put this class in your src directory and modify your HtmlServlet class to use it instead of generating all of the html itself.
Often the client sends some information to the servlet that the servlet uses to craft its response -- this might be an order, the answers to a survey, or any other data you can imagine submitting via a web page. This information is often collected with an html form; a simple form that contains a textfield and a submit button is shown below:
The text to create this form is as follows (embedded in an HTML file):
<form action="http://cs.roanoke.edu:8080/bloss/MyTest" method="GET"> Please enter your name: <input type="text" name="userName"> <br> <input type="submit" value="Submit"> </form>When the submit button is pressed, the servlet in MyTest is executed (because it is the form's action -- see the first line) and is passed the information from the form. Since this is part of the request, it shouldn't be surprising that the form information is available to the servlet through its
request
parameter.
Class HttpServletRequest has a getParameter method that takes the
name of the parameter and returns its value. For a form, the name of
a parameter is usually its name attribute, so for this request
the servlet might contain the code
String who = request.getParameter("userName");If the user typed Mary in the textbox before pressing Submit, string
who
would then have value Mary. This,
of course, can then be used to construct the personalized output.
When you press Submit in the program above, you will notice that the name parameter had been attached to the URL as ?name=Mary. This is how parameter information is sent in a GET request, and you can do this explicitly as well. Instead of loading your test html page, just type in http://cs.roanoke.edu:8080/x/y?name=Mary (where x is your user id and y is the url-name for your servlet). You will get the same thing you got before -- the servlet can't tell how the input was generated, it just reads what comes after the ?.
When you click on Next Page, you should get page 3, which looks similar (except with a 3, of course). When you click on Previous Page, you should get page 1.
Notice that Previous Page and Next Page are links, so you would create them with the (anchor) tag (just think carefully about where you want to link to!). If you don't know enough html to do this, consult one of the many html tutorials on the web -- my favorite for basics is at http://www.cwru.edu/help/introHTML/toc.html.
When your infinite document works, think about this: what should happen when you click on Previous Page from page 1? Fix it so that it does something nicer than run through an infinite number of negative pages.