First hand on Servlet 3.0 using Annotation

   This tutorial gives introduction to coding and deploying Servlet as per 3.0 specification. Servlet 3.0 specification introduced Annotations that makes deploying servlet, Lister and Filter easier ever before.

Here is the Servlet with Annotation ready to deploy without any web.xml. Here @webservlet annotation is being used to define servlet name, url pattern and init parameters. This annatation is equivalent to following entry in web.xml

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>demo.HelloServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>Yogesh</param-value>
</init-param>
</servlet>

<
servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>


Complete Code :




/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package demo.servlet;

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;

/**
*
* @author Yogesh
*/
@WebServlet(name="HelloServlet", urlPatterns={"/HelloServlet"}, initParams={@WebInitParam(name="name", value="Yogesh")})
public class HelloServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = getInitParameter("name");
try {
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Good Morning</h2>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}





To run this file I have used NetBeans 6.8 , just created Java Web Application and normal java class HelloServlet. Running this class deploy HelloServlet to embedded server and gives following output in browser. 



Fullscreen capture 1112010 113721 AM.bmp

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example