CS636 Class 18 Servlets and JSP

Hw4 is available, due Nov. 28, after Thanksgiving break (no class Nov. 23)

Also tomcatSetup.html and port assignments, for your tomcat site on topcat.

First example: servlet1---look at online, execute using ant.

Email List Servlet Example in Chapter 5: ch05email

Example in Murach Chap 5, pg 134-135: have a HTML/JSP form with ACTION= <URL for servlet>, so servlet executes on submit. 

This is almost the same app as ch02email in Chap. 2. index.html has been renamed index.jsp, but works the same.
Look at slides (6pp) again.

This project is available at $cs636/ch05email, with a directory system compatible with eclipse web projects and a build.xml derived from the one for project servlet1.

Test your knowledge on this important example, ch05email:

How does the user input get to the servlet?
How does the servlet access the incoming parameters?

How does the servlet pass on this information to thanks.jsp (or its servlet)?

How does thanks.jsp get the information out into its response HTML so the user can see it?

More on forwarding the request for ch05email

The forwarding operation is a little mysterious because it utilizes a URL but does not involve handling an external request coming into the server. Instead it is used to “chain” work inside the server.  The code to do the forward in EmailListServlet is:

getServletContext().getRequestDispatcher(url).forward(request, response);

This shows the request and response objects being handed over to be reused with the target servlet. No other data objects are handed over, but we know lots of info can be attached to the request.

The url in this case is “/thanks.jsp”. This is neither a relative URL or an absolute one.  The / here represents the root directory of the webapp, …/ch05email in this case, so this is forwarding to /ch05email/thanks.jsp.  Again we see that we don’t need to know the webapp name to code the webapp, because this is relative to the root directory of the webapp, wherever that is. 

We must always use a URL like this for forward (see Javadoc), that is, it must start with a /. Forwards can only go to somewhere in our current webapp, not out into the Internet or even to another webapp on this server.

Added note: Another URL starting with / is shown on pg. 101.  It means something different here as the target of an href in HTML: it means the root of the whole webserver filesystem, i.e., the webapps directory of tomcat.

Only the request and response objects are handed over in a forward. All the other variables are not accessible to the forwarded-to servlet/JSP, unlike in PHP.  The trick is to attach attributes to request to let them ride across to the target.