user460920
user460920

Reputation: 887

servlet 3.0 import package of annotation

In Servlets 3.0 we have to import the annotations package. So i want to know what are classes and interfaces?

import javax.servlet.annotation.WebServlet; 

What is here servlet, annotation and WebServlet a class or interface in the javax package?

Upvotes: 1

Views: 2067

Answers (1)

Ramesh PVK
Ramesh PVK

Reputation: 15446

Before annotations the only way to define any deployment properties was using deployments descriptors. For Web Applications, it was web.xml.

From JavaEE 5 annotations were supported which lets you define certain deployment properties. They were mostly related to resources the servlets used. But still the servlets has to defined in web.xml only.

Starting with Java EE 6, annotations such as @WebServlet, @WebFilter, @WebListener were introduced which lets you define the deployment properties in the java class itself. You do not have to mention them in web.xml. All the properties you can mention in web.xml can now be provided using @WebSerlvet annotation. And one can still override the properties using web.xml tag.

This is how Servlets can be defined using annotation:

import javax.servlet.annotation.WebServlet; 

 @WebServlet(asyncSupported = false, name = "HelloWorldServlet",
  urlPatterns = {"/hello"}, 
  initParams = {@WebInitParam(name="param1", value="value1"),
                @WebInitParam(name="param2", value="value2")}
 )
 public HelloWorldServlet extends HttpServlet
 {


  public void doGet(HttpSerlvetRequest request, HttpServletResponse response)
  {
   //write hello world.
  }

 }

Upvotes: 8

Related Questions