0

I have following Servlet .

package com.ser1;

import java.io.IOException;

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

/**
 * Servlet implementation class FileDao
 */
@WebServlet("/FileDao")
public class FileDao extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public FileDao() {
    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
}

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

}

I just created this new Servlet in eclipse but I am understanding the line @WebServlet("/FileDao"). Can someone please tell what is the use of @WebServlet("/FileDao") and how to resolve the error ? Here is the error shown by eclipse

@WebServlet("/FileDao") this line is shown as error in eclipse WebServlet cannot be resolved to a type The attribute value is undefined for the annotation type WebServlet

user1121210
  • 83
  • 1
  • 4
  • 14
  • 1
    which error ? & http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html – jmj Mar 04 '14 at 08:24
  • 1
    where is error? put the error code also – Engineer Mar 04 '14 at 08:25
  • @WebServlet("/FileDao") this line is shown as error in eclipse WebServlet cannot be resolved to a type The attribute value is undefined for the annotation type WebServlet – user1121210 Mar 04 '14 at 08:26
  • Did you import correct jar? – Leos Literak Mar 04 '14 at 08:28
  • Make sure your project is set up to use servlet spec 3.0 or above and include the import the for the annotation [@WebServlet](http://docs.oracle.com/javaee/7/api/javax/servlet/annotation/WebServlet.html). This annotation is used to configure the servlet, see the linked javadoc. – A4L Mar 04 '14 at 08:29
  • Is not it duplicate of http://stackoverflow.com/questions/7595797/cant-import-javax-servlet-annotation-webservlet? – Leos Literak Mar 04 '14 at 08:30
  • it is spec 3 . I am not getting proper jar file . I did not understand need for that line – user1121210 Mar 04 '14 at 08:37
  • put import javax.servlet.annotation.WebServlet at top of class. Does it resolve? – Leos Literak Mar 04 '14 at 08:41
  • @ Leos can u give me proper jar file. I am sure its because of wrong jar file – user1121210 Mar 04 '14 at 08:44
  • I googled this link: http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/ Or you can search your web container libs folder. And remember, that it must no be copied to war, it just compilation dependency. – Leos Literak Mar 04 '14 at 08:52
  • @ Leos thanks buddy .It is working but i do not understand what is need of @WebServlet – user1121210 Mar 04 '14 at 08:53
  • This is programmatic way of creating servlet. Otherwise you would have to declare it in web.xml. I will post an answear – Leos Literak Mar 04 '14 at 08:55

2 Answers2

1

You need to import proper annotation:

import javax.servlet.annotation.WebServlet 

and import servlet3.jar into your project as compile time dependency. Do not copy it to war, otherwise it will break deployment (or do some strange things). Jar can be either copied from your web container (tomcat) or from Oracle.

This annotation is used to define servlet in programmatic way. It is faster and more convinient than writing cca 8 xml tags in web.xml. See oracle tutorial.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • Try to open URL http://yourserver:yourport/yourcontext/FileDao, replace with actual values (localhost, 8080, your war name) – Leos Literak Mar 04 '14 at 09:24
1

You have to assign a server to your project. Right click on project and select Properties (last one) Select on left panel "Targeted Runtimes" and select your server. Click on and re-import the classes if it's needed with Ctrl + Shift + O

user3551863
  • 301
  • 3
  • 7