使用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:
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
AddPassengerclass is included in the deployment artifacts
- Your project (
3. Ensure Proper Servlet Inheritance & Method Overrides
A common mistake is miswriting the servlet methods:
- Make sure your class extends
HttpServlet(importjavax.servlet.http.HttpServletorjakarta.servlet.http.HttpServletdepending on your Java EE version) - Double-check the method signature for
doGetordoPost—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 thatweb1_war_exploded/WEB-INF/classescontains your compiledAddPassenger.classfile.
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

