You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Glassfish5部署Web应用后JSP无法连接Servlet报404错误求助

Troubleshooting 404 Error When Connecting JSP to Servlet on Glassfish (After Tomcat)

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 @WebServlet annotation: Make sure the urlPatterns value matches exactly what's in your JSP's form action. For example, if your Servlet has:
    @WebServlet("/empServlet")
    public class EmpServlet extends HttpServlet {
        // ...
    }
    
    Your JSP form should use:
    <form action="empServlet" method="POST">
        <!-- form fields -->
    </form>
    
    Avoid typos (like missing leading slashes or extra characters) here—even a small mistake will trigger a 404.
  • 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 your web.xml uses 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>
    

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.xml is 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 @EJB instead of manually instantiating it. Glassfish manages EJB lifecycles, so manual new 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:26:45