Java Servlet 3.0 Tutorial: WebServlet Annotations with NetBeans 7, Jetty and Maven

Post to Twitter

Java Servlet 3.0 has been officially released for a while now. Today I’ll show you one of the new features: WebServlet Annotations. I’ll be using Netbeans 7 with Jetty and Maven to build and run the demo code.


Create a new Maven Web Application with NetBeans 7 called: Servlet3Demo

Once the project is created make sure to remove the index.jsp file as we will not be using that.

Add a new servlet to the project called: MyServlet

Prior to Servlet 3.0 you had to put all your servlet, filter, etc. information in the web.xml file. With annotations now you don’t have to rely on web.xml anymore. Below is the code for the MyServlet, note the use of annotations to define this servlet and to set some initial parameters.

package com.giantflyingsaucer.servlet3demo;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(asyncSupported = false, name = "MyServlet", urlPatterns = {"/"},
initParams = {@WebInitParam(name="webInitParam1", value="Hello"), @WebInitParam(name="webInitParam2", value="World")})
public class MyServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("MyServlet");
            out.write(getServletConfig().getInitParameter("webInitParam1") + " ");
            out.write(getServletConfig().getInitParameter("webInitParam2"));
            out.println("");
            out.println("");
        } finally {            
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

Now we need to add support to run Jetty and have it load up the WAR file compliments of Maven. Modify your POM to include Jetty and the Jetty plugin.



    4.0.0

    com.giantflyingsaucer
    Servlet3Demo
    1.0-SNAPSHOT
    war

    Servlet3Demo

    
        ${project.build.directory}/endorsed
        UTF-8
    

    
        
            org.mortbay.jetty
            jetty-maven-plugin
            8.0.1.v20110908
        
        
            javax
            javaee-web-api
            6.0
            provided
        

        
            junit
            junit
            4.8.2
            test
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.6
                    1.6
                    
                        ${endorsed.dir}
                    
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.1.1
                
                    false
                
            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                2.1
                
                    
                        validate
                        
                            copy
                        
                        
                            ${endorsed.dir}
                            true
                            
                                
                                    javax
                                    javaee-endorsed-api
                                    6.0
                                    jar
                                
                            
                        
                    
                
            
            
                org.mortbay.jetty
                jetty-maven-plugin
                8.0.1.v20110908
                
                    10
                    
                        /
                    
                
            
        
    


Set the project properties for Maven like the following:

Run your project, NetBeans 7 will display a prompt which you can use the following settings.

Load the project in your browser: http://localhost:8080

You should see Hello World in the browser.

Post to Twitter

This entry was posted in Java, Netbeans. Bookmark the permalink.

One Response to Java Servlet 3.0 Tutorial: WebServlet Annotations with NetBeans 7, Jetty and Maven

  1. Pingback: Building a simple REST service using NetBeans 7, a Java Servlet, and JAXB | Giant Flying Saucer

Comments are closed.