0

I have made the simple web application using servlets, here is the HelloClass.java file:

package com.hello;

import java.io.IOException;
import java.io.PrintWriter;
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("/report")
public class HelloClass extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
    throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");

        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + "Hello World" + "</h1>");
    }

}

I have read that if using annotations then you don't have to provide all servlets inside of web.xml.

Everything compiles well using the maven and deployed in glassfish, however I can't reach the working serlvet by typing in address bar in browser: localhost:8080/my-webapp/report

It says: HTTP Status 404 - Not Found

Here is pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
  </build>
</project>

EDIT: web.xml file provided

<?xml version="1.0" encoding="UTF-8"?>

<web-app
        version="3.0"
        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">

</web-app>
3
  • yes I got HTTP Status 404 - Not Found
    – maximus
    Aug 1, 2013 at 14:45
  • what webserver are you using? jboss, glassfish (It could be a good idea to add the tag)
    – jsedano
    Aug 1, 2013 at 14:47
  • 1
    I have mentioned it in the question: the glassfish.
    – maximus
    Aug 1, 2013 at 14:48

1 Answer 1

1

It appears you need to modify your web.xml to force version 3.0, in order to get annotations working.

Please view @WebServlet annotation with Tomcat 7 as the question is very similar.

4
  • I have put the web.xmp related with version 3.0, still can't access the web page.
    – maximus
    Aug 1, 2013 at 15:29
  • Would you mind posting your web.xml contents so we can get a better handle on the issue? Aug 1, 2013 at 15:43
  • This may be a Glassfish issue, as I'm more familiar with Tomcat, but that should be OK. Have you tested by using the xml-based servlet declaration (the one you're trying to avoid using)? Aug 1, 2013 at 16:14
  • I would recommend trying that method just to rule out issues not related to annotations. Aug 1, 2013 at 19:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.