servlet 示例

好些日子没有编码了,今天突然想搞个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,不能总让我们看你们的源代码吧.呵呵 .




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值