Http Get & Post總結

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區別】,有不足之處請指正。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章