HttpServer服務器,處理請求的基本原理

大部分框架在對Http請求封裝的基礎原理爲:html

  1. 截取URL請求java

    在web.xml中配置一個Servlet或者Filter,配置須要截取的url,好比struts1是在web.xml文件中配置截取.do的全部請求。web

  2. 解析參數,從新封裝 app

    經過繼承HttpServlet,加載並啓動全部用戶自定義服務,加載全部用戶自定義業務處理Action配置框架

    經過HttpServlet中的init()方法中的ServletConfig獲取上下文ServletContextjsp

    經過HttpServlet中的service()方法中的ServletRequest和ServletResponse入參,能夠控制本次鏈接的輸入輸出ide

    HttpServletRequest的request.getParameterNames()方法能夠獲得全部的參數post

  3. 獲取請求URI並進行轉向測試

    HttpServletRequest的hRequest.getRequestURI()和 hRequest.getContextPath();方法能夠獲取本次請求的URI,根據URI能夠將這次請求轉向一個固定的業務處理行爲ui

  4. 最後,作destroy的時候須要考慮釋放全部的資源,特別是用戶啓動的服務

樣本:

get請求和返回:

GET /testupload/file_upload_javabean_form1.jsp?test=11123 HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.2; WOW64; Trident/6.0)
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:1111
Connection: Keep-Alive
Cookie: JSESSIONID=1rc5wzqgboec02hogbehpjs88

HTTP/1.1 200 OK
Date: Tue, 20 May 2014 14:52:35 GMT
Content-Type: text/html; charset=gb2312
Content-Length: 853
Server: Jetty(8.0.4.v20111024)

<!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=gb2312">
  <title>文件上傳</title>
 </head>
 <body>
  <h1 align="center">文件上傳</h1>
  <FORM NAME="form1" METHOD="POST" ACTION="file_upload_javabean_do.jsp">
    <table width="80%" border="0" align="center" >
   <tr> 
     <td height="30" width="50%" align="right">請選擇要上傳的文件:</td>
     <td height="30" width="50%" align="left"><input type="input" name="file" size="30" value="111"></td>
   </tr>  
   <tr> 
     <td colspan="2" height="30" align="center">
     <input type="submit" name="Sub" value="上傳">
     &nbsp;&nbsp;
     <input type="reset" name="Res" value="重選">
    </td>
   </tr>
    </table>
  </FORM>
 </body>
</html>

 post文件上傳請求和返回

POST /testupload/file_upload_javabean_do.jsp HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://127.0.0.1:1111/testupload/file_upload_javabean_form.jsp
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.2; WOW64; Trident/6.0)
Content-Type: multipart/form-data; boundary=---------------------------7de776203f0
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:1111
Content-Length: 280
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=1qnwmkj0juw6w1epld6xbloicx
 
-----------------------------7de776203f0
Content-Disposition: form-data; name="file"; filename="111.txt"
Content-Type: text/plain
12345
-----------------------------7de776203f0
Content-Disposition: form-data; name="Sub"
上傳
-----------------------------7de776203f0--

  
HTTP/1.1 200 OK
Date: Tue, 20 May 2014 14:47:06 GMT
Set-Cookie: JSESSIONID=1rc5wzqgboec02hogbehpjs88;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/plain;charset=gb2312
Content-Length: 335
Server: Jetty(8.0.4.v20111024)
 
<!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=gb2312">
  <title>上傳文件</title>
 </head>
 <body>
 <h1 align=center>文件 <font color=red>111.txt</font> 上傳成功!</h1>
 </body>
</html>

基本post請求

POST /testupload/file_upload_javabean_do1.jsp HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://127.0.0.1:1111/testupload/file_upload_javabean_form1.jsp?test=11123
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.2; WOW64; Trident/6.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:1111
Content-Length: 32
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=1rc5wzqgboec02hogbehpjs88
 
data=4321&Sub=%E4%B8%8A%E4%BC%A0

  
HTTP/1.1 200 OK
Date: Tue, 20 May 2014 15:02:20 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 290
Server: Jetty(8.0.4.v20111024)
 
<!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=gb2312">
  <title>測試頁面</title>
 </head>
 <body>
 
 
 <div>4321</div>
 </body>
</html>
相關文章
相關標籤/搜索