Solved

Servlet 3.0 Annotations in Tomcat 7.0.8

Posted on 2011-02-18
7
1,067 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 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
Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

688 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