0

Hey Guys iam trying to make a simple Java Servlet that returns a JSON. The web.xml is in the WEB-INF directory and i checked already if i typed in the link false or something like that. Here is my ServletClass:

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "BetaAuthServlet", value = "/betaAuthServlet")
public class BetaAuthServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String message = "Test";
        out.print(message);
        out.flush();
    }
}

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>BetaAuthServlet</servlet-name>
        <servlet-class>de.hanimehagen.tridentdevelopment.betaauth.BetaAuthServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>BetaAuthServlet</servlet-name>
        <url-pattern>/BetaAuthServlet</url-pattern>
    </servlet-mapping>
</web-app>

When i lounch the App an go on localhost:8080/betaAuthList i got a 404 error. Iam not realy sure if i need a index.jsp but i think i dont need it. The Tomcat server should work fine. It would be very nice if someone could help me. Greetings Hagen

I found a solve for my problem: I used Tomcat Version 10. After i used Version 9 it works!

  • Hi & Welcome! the WEB-INF directory .... of "ROOT" (or which) application? Where is the compiled class? How did you "introduce" it "to tomcat"? – xerx593 Jan 15 '22 at 01:32
  • ..when you do `@WebServlet`, you don't need to/no web.xml. Here is a ["nice tutorial/article"](https://www.javaguides.net/2019/02/webservlet-annotation-example.html) ..just "package" that @webservlet into a "war" and deploy it. – xerx593 Jan 15 '22 at 01:36
  • Your `@WebServlet` and web.xml show the path as `/BetaAuthServlet` or `betaAuthServlet` - paragraph at end states you're trying to get `/betaAuthList`? – Heng Ye Jan 15 '22 at 01:51

1 Answers1

0

Here are some suggestions to check - it depends on your configuration within Tomcat server.

Say your code is packaged to xyz.war. All paths within xyz.war will have same root path, if no explicit path is registered, Tomcat may use the default xyz so all servlets under your web application will be of form (substitute xyz as appropriate):

http://localhost:8080/xyz/someotherpaths

You've specified both @WebServlet and web.xml but each give different servlet path /BetaAuthServlet and /betaAuthServlet. If your Tomcat is setup to auto-scan web applications with @WebServlet annotations, the try the setting:

http://localhost:8080/xyz/betaAuthServlet

If your Tomcat is not setup to auto-scan web applications try the web.xml setting:

http://localhost:8080/xyz/BetaAuthServlet
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • Hey i already already checked this. I have now created a new Project in intellij with the Java Enterprise Webapplication Template. There is a "/hello-servlet" auto created. If i run the default application also the /hello-servlet throws a 404. Also the Tomcat settings are auto created from intellij. So iam wondering why the Autocreated application has the same problem. – Hagen Kanne Jan 15 '22 at 11:33