We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 5 April 2014

Servlet - Hello World Sevlet using @WebServlet Annotation

In this tutorial we are going to see about Hello World Servlet  program using Servlet 3 @WebServlet Annotation.
public @interface WebServlet
Annotation used to declare a servlet.
This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called WebServletAnnotation as shown in figure.
Create Servlet
http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.
http servlet Services
7. @WebServlet in Servlet3 will map the url in Servlet itself as annotation, Instead of mapping Servlet in web.xml.
Servlet Mapping in Annotation will be look like this
@WebServlet("/WebServletAnnotation")

8. Add the required code inside doGet() method.

WebServletAnnotation.java

package com.javatutorialscorner.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WebServletAnnotation
 */
@WebServlet("/WebServletAnnotation")
public class WebServletAnnotation extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private String msg = "Java Tutorials Corner Hello World Servlet Program using  @WebServlet Annotation";

 /**
  * @see HttpServlet#HttpServlet()
  */
 public WebServletAnnotation() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("text/html");
  PrintWriter writer = response.getWriter();
  writer.write("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet - @WebServlet Annotation</title><body><h2>"
    + msg + "</h2></body></html>");
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

10.call the URL which is mapped @WebServlet Annotation

http://localhost:8080/ServletExample/WebServletAnnotation

Output

image

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet - Hello World Sevlet using @WebServlet Annotation Rating: 5 Reviewed By: eHowToNow