Servlet的doGet与doPost方法的区别与使用

本文详细对比了GET与POST两种HTTP请求方法的区别,并通过实例演示了它们在Java Web开发中的具体应用。

一,区别

在使用表单提交数据到服务器的时候有两张方式可共选择,一个是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

 

[java] view plain copy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>登录</title>  
  8. </head>  
  9. <body>  
  10.       <h3>登录</h3>  
  11.       <hr>  
  12.       <form action="LoginServlet" method="post">  
  13.                 用户名:<input type="text" name="username"/><br>  
  14.                  密码:<input type="password" name="password"/><br>  
  15.               <input type="submit" />  
  16.       </form>  
  17.   
  18. </body>  
  19. </html>  

LoginServlet:

 

[java] view plain copy

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.         // TODO Auto-generated method stub  
  3.         request.setCharacterEncoding("UTF-8");  
  4.         response.setCharacterEncoding("UTF-8");  
  5.         //向服务器发送请求获取到参数  
  6.         String username=request.getParameter("username");  
  7.         String password=request.getParameter("password");  
  8.         System.out.println(username+"--"+password);  
  9.           
  10.         response.setHeader("Content-Type""text/html; charset=UTF-8");  
  11.         Writer out=response.getWriter();  
  12.         out.write("用户名:"+username);  
  13.         out.write("密码:"+password);  
  14.         out.flush();  
  15.         out.close();      
  16.     }  

效果图:

这就是Post方式提交和doPost方法使用的效果,是不是更安全呢~~~

2.get方式--doGet方法

login.jsp:

 

[java] view plain copy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>登录</title>  
  8. </head>  
  9. <body>  
  10.       <h3>登录</h3>  
  11.       <hr>  
  12.       <form action="LoginServlet" method="get">  
  13.                 用户名:<input type="text" name="username"/><br>  
  14.                  密码:<input type="password" name="password"/><br>  
  15.               <input type="submit" />  
  16.       </form>  
  17.   
  18. </body>  
  19. </html>  

LoginServlet:

 

[java] view plain copy

  1. @Override  
  2.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  3.         // TODO Auto-generated method stub  
  4.         request.setCharacterEncoding("UTF-8");  
  5.         response.setCharacterEncoding("UTF-8");  
  6.         //向服务器发送请求获取到参数  
  7.         String username=request.getParameter("username");  
  8.         String password=request.getParameter("password");  
  9.         System.out.println(username+"--"+password);  
  10.           
  11.         response.setHeader("Content-Type""text/html; charset=UTF-8");  
  12.         Writer out=response.getWriter();  
  13.         out.write("用户名:"+username);  
  14.         out.write("密码:"+password);  
  15.         out.flush();  
  16.         out.close();  
  17.     }  

效果图:

 

3.也可以post方式提交,然后在doGet方式写逻辑代码,不过要在doPost方法中调用doGet方法,同样get方式也是一样的道理~~~

另外,除了doGet和doPost方法之外,还有doPutdoDeletedoTracedoHeaddoOptions,但使用的比较少。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值