0

I have a logging servlet called LoggingServlet and it overrides the doGet and doPost method as follows:

public class LoggingServlet extends HttpServlet {
    private static final long serialVersionUID = 2L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        ...do stuff here
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        ...do stuff here
    }

And my web.xml looks something like this:

  <servlet>
    <servlet-name>LoggingServlet</servlet-name>
    <servlet-class>com.example.servlets.LoggingServlet</servlet-class>
  </servlet>    

  <servlet-mapping>
    <servlet-name>LoggingServlet</servlet-name>
    <url-pattern>/LoggingServlet/*</url-pattern>
  </servlet-mapping>

And this is a snippet of a jsp that implements some JavaScript:

<script>
    document.getElementById("ad_div").onmousedown = function () {
        xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","/LoggingServlet?bookie_id=<%= request.getParameter("id") %>&app_id=<%= request.getParameter("app_id") %>",true);
        xmlhttp.send();
        return true;
    };
</script>

I also have another jsp that calls the LoggingServlet via a Post request. Now my problem:

  1. The Post request works fine without any problems
  2. However the Get request from the javascript returns a 404 error. I also manually entered the complete url into the webbrowser and I got a 404 error.

How is that possible?

Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

6

The servlet is mapped to

/LoggingServlet/*

Your URL is

/LoggingServlet?bookie_id...

The URL thus lacks a trailing slash before the query string to be mapped to the servlet. It would need to be

/LoggingServlet/?bookie_id...

This would also only work if the webapp is the root webapp. If it isn't, you also need to prepend the context path of the application:

/MyWebApp/LoggingServlet/?bookie_id...
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255