Solved

Servlet 3.0 Annotations in Tomcat 7.0.8

Posted on 2011-02-18
7
1,058 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 27

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 27

Author Comment

by:rrz
ID: 34929892
No error messages anywhere.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Accepted Solution

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

remove that or set it to false
0
 
LVL 27

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 27

Author Closing Comment

by:rrz
ID: 34930000
thanks object
0
 
LVL 27

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now