?
Solved

Servlet 3.0 Annotations in Tomcat 7.0.8

Posted on 2011-02-18
7
Medium Priority
?
1,073 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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