Solved

Servlet 3.0 Annotations in Tomcat 7.0.8

Posted on 2011-02-18
7
1,064 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Running JavaFX on JDeveloper 12C 1 54
Desingning Refactoring existing code 2 26
Java class and jar 3 23
This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
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…

860 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