Oracle JDeveloper中Servlet JSP AJAX Get请求404错误求助
Hey there, let's troubleshoot that 404 error you're seeing when your input loses focus. A 404 means the server can't locate the GetUserServlet resource you're requesting, so let's walk through the most common fixes step by step:
1. Correct the AJAX Request URL (Most Likely Culprit)
Your AJAX call uses url : 'GetUserServlet', but this relative path might not account for your project's context root (the name your app is deployed under). For example, if your app is deployed at http://localhost:8080/MyWebApp/, the correct URL for the servlet would be http://localhost:8080/MyWebApp/GetUserServlet—not just GetUserServlet.
Fix:
Add the context path dynamically to your AJAX request to avoid hardcoding it:
- First, add this script block in your
index.jsp(before loadingapp-ajax.js):<script type="text/javascript"> // Get the app's context path dynamically var appContextPath = "${pageContext.request.contextPath}"; </script> - Then update your
app-ajax.jsto use this variable:$(document).ready(function() { $('#userName').blur(function() { $.ajax({ url : appContextPath + '/GetUserServlet', // Add context path here data : { userName : $('#userName').val() }, success : function(responseText) { $('#ajaxGetUserServletResponse').text(responseText); } }); }); });
2. Verify Servlet Deployment & Annotation
Make sure your servlet is properly recognized by the server:
- Check Servlet 3.0+ Compatibility: The
@WebServletannotation requires your project to use Servlet 3.0 or newer. Confirm yourweb.xmlhas the correct version declaration:<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> - Ensure Servlet is Compiled & Deployed: In JDeveloper, right-click your project →
Deploy→ check the deployment logs for errors. You can also open the generated WAR file (found in your project's deployment directory) and verifyWEB-INF/classes/GetUserServlet.classexists.
3. Validate the Request with Browser Dev Tools
Use your browser's developer tools to see the exact request being sent:
- Press
F12to open Dev Tools. - Go to the
Networktab. - Trigger the blur event on your input.
- Look for the
GetUserServletrequest—check its full URL. If it's missing your app's context root, that's the issue (fix covered in step 1).
4. Confirm Server & Project Deployment Status
- Check JDeveloper's server console for deployment success messages (e.g., "Deployment of application module [YourProjectName] completed successfully").
- If the server failed to start or the project didn't deploy, resolve those issues first (e.g., port conflicts, missing dependencies).
Once you've applied these checks, test your input again—your AJAX call should reach the servlet without the 404 error.
内容的提问来源于stack exchange,提问作者proera

