Page 1 of 1

Simple Servlet 3.0 Tutorial Rate Topic: -----

#1 javadork  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Posted 09 November 2010 - 10:09 PM

The following tutorial will show you how to create servlets using the Java Servlet 3.0 API. Servlet 3.0 is a step-up from prior Servlet incarnations with major enhancements and new features like asynchronous support, easier configuration via annotations (no more web.xml configuration if you so desire), and an enhanced API. This tutorial does not attempt to explain the details and intricacies of Servlet 3.0, but is rather designed to get you up and running quickly and to provide a springboard to further tinkering and research.

In my previous tutorial, Simple EJB 3.0, the example was done in a text editor and manually compiled and built. For this example, however, we'll make it more exciting and use the Eclipse IDE. You can always do it the old fashioned way if you wish, however.

To compile and build Servlet 3.0, you'll need Java 6 installed. The version of Eclipse you'll want to use is Eclipse IDE for Java EE Developers (current version is Helios). Download and unpack. To run Servlet 3.0 applications, I used the latest Tomcat beta (as of this writing, it is 7.0.4). Unzip the contents of the archive and follow the instructions in RUNNING.txt to start the Tomcat server. I'll refer to the installation directory as $CATALINA_HOME, just as Apache does in RUNNING.txt. On my machine, $CATALINA_HOME would be C:\apache-tomcat-7.0.4

Now, fire up Eclipse.

- From the File menu, choose New / Dynamic Web Project (or, New / Project... / Web / Dynamic Web Project).

- For project name specify ServletTutorial and set the target runtime to Apache Tomcat v7.0. Click Finish.

- Right-click the ServletTutorial project and select New / Servlet. Specify package as example, the class name as CalcServlet, and superclass as javax.servlet.GenericServlet, then click Finish.

- Although we could start with the default code that Eclipse generates, let's use this instead:

package example;

import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet(
		value="/calc", 
		name="calc-servlet"
			)
public class CalcServlet extends GenericServlet {
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		response.getWriter().println("The result of adding 1 to 1 is " + (1 + 1) + ".");
	}
}



- Save your work.

- Next, right-click the ServletTutorial project and select Export / WAR file. We'll package the servlet into a WAR file and deploy it directly to the Tomcat webapps directory (destination: $CATALINA_HOME\webapps\calc-app.war). The application name is the same as the WAR file name, calc-app. Click Finish and the WAR will be deployed. To verify, you can check the Tomcat logs located at $CATALINA_HOME\logs.

- The final step is to test the servlet, so in your browser point to http://localhost:8080/calc-app/calc, where calc-app is the application name and calc is value from @WebServlet annotation.

- The output in your browser should be

The result of adding 1 to 1 is 2.



Let's try another example, this time using the @WebInitParam annotation to supply values to our servlet. @WebInitParam is equivalent to <init-param> that you'd find in web.xml.

- In our project, create a new servlet using the same parameters as the first example but use the class name to CalcServletWithParam.

- Add the following code:

package example;

import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(
		value="/calcwparam",
		initParams={@WebInitParam(name="n1", value="1"), @WebInitParam(name="n2", value="1")}
			)
public class CalcServletWithParam extends GenericServlet {
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		String n1 = getInitParameter("n1"); response.getWriter().print("Parameter n1 = " + n1);
		String n2 = getInitParameter("n2"); response.getWriter().println(", Parameter n2 = " + n2);
		response.getWriter().println("The result of adding " + n1 + " to " + n2 + " is " + (Integer.parseInt(n1) + Integer.parseInt(n2)) + ".");
	}
}



- Save your work.

- Create the WAR file and deploy as before, then run the servlet in your browser http://localhost:808...-app/calcwparam

- The output in your browser should be

Parameter n1 = 1, Parameter n2 = 1
The result of adding 1 to 1 is 2.



Obviously, this tutorial just barely scratches the surface, but it has given you a glimpse at the power and simplicity of the new servlet specification compared to prior versions. In my opinion, Servlet 3.0 is a huge step in the right direction. You can learn more at the Java Servlet 3.0 Specification. As new as Servlet 3.0 is, there are a ton of resources out there on the web, so use Google judiciously.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1