NetBeans中Servlet HTTP 500错误反复出现,求助排查
Let’s walk through the most likely causes of your HTTP 500 error, using the code and environment details you’ve shared. I’ll start with the most common issues given your dual-JDK setup and NetBeans/Tomcat configuration:
1. Ensure Consistent JDK Version Across Compilation and Server Runtime
You installed JDK 8 to fix an -Xbootclasspath error with JDK 14—but mixed JDK versions often cause runtime compatibility hiccups (Tomcat 9 officially supports JDK 8 through 11, with limited support for newer releases). Here’s how to lock in consistency:
- Right-click your project in NetBeans → Properties → Build → Compiling: Set Source/Binary Format to JDK 8.
- Navigate to Run → Server: Confirm your Tomcat instance is configured to use JDK 8 (NetBeans lets you specify the JDK for each server instance separately).
- Clean and rebuild your project (right-click → Clean and Build) to ensure all class files are compiled with JDK 8’s bytecode standards.
2. Verify Servlet Class Deployment
A missing or mislocated Servlet class is the top culprit for 500 errors. Double-check these details:
- After building, navigate to your project’s
build/web/WEB-INF/classes/com/anothertest/directory. You should seeServletTest.classhere—if it’s missing, your code didn’t compile correctly. - Confirm
ServletTest.javalives in the correct package directory:src/main/java/com/anothertest/(standard for NetBeans web projects).
3. Validate web.xml Configuration (and Check for Typos)
Your web.xml looks structurally correct, but small typos can break everything:
- Make sure the
<servlet-class>value (com.anothertest.ServletTest) exactly matches your Java class’s package and name (Java is case-sensitive—no mistakes likeservletTestorcom.AnotherTest). - The
<url-pattern>/ServletTestmatches the href in yourindex.html(<a href="ServletTest">click</a>)—this part checks out, but if your project uses a custom context path, you might need to adjust the href to/${pageContext.request.contextPath}/ServletTest(though this is less likely to trigger a 500).
4. Check Tomcat’s Error Logs for Exact Details
HTTP 500 is a generic error—Tomcat’s logs will tell you exactly what’s wrong (e.g., ClassNotFoundException, NoClassDefFoundError):
- Open
[Tomcat Installation Directory]/logs/localhost_err.logorlocalhost.logright after triggering the error. The stack trace at the bottom will point directly to the root cause (missing class, incompatible bytecode, etc.).
Your Provided Code (For Reference)
index.html
<html> <head> <title>title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div>TODO write content</div> <a href="ServletTest">click</a> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" 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_3_1.xsd"> <servlet> <servlet-name>ServletTest</servlet-name> <servlet-class>com.anothertest.ServletTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletTest</servlet-name> <url-pattern>/ServletTest</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>
ServletTest.java
package com.anothertest; 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 ServletTest extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try ( PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet ServletTest</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet ServletTest at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
内容的提问来源于stack exchange,提问作者Carlos Miranda

