大部分框架在对Http请求封装的基础原理为:html
截取URL请求java
在web.xml中配置一个Servlet或者Filter,配置须要截取的url,好比struts1是在web.xml文件中配置截取.do的全部请求。web
解析参数,从新封装 app
经过继承HttpServlet,加载并启动全部用户自定义服务,加载全部用户自定义业务处理Action配置框架
经过HttpServlet中的init()方法中的ServletConfig获取上下文ServletContextjsp
经过HttpServlet中的service()方法中的ServletRequest和ServletResponse入参,能够控制本次链接的输入输出ide
HttpServletRequest的request.getParameterNames()方法能够获得全部的参数post
获取请求URI并进行转向测试
HttpServletRequest的hRequest.getRequestURI()和 hRequest.getContextPath();方法能够获取本次请求的URI,根据URI能够将这次请求转向一个固定的业务处理行为ui
最后,作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="上传"> <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>