简体   繁体   中英

Http 404/500 error with Java servlets?

I am following the Murach book on servlets and I put together a really basic servlet but I keep getting a 404 error. Following is the code:

The servlet code:

package murach;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    PrintWriter output = response.getWriter();
    response.setContentType("text/html");
    output.write("<h1>" + request.getParameter("txtInput") + "</h1>");
    output.close(); 
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    doPost( request, response);
}
}

And the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"....>
 <display-name>Servlet</display-name>

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>murach.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>TestServlet</servlet-name>
     <url-pattern>/testServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
   <welcome-file>index.html</welcome-file>
   </welcome-file-list>
</web-app>

And the main the html page(index.html) that calls the servlet:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Test Servlet</title>
</head>
<body>
<form action="testServlet" method="post">
    <input type="text" name="txtInput">
    <input type="submit" value="Submit">
</form>
</body>
</html>

And the project setup, if relevant: 在此处输入图片说明

UPDATE : I have fixed the issue with testServlet not being same as TestServlet. Now I am getting a Http 500 error: 在此处输入图片说明

Your class did not compile because the core servlet libraries are missing from the build path.

For a web application it is not just necessary to have the jars in the build path, but the related lib jars should also present under 'WebContent/WEB-INF/lib' directory.

So put the jar in the lib folder present under WebContent and try running the application.

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