Wednesday, October 23, 2013

How do I make servlet requests look like static content requests?
Author: Deron Eriksson
Description: This Java servlet tutorial describes mapping URLs ending with .html to a servlet.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 >

One great thing about JavaSW web application technology is that it's so flexible. In fact, in your web.xmlW file, it's possible to map file extensions (like .html) to a servletW. This tutorial will describe this technique and how you can use it to pass values to a servlet. The tomcat-demo project has a web.xml file and one servlet called TestServlet.
'tomcat-demo' project
In web.xml, the url-pattern '/normal' maps to TestServlet, so if you hit "http://localhost:8080/tomcat-demo/normal", you get directed to TestServlet. The web.xml file also contains a url-pattern '*.html' mapping to TestServlet, which means that any requests to your application where the URL ends in '.html' will get directed to TestServlet. Examples of this are "http://localhost:8080/tomcat-demo/abc.html" and "http://localhost:8080/tomcat-demo/ok/123.html".

web.xml

 version="1.0" encoding="UTF-8"?>
 id="tomcat-demo" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
		TestServlet
test.TestServlet TestServlet /normal TestServlet *.html
TestServlet displays the request URL. It tries reading in the 'my-param' parameter if it's being passed to the servlet using the standard GET method. If 'my-param' is present, it displays the parameter value. If the parameter isn't present, it looks to see if it can pull out a value from the request URL between the last underscore and a '.html' on the URL. If it can do this, it displays this value. It also contains three hyperlinks that all get directed to TestServlet.

TestServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet
");
		String requestUrl = request.getRequestURL().toString();
		out.println("
Request URL: " + requestUrl);

		String val = request.getParameter("my-param");
		if (val == null) {
			if ((requestUrl.lastIndexOf("_") != -1) && (requestUrl.lastIndexOf(".html") != -1)) {
				val = requestUrl.substring(requestUrl.lastIndexOf("_") + 1, requestUrl.indexOf(".html"));
				out.println("
Value: " + val);
			}
		} else {
			out.println("
Parameter Value: " + val);
		}

		out.println("

This hits TestServlet");
		out.println("
This looks static but it hits TestServlet");
		out.println("
This also looks static but it hits TestServlet");
	}
}


If we fire up the project in EclipseSW, we can hit TestServlet via "http://localhost:8080/tomcat-demo/normal", as shown below.
http://localhost:8080/tomcat-demo/normal
The "This hits TestServlet" link goes to TestServlet via the '/normal' url pattern, and it includes the 'my-param' parameter with the value of 'blah1'. If we click this link, we see that the full URL is "http://localhost:8080/tomcat-demo/normal?my-param=blah1", and we see that the servletW displays the 'blah1' value of 'my-param'.
http://localhost:8080/tomcat-demo/normal?my-param=blah1
The "This looks static but it hits TestServlet" link is next in the list of links. Its href value is "dynamic_blah2.html".
link goes to dynamic_blah2.html
If we click this link, we see that the request URL is "http://localhost:8080/tomcat-demo/abc/dynamic_blah2.html". Although this looks like a static htmlW page based on the URL, it's actually hitting the servlet! Not only that, but TestServlet parses the request URL and pulls out 'blah2' from the request URL and displays it. Although there is no request parameter like we had in the first link, we actually parsed the request URL so that we could read in a value passed to the servlet as part of the URL. Sneaky, huh?
http://localhost:8080/tomcat-demo/abc/dynamic_blah2.html
As another example, we have a third link: "This also looks static but it hits TestServlet".
link goes to dynamic_blah3.html
If we click the third link, we see that the request URL is "http://localhost:8080/tomcat-demo/abc/dynamic_blah3.html". Once again, this request URL ending in '.html' gets sent to TestServlet, which pulls out 'blah3' from the URL and displays it.
http://localhost:8080/tomcat-demo/abc/dynamic_blah3.html
This can be a very useful mechanism if you want a dynamic web site but you'd like the URLs to be clean without request parameters.

No comments: