?
Solved

Servlet 3.0 Annotations in Tomcat 7.0.8

Posted on 2011-02-18
7
Medium Priority
?
1,094 Views
Last Modified: 2013-12-02
I have been trying to get the new Servlet 3.0 annotations to work in Tomcat 7.0.8  Am I too early on this ? Does anybody have the trick ?  
I googled. No luck so far.  Here is my Servlet.
package rrz;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sample") 
public class SampleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain");
        response.getWriter().write(new Date().toString());
    }
}

Open in new window

and my web.xml is  
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

Open in new window

I tried the following lines
@WebServlet(value="/sample", loadOnStartup=1)
@WebServlet("/sample")
@WebServlet(urlPatterns = {"/sample"})
@WebServlet(name = "SampleServlet", urlPatterns = {"/sample"})
@WebServlet(urlMappings = {"/sample"}) // didn't compile
but none of them worked.
I tried to browse to
http://localhost:8080/sample 
(I am using the preinstalled  ROOT web app)
0
Comment
Question by:rrz
  • 5
  • 2
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34929738
> @WebServlet(name = "SampleServlet", urlPatterns = {"/sample"})


that one looks correct, what are you getting when you use that

do the logs show tomcat starting it up ok
0
 
LVL 28

Author Comment

by:rrz
ID: 34929877
Hi objects, I tried this line again
> @WebServlet(name = "SampleServlet", urlPatterns = {"/sample"})
no luck.  
In console I had
Feb 18, 2011 1:18:19 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
in localhost_access_log
127.0.0.1 - - [18/Feb/2011:13:18:51 -0800] "GET /sample HTTP/1.1" 404 971
in browser I got
HTTP Status 404 - /sample
--------------------------------------------------------------------------------
type Status report
message /sample
description The requested resource (/sample) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.8
0
 
LVL 28

Author Comment

by:rrz
ID: 34929892
No error messages anywhere.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 34929973
>   metadata-complete="true">  

remove that or set it to false
0
 
LVL 28

Author Comment

by:rrz
ID: 34929994
Great. That works.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="false">  
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

Open in new window

0
 
LVL 28

Author Closing Comment

by:rrz
ID: 34930000
thanks object
0
 
LVL 28

Author Comment

by:rrz
ID: 34930082
These all work now.
@WebServlet(value="/sample", loadOnStartup=1)
@WebServlet("/sample")
@WebServlet(urlPatterns = {"/sample"})
@WebServlet(name = "SampleServlet", urlPatterns = {"/sample"})
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question