gpt4 book ai didi

java - 对 servlet 的 POST 请求返回 GET 方法的结果

转载 作者:行者123 更新时间:2023-11-30 05:40:27 25 4
gpt4 key购买 nike

背景:

我正在编写一个 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?

最佳答案

我认为您忘记在 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());
}
}

关于java - 对 servlet 的 POST 请求返回 GET 方法的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55757581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com