Glassfish5部署Web应用后JSP无法连接Servlet报404错误求助
Hey Julia, sorry to hear you're stuck with this 404 issue after switching from Tomcat to Glassfish—let's walk through the most common culprits and fixes, since these two servers have key differences that often cause these kinds of hiccups.
1. Verify Servlet Mapping Configuration
First, double-check how your Servlet is mapped, because Tomcat and Glassfish handle mappings (whether via annotations or web.xml) with subtle differences:
- If using
@WebServletannotation: Make sure theurlPatternsvalue matches exactly what's in your JSP's form action. For example, if your Servlet has:
Your JSP form should use:@WebServlet("/empServlet") public class EmpServlet extends HttpServlet { // ... }
Avoid typos (like missing leading slashes or extra characters) here—even a small mistake will trigger a 404.<form action="empServlet" method="POST"> <!-- form fields --> </form> - If using
web.xml: Confirm your<servlet>and<servlet-mapping>entries are correctly paired, and that the<url-pattern>matches the JSP's action. Also, ensure yourweb.xmluses a Servlet version compatible with Glassfish (Servlet 3.0+ is standard for modern Glassfish versions):<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>EmpServlet</servlet-name> <servlet-class>app.servlet.EmpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EmpServlet</servlet-name> <url-pattern>/empServlet</url-pattern> </servlet-mapping> </web-app>
2. Fix EJB-Related Configuration (Critical for Glassfish)
Your empBean is marked as a @Stateless EJB—this is a key point because Tomcat doesn't support EJBs, so you likely used it as a plain Java class there. Glassfish, however, is a full Java EE server and requires proper EJB setup:
- Ensure your project is packaged as a Java EE web application (WAR file) and not a plain Tomcat web project.
- Check that your
persistence.xmlis in the correct location:WEB-INF/classes/META-INF/persistence.xml. This file needs a valid persistence unit that Glassfish can recognize (make sure the datasource name matches what's configured in Glassfish's admin console). - In your Servlet, inject the EJB using
@EJBinstead of manually instantiating it. Glassfish manages EJB lifecycles, so manualnew EmpBean()will cause issues:public class EmpServlet extends HttpServlet { @EJB private EmpBean empBean; // Correct injection // ... }
3. Check Context Root and Deployment Path
Glassfish sometimes uses different context roots than Tomcat:
- Confirm your app's context root in Glassfish's admin console (default URL:
http://localhost:4848). Go to Applications → find your app → check the "Context Root" column. - If your context root is, say,
/employeeApp, your JSP's form action should include it (or use a relative path that resolves correctly):<form action="/employeeApp/empServlet" method="POST"> - Also, verify your app is fully deployed without errors in the Glassfish console—look for any red flags in the deployment logs.
4. Debug with Logs and Direct URL Tests
- Check Glassfish logs: Navigate to your Glassfish domain's log directory (e.g.,
glassfish4/glassfish/domains/domain1/logs/server.log) and search for errors related to your Servlet or EJB. Common issues here include missing classes, invalid persistence units, or mapping conflicts. - Test the Servlet directly: Try accessing the Servlet's URL directly in your browser (e.g.,
http://localhost:8080/your-context-root/empServlet). If you get a 404 here, the problem is definitely with the Servlet mapping. If it loads (even with an error), the issue is in your JSP's form action path.
内容的提问来源于stack exchange,提问作者Julia K

