背景:
我正在编写一个servlet应用程序,并且希望servlet处理GET和POST请求。将该servlet内置到WAR文件中,并使用我的开发计算机(localhost:8080)上的Tomcat服务器在本地托管进行测试。
对Servlet的GET请求工作正常(返回index.jsp页面),但是,将POST张贴到相同的URL不会运行doPost()方法中的代码(还会返回index.jsp页面)。
这是我的servlet类代码:
public class SelfserviceServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/index.jsp");
view.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ip_address = request.getParameter("someparam");
String port = request.getParameter("someotherparam");
// Do some operation here to produce html_output
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(html_output.toString());
}
}
这是我的web.xml文件:
<servlet>
<servlet-name>SelfservicePortal</servlet-name>
<servlet-class>com.somename.module.SelfserviceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SelfservicePortal</servlet-name>
<url-pattern>/portal</url-pattern>
</servlet-mapping>
编辑:这是index.jsp文件:
<html>
<head>
<script>
function sendPOST(){
// First URL try
$.ajax({
url: "/SelfservicePortal",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: "someparam=" + encodeURIComponent(someparam) + "&someotherparam=" + encodeURIComponent(someotherparam),
success: function(data) {
alert(data);
},
error: function() {
alert("Error");
}
});
// Second URL try
$.ajax({
url: "/SelfservicePortal/portal/",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: "someparam=" + encodeURIComponent(someparam) + "&someotherparam=" + encodeURIComponent(someotherparam),
success: function(data) {
alert(data);
},
error: function() {
alert("Error");
}
});
}
</script>
<body>
<button type="button" onclick="sendPOST();">Send POST</button>
</body>
</html>
这些是我的结果:
GET localhost:8080/SelfservicePortal
-> index.jsp
GET localhost:8080/SelfservicePortal/portal
->找不到404
POST localhost:8080/SelfservicePortal
-> index.jsp [应该是html_output]
POST localhost:8080/SelfservicePortal/portal
->找不到404
我如何使POST请求正常工作,为什么对url映射模式的请求返回404?
答案 0 :(得分:1)
我认为您忘记在Servlet中添加注释,请尝试使用以下代码,看看它是否有效。如果要使用表单数据而不是url编码的数据,还可以添加@MultipartConfig。
@WebServlet("/portal")
@MultipartConfig
public class SelfserviceServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/index.jsp");
view.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ip_address = request.getParameter("someparam");
String port = request.getParameter("someotherparam");
// Do some operation here to produce html_output
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(html_output.toString());
}
}