Tagged: j2ee

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: http://mvnrepository.com/artifact/org.apache.geronimo.specs/geronimo-servlet_2.5_spec/1.2 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.io.IOException;
    import java.io.PrintWriter;
    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 j2ee

Maven p19: Starting a Java webapp

You can create a simple java webapp with this, noting the archetypeArtifactId.

    mvn archetype:create -DgroupId=your.namespace -DartifactId=yourwebapp -DarchetypeArtifactId=maven-archetype-webapp

This is create very simple project, with the web descriptor in src/main/webapp/WEB-INF/web.xml. This is where you configure the app, but at the moment there’s on the display-name in, which if ever used is only used in IDEs and such descriptively. The src/main/webapp/index.jsp is just some simple HTML, bigger apps it would have java commands delimited with <% %>.

To run this quickly, instead of setting up Tomcat or whatever, you can install the jetty plugin for maven, allowing you to start a small web server to see the webapp quickly. Put this in your pom.xml within the build tag:


Now you can run mvn jetty:run and your webapp will be available at

Obviously this has not servlets or anything, so it’s very basic!

maven maven-web j2ee java jetty

Page 1 of 1