Servlet的doGet与doPost方法的区别与使用
2016-07-07 13:05
543 查看
一,区别
在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个是get。可在<form>中的method属性中指定提交的方式。如:<form action="inputForm"method="get">,如果不指定method属性,则会默认该属性为”get”方式。Get和post都能够提交数据,那么他们有什么不同呢?
不同点一:
通过get方式提交的数据有大小的限制,通常在1024字节左右。也就是说如果提交的数据很大,用get方法就可需要小心;而post方式没有数据大小的限制,理论上传送多少数据都可以。
不同点二:
通过get传递数据,实际上是将传递的数据按照”key,value”的方式跟在URL的后面来达到传送的目的的;而post传递数据是通过http请求的附件进行的,在URL中并没有明文显示。
不同点三:
通过Get方式提交的数据安全性不高,而Post方式的更加安全~
二,使用
下面举个例子说明:1.post提交--doPost方法
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 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=UTF-8"> <title>登录</title> </head> <body> <h3>登录</h3> <hr> <form action="LoginServlet" method="post"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" /> </form> </body> </html>LoginServlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //向服务器发送请求获取到参数 String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username+"--"+password); response.setHeader("Content-Type", "text/html; charset=UTF-8"); Writer out=response.getWriter(); out.write("用户名:"+username); out.write("密码:"+password); out.flush(); out.close(); }效果图:
这就是Post方式提交和doPost方法使用的效果,是不是更安全呢~~~
2.get方式--doGet方法
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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=UTF-8"> <title>登录</title> </head> <body> <h3>登录</h3> <hr> <form action="LoginServlet" method="get"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" /> </form> </body> </html>LoginServlet:
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //向服务器发送请求获取到参数 String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username+"--"+password); response.setHeader("Content-Type", "text/html; charset=UTF-8"); Writer out=response.getWriter(); out.write("用户名:"+username); out.write("密码:"+password); out.flush(); out.close(); }效果图:
看这个效果图是不是觉得用户名和密码都暴露了呢~~这样很不安全~
3.也可以post方式提交,然后在doGet方式写逻辑代码,不过要在doPost方法中调用doGet方法,同样get方式也是一样的道理~~~
另外,除了doGet和doPost方法之外,还有doPut、doDelete、doTrace、doHead、doOptions,但使用的比较少。
相关文章推荐
- 小心服务器内存居高不下的元凶--WebAPI服务
- 运维入门
- 利用开源软件打造自己的全功能远程工具
- Linux5.9无人值守安装
- 数据中心和云未来的十二大趋势
- 虚拟化基础架构Windows 2008篇之11-WSUS服务器的安装与配置
- 用vsftp快速搭建ftp服务器
- Linux快速构建apache web服务器
- 服务器监控策略浅谈
- 如何降低服务器采购成本 原理分析
- 建议的服务器分区办法
- 服务器托管六大优势分析
- Erlang实现的一个Web服务器代码实例
- 服务器技术全面解析
- 保护DNS服务器的几点方法小结
- 我国成为全球第二大服务器消费国
- 服务器 安全检查要点[星外提供]
- 服务器应用自动重新启动IIS批处理[原创]_DOS/BAT_脚本之家