Saturday 12, Apr 2025
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

Related Posts:

  • Servlet – Log4j Configuration in Web Application In this tutorial we are going to see about, how to configure Log4j in web application.See more Log4j Examples 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project ca… Read More
  • Servlet - Hello World GenericServletIn this tutorial we are going to see about GenericServlet with example program.A Servlet is just normal class which implements the Interface Servletjavax.servlet.ServletThe following classes implements the Servlet Interface.i… Read More
  • Servlet IntroductionServletServlet is a component based, platform independent technology for build the web application.Initially Common Gateway Interface (CGI) server-side scripts were the main technology for build web application with dynamic c… Read More
  • Servlet – Request Dispatcher include In this tutorial we are going to see about Servlet Request Dispatcher include with example program. void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException Includes the conte… Read More
  • Servlet – File Upload using Servlet-3 @MultipartConfig Annotation In this tutorial we are going to see about File upload using Servlet 3 @MultipartConfig Annotation. In previous tutorial I wrote File upload using Apache File Upload API click here to see File Upload using Apache File upload… Read More
  • 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