好些日子沒有編碼了,今天突然想搞個servlet來練練手,卻一直提示:HTTP method POST is not supported by this URL
百度了下,找到了解決方案,現在把代碼和方案貼出來,一方面留給碰到此問題的網友,另一方面,爲自己以後那天再練手的時候,有個參考。
言歸正傳,上代碼:
web.xml
<display-name>WebWorkProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class> org.weib.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
index.html:
<html>
<head>
</head>
<body>
<form action="/WebWorkProject/login.do" method="post">
<table>
<tr>
<td colspan ="2"></td>
</tr>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td >密 碼:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交" name="sub_mit"></td>
<td><input type="reset" value="重置" name="re_set"></td>
</tr>
</table>
</form>
</body>
</html>
content.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
welcoming to come back ! <% request.getAttribute("username"); %>
</body>
</html>
LoginServlet :
package org.weib.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
/* ServletRequest request = (ServletRequest)req;
ServletResponse response = (ServletResponse)resp;*/
req.setCharacterEncoding("gb2312"); //設置編碼,否則從前臺過來的中文是亂碼
String name = req.getParameter("username");
String pass = req.getParameter("password");
/*
* 兩種方式
* 方式1
* resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8"); //設置編碼,否則傳到前臺的中文是亂碼
PrintWriter out = resp.getWriter();
out.println("<HTML>");
out.println("<BODY>");
out.println("welcome to coming :"+name);
out.println("這是Servlet的例子");
out.println("</BODY>");
out.println("</HTML>");
out.flush();*/
req.setAttribute("username", name);
/**
* 方式2
*/
resp.sendRedirect("content.jsp");
}
}
HTTP method POST is not supported by this URL 解決方案:註釋掉doPost()中的super方法。
HTTP method POST is not supported by this URL錯誤的解決方案(參考文檔:http://hi.baidu.com/winlei/item/cdd3000da3224eca9057181b)
最近一段時間一直從事SWING界面和EJB方面的開發,今天偶爾重新溫習了一下BS方面的開發,結果僅僅寫了一個簡單的servlet,提交之後訪問servlet之後總是會報HTTP method POST is not supported by this URL錯誤, 感到很疑惑,之前在BS開發過程中重來也沒有遇到過這類錯誤,百度一下之後也沒有解決問題,最後發現竟然是在重寫的doPost多加了一個語句super.doPost導致的,無奈之下只好下了tomcat的原代碼,打開javax.servlet.http.HttpServlet.java文件之後才發現超類的doPost方法如下:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
也就是說不管你的http是不是1.1的,都是用resp.sendError方法返回一個http.method_post_not_supported的錯誤信息給前臺界面,把我害的好苦,強烈譴責tomcat代碼的註釋,應該註明不要加super.doPost,不能總讓我們看你們的源代碼吧.呵呵 .