Maven p20: Java servlets

To actually use Servlets in your webapp, we need a depedency which gives us all the J2EE goodness, the OSS Apache Geronimo project. The mvn depedency can be found here: Let's put that in the dependencies section of your pom.xml:


Now we need to tell web.xml that we're going to create a servlet, and give it a mapping also:

	  <display-name>Archetype Created Web Application</display-name>

We're saying they'll be a servlet, called simple, and we give its to-be-created class. Then we map its name to a url pattern. Let's now create that class in src/main/java/your.pky.simpleservlet

	package your.pkg.simpleservlet;

	import java.util.Date;

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

	public class SimpleServlet extends HttpServlet {
		public void doGet(HttpServletRequest request, 
				HttpServletResponse response)
			throws ServletException, IOException {

			PrintWriter out = response.getWriter();

			HttpSession lSess = request.getSession(true);
			int lInactive = lSess.getMaxInactiveInterval();
			long lTime = lSess.getLastAccessedTime();
			lSess.setAttribute("bar", "Hello, session attribute!");
			String att = (String) lSess.getAttribute("bar");


		public void init() throws ServletException {

		public void destroy() {

Notice we're importing lots of servlet classes. These are given to use by the Geronimo project. The class extends a HttpServlet. init() and destory() are called at moments that you can imagine well enough yourself. The doGet() is called during a GET request. Whatever's written to the HttpServletResponse is outputted. And we can get session information, even getting session variables.

maven java java-servlet

Edit on github
comments powered by Disqus
Click me