Http Get & Post总结
2012-12-29 12:00
330 查看
Get & Post区别:
参考网上的比较好。以后会附上好的链接…Get & Post实现方式总结:
通过Eclipse下创建ServletDemo,并连接Tomcat Server测试,发现了一些重要的知识点,总结如下:1、无论Get 还是 Post,HttpServlet.java端必须要实现对应的方式。如果没有实现,在指定方式请求时,将会提示异常: HTTP method GET(POST) is not supported by this URL
2、实现方式,一共有两种:
① 直接在浏览器地址栏输入访问路径:如
http://localhost:8080/ServletDemo/servlet/SimpleTestDoGet (只实现了 DoGet 协议)
Note:该方式一定是[Get] method
② 在request.html中使用<form action=" " method=" "> <p>username:<input ...></p> <p>password:<input ...></p> <p>submit:<input ...> </p> </form>表单的方式。
Note:该方式可以是[Get] method,也可以是[Post]
method;只有这两种,通过method=" "来指定。
在点击Submit时,如果是[Post] method,访问的路径是:http://localhost:8080/ServletDemo/servlet/SimpleServiceDo4GetAndPost(通过service实现了
DoGet 和 DoPost 双协议)
如果是[Get] method,访问的路径是:http://localhost:8080/ServletDemo/servlet/SimpleServiceDo4GetAndPost?name=&%E6%8F%90%E4%BA%A4=%E6%8F%90%E4%BA%A4
或 http://localhost:8080/ServletDemo/servlet/SimpleServiceDo4GetAndPost?name=&提交=提交
或 http://localhost:8080/ServletDemo/servlet/SimpleServiceDo4GetAndPost?name=&%E6%8F%90%E4%BA%A4=Submit+Query
不同的client,编码显示的形式不同,但实质都是:表单方式Get request时,是会显示参数的。所以中文或重要参数时,要使用post request。
③如果直接在地址栏输入访问路径:http://localhost:8080/ServletDemo/servlet/SimpleTestDoPost(只实现了 DoPos t协议),则为[ Get ] method,此时仍访问失败,提示HTTP method GET is not
supported by this URL
3、下面是实例源代码,也供参考:
只实现了 DoGet 协议:
public class SimpleTestDoGet extends HttpServlet { // 处理客户端的 GET请求 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("处理 HTTP GET 请求"); } }
只实现了 DoPost协议:
public class SimpleTestDoPost extends HttpServlet { // 处理客户端的 POST 请求 @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("处理 HTTP POST 请求"); } }
通过service实现了 DoGet 和 DoPost 双协议:
public class SimpleServiceDo4GetAndPost extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // super.service(arg0, arg1); response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("处理所有的 HTTP 请求"); // 向客户端输出消息。。。 } }
request.html:
<!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=utf-8"> <title>Post Test</title> </head> <body> <!-- 使用<form>表单,通过post 方法访问 HelloWorldServlet --> <!-- 要访问classes/../HelloWorldServlet.class 应该按照web.xml中“<servlet-mapping><url-pattern>”的value,即 .value --> <form action="./servlet/HelloWorld" method="post"> <!-- 产生name请求参数 --> HelloWorldServlet :<p/> <input type="text" name="name" /> <p/> <input type="submit" name="提交" /> </form> <form action="./servlet/SimpleTestDoPost" method="post"> <!-- 产生name请求参数 --> SimpleTestDoPost :<p/> <input type="text" name="name" /> <p/> <input type="submit" name="提交" /> </form> <form action="./servlet/SimpleServiceDo4GetAndPost" method="post"> <!-- 产生name请求参数 --> SimpleServiceDo4GetAndPost :<p/> <input type="text" name="name" /> <p/> <input type="submit" name="提交" /> </form> <form action="./servlet/SimpleServiceDo4GetAndPost" method="get"> <!-- 产生name请求参数 --> SimpleServiceDo4GetAndPost :<p/> <input type="text" name="name" /> <p/> <input type="submit" name="提交" /> </form> </body> </html>
action的value在web.xml中的servlet配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>SimpleServiceDo4GetAndPost</servlet-name> <!-- servlet-class这个是最重要的,package.*.class --> <servlet-class>com.demo.servlet.test.SimpleServiceDo4GetAndPost</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleServiceDo4GetAndPost</servlet-name> <!-- 说明:url-pattern匹配路径可以自定义,不是一定要按照包名、类名来的 --> <url-pattern>/servlet/SimpleServiceDo4GetAndPost</url-pattern> </servlet-mapping> </web-app>
4、其他思考
初次研究httpservlet,有很多不懂的,关于【Get & Post区别】,有不足之处请指正。
相关文章推荐
- HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结
- HttpGet &&HttpPost方法发送header,params, Content
- post&get请求总结
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- android之HttpPost&HttpGet使用方法介绍
- HttpURLConnection的GET请求POST请求小总结
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP POST GET 本质区别详解(转自C…
- XMLHttpRequest的亲密接触(2.3)——post&get提交的捕获请求方法
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等【重要理解】
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP GET和POST格式解析
- [zt] HTTP 协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP请求方法 GET POST【总结】
- (转)HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP POST&GET
- 谈谈$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')