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

Oracle JDeveloper中Servlet JSP AJAX Get请求404错误求助

Fixing the 404 Not Found Error for jQuery AJAX to Servlet Call

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 loading app-ajax.js):
    <script type="text/javascript">
        // Get the app's context path dynamically
        var appContextPath = "${pageContext.request.contextPath}";
    </script>
    
  • Then update your app-ajax.js to 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 @WebServlet annotation requires your project to use Servlet 3.0 or newer. Confirm your web.xml has 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 verify WEB-INF/classes/GetUserServlet.class exists.

3. Validate the Request with Browser Dev Tools

Use your browser's developer tools to see the exact request being sent:

  1. Press F12 to open Dev Tools.
  2. Go to the Network tab.
  3. Trigger the blur event on your input.
  4. Look for the GetUserServlet request—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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 21:58:14