Thursday, June 7, 2012

Servlet with annotations.

Here, I have used all possible annotations for servlet.
Just compile it and copy MyServlet.class file to WEB-INF/classes folder and start the server.
and hit url like http://localhost:8080/APP_NAME/one or http://localhost:8080/APP_NAME/two.


package com.web.control;

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(
 description = "Servlet Description",
 urlPatterns = { "/one", "/two" },
 initParams  = {
            @WebInitParam(name = "first", value = "Leninkumar", description = "first description"),
            @WebInitParam(name = "last", value = "Koppoju", description = "last description") })
public class MyServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out = response.getWriter();
            String first = this.getInitParameter("first");
            String last = this.getInitParameter("last");
            out.println("Hi, " + first + " " + last + "!");      
      }

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

No comments:

Post a Comment