HttpServlet:此URL不支持HTTP方法GET或者POST 错误405
第一次敲这个demo时翻了一小下博客,依旧无法解决问题,原先的代码如下
package com.bdqn.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HttpServletTest extends HttpServlet
{
public void doGet(HttpServletResponse response,HttpServletRequest request) throws IOException,ServletException
{
String uName = request.getParameter("username");
if(uName == null)
{
uName = "youke";
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet</title><head>");
out.println("<body>");
out.println("你好"+uName);
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
public void doPost(HttpServletResponse response,HttpServletRequest request) throws IOException,ServletException
{
request.setCharacterEncoding("UTF-8");
doGet(request,response);
}
}
错误出在doGet和doPost的参数列表,只需要将他们两个参数交换一下位置就可以完美解决。
本文分享了在使用Servlet时遇到的一个常见问题:HTTP方法GET和POST参数错误导致的405错误。通过调整doGet和doPost方法的参数顺序,成功解决了Servlet中参数接收的问题。
5028

被折叠的 条评论
为什么被折叠?



