简体   繁体   中英

Servlet error HTTP Status 404 – Not Found Java Tomcat

I know that there were similar problems already, but non of the solutions worked for me. I checked the directories and I edited my `web.xml file a couple of times but it still does not work.

I am writing a simple servlet in Java running on Tomcat and I am getting the error:

HTTP Status 404 – Not Found

Type Status Report

Message /WorkshopForm/MainWorkshopForm

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

My servlet class is:

package workshop;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
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(description = "This registration form", urlPatterns = { 
"/WorkshopForm" })


public class WorkshopForm {
public class MyServlet extends HttpServlet implements Servlet {
    private static final long serialVersionUID = 13425L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String participantName = request.getParameter("participantName");
    String participantSurname = request.getParameter("participantSurname");
    String participantEmail = request.getParameter("participantEmail");

    PrintWriter writer = response.getWriter();
    writer.println("Welcome" + participantName + " " + participantSurname + " " + participantEmail); 
    }       
}

}

My web.xml :

屏幕截图Web文件

Tree in Eclipse:

屏幕截图目录

Is it a problem with the web.xml file? I am thinking that maybe I have some mismatch with names or paths but I tried to solved it already and no idea why it is not working.

The URL pattern /WorkshopForm matches only the exact URL path /WorkshopForm . If you want the servlet to also handle longer paths like /WorkshopForm/MainWorkshopForm , you need to change the URL pattern to /WorkshopForm/* . Then you can call request.getPathInfo() in your servlet code to obtain the variable part of the path.

Another alternative is to use some JAX-RS framework to handle the mapping from URL paths to Java methods that handle individual paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM