我有一个名为LoggingServlet的日志记录servlet,它覆盖了doGet和doPost方法,如下所示:
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
}
我的web.xml看起来像这样:
<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>
这是一个实现一些JavaScript的jsp片段:
<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>
我还有另一个通过Post请求调用LoggingServlet的jsp。 现在我的问题是:
这怎么可能?
答案 0 :(得分:5)
servlet映射到
/LoggingServlet/*
您的网址是
/LoggingServlet?bookie_id...
因此,在要映射到servlet的查询字符串之前,URL缺少尾部斜杠。它需要
/LoggingServlet/?bookie_id...
这也适用于webapp是root webapp的情况。如果不是,您还需要预先添加应用程序的上下文路径:
/MyWebApp/LoggingServlet/?bookie_id...