java - 对 servlet 的 POST 请求返回 GET 方法的结果
问题描述
背景:
我正在编写一个 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
-> 索引.jsp
GET localhost:8080/SelfservicePortal/portal
-> 404 未找到
POST localhost:8080/SelfservicePortal
-> index.jsp [这应该是 html_output]
POST localhost:8080/SelfservicePortal/portal
-> 404 未找到
如何让 POST 请求正常工作,为什么对 url-mapping 模式的请求返回 404?
解决方案
我认为您忘记在您的 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());
}
}
推荐阅读
- mercurial - 如何摆脱这个 Mercurial 警告?(主机安全/主机指纹)
- python-3.x - sklearn 的 log_loss 给出了 nan,而 tensorflow.losses.log_loss 有效
- android - 如何修复谷歌播放服务错误
- android - 添加第二个 Y 轴标题,标记线 + 添加线标题
- javascript - 使用外部组件库时的不变违规
- python - 来自导入文件的 Python 多处理
- php - Laravel 中的 try-catch 样板
- ruby - ruby request.inspect 显示没有正文
- .net - 如何插入一瞥时间线
- c# - 需要帮助将 c# 中的 opencv 转换为 vb.net