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

使用Glassfish与IntelliJ部署Java EE WebApp遇Servlet 404问题求助

Hey there, let's tackle this 404 issue with your AddPassenger servlet—since you're using IntelliJ instead of the Eclipse from your tutorial, there are a few specific gotchas to check. Let's work through the most common fixes step by step:

Common Fixes for Servlet 404 in IntelliJ + GlassFish

1. Verify Servlet Annotation Configuration

Since you're using Java EE, the easiest way to map a servlet is with the @WebServlet annotation. Double-check your AddPassenger class:

  • Make sure it has the annotation with a clear URL pattern, like this:
    @WebServlet("/add-passenger") // This defines the path you'll use to access the servlet
    public class AddPassenger extends HttpServlet {
        // Override doGet/doPost correctly (see step 3 for details)
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Your HTML output logic here
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<h1>Hello from AddPassenger!</h1>");
        }
    }
    
  • The URL you need to visit will be http://localhost:8080/web1_war_exploded/[your-url-pattern]—so if your annotation uses /AddPassenger, use that exact case (URLs are case-sensitive in GlassFish).

2. Check IntelliJ's Deployment Configuration

IntelliJ sometimes needs a nudge to deploy things correctly:

  • Open Run/Debug Configurations (click the run dropdown in the top-right, then "Edit Configurations")
  • Select your GlassFish server entry, switch to the Deployment tab
  • Confirm:
    • Your project (web1_war_exploded) is listed with the correct Application context (it should match /web1_war_exploded, since that's where your index.jsp works)
    • The module containing your AddPassenger class is included in the deployment artifacts

3. Ensure Proper Servlet Inheritance & Method Overrides

A common mistake is miswriting the servlet methods:

  • Make sure your class extends HttpServlet (import javax.servlet.http.HttpServlet or jakarta.servlet.http.HttpServlet depending on your Java EE version)
  • Double-check the method signature for doGet or doPost—it must match exactly:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Your code here
    }
    

If the signature is wrong (e.g., missing exceptions or using incorrect parameter types), the server won't recognize it as a valid request handler.

4. Clean & Redeploy to Fix Cache Issues

IntelliJ and GlassFish can hold onto old build files:

  • Click Build > Clean Project in the top menu
  • Restart your GlassFish server (or use the "Redeploy" button in the Run/Debug Configurations window)
  • You can also verify the deployment manually: navigate to your GlassFish domain's applications folder (usually glassfish/domains/domain1/applications) and check that web1_war_exploded/WEB-INF/classes contains your compiled AddPassenger.class file.

5. Validate web.xml Configuration (If Using It)

If you're not using annotations and instead mapping the servlet in web.xml, confirm the configuration is complete:

<servlet>
    <servlet-name>AddPassengerServlet</servlet-name>
    <servlet-class>com.yourpackage.AddPassenger</servlet-class> <!-- Use FULL package name here -->
</servlet>
<servlet-mapping>
    <servlet-name>AddPassengerServlet</servlet-name>
    <url-pattern>/add-passenger</url-pattern>
</servlet-mapping>

A missing or incorrect package name in <servlet-class> will cause the server to fail finding your servlet.

Once you've checked these points, try accessing the servlet again with the exact URL matching your annotation or web.xml mapping. Most of the time, it's a small configuration oversight specific to IntelliJ's deployment flow!

内容的提问来源于stack exchange,提问作者Steerpike

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:09:59