doGet和doPost的差别



get和post是http协议的两种方法,另外还有head,
delete等 



这两种方法有本质的差别,get仅仅有一个流,參数附加在url后。大小个数有严格限制且仅仅能是字符串。

post的參数是通过另外的流传递的。不通过url。所以能够非常大,也能够传递二进制数据,如文件的上传。

在servlet开发中,以doGet()和doPost()分别处理get和post方法。

首先推断请求时是get还是post,假设是get就调用doGet(), 假设是post就调用doPost()。都会运行这种方法。 



1.doGet

GET 调用用于获取server信息,并将其做为响应返回给client。当经由Web浏览器或通过HTML、JSP直接訪问Servlet的URL时,一般用GET调用。 GET调用在URL里显示正传送给SERVLET的数据。这在系统的安全方面可能带来一些问题,比方用户登录,表单里的username和password须要发送到server端, 若使用Get调用,就会在浏览器的URL中显示username和password。

例:

jsp页代码:

<form action="/doGet_servlet" name=”form1” method="get">

………

<input type="text" name="username">

………

</form>

servlet代码:

public class doGet_servlet extends HttpServlet {

  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {

      request.setCaracterEncoding(“UTF-8”);//汉字转码

      String username = request.getParameter("username");

request.setAttribute("username",username);

request.getRequestDispatcher("/out.jsp").forward(request, response);//跳转到out.jsp页面

}

}

out.jsp页面

<html>

``````

<%=request.getAttribute("username")%>//在页面上输出username的信息

</html>

这样提交表单后,參数会自己主动加入到浏览器地址栏中,带来安全性问题。



2.doPost

它用于client把数据传送到server端,也会有副作用。但优点是能够隐藏传送给server的不论什么数据。Post适合发送大量的数据。

例:

jsp页代码:

<form action="/doPostt_servlet" name=”form2” method="post">

………

<textarea name="name2" cols="50" rows="10"></textarea>

………

</form>

servlet代码:

public class doPostt_servlet extends HttpServlet {

  public void doPost(HttpServletRequest request,HttpServletResponse esponse) throws IOException,ServletException {

      request.setCaracterEncoding(“UTF-8”);//汉字转码

      PrintWriter out = response.getWriter();

      out.println("The Parameter are :"+request.getParameter("name2"));

  }

}

最好用上面在doGet中提到的输出方式进行输出

3.能够把方法写在doGet()方法中,在doPost()方法中调用运行,这样,不管你提交的是post还是get方法都能够运行

比如:

jsp页代码:

<form action="/servlet" name=”form” method="post">

………

<input type="text" name="name1">

………

</form>

servlet代码:

public class servlet extends HttpServlet {

  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {

      request.setCaracterEncoding(“UTF-8”);//汉字转码

      PrintWriter out = response.getWriter();

      out.println("The Parameter are :"+request.getParameter("name1"));

  }



  public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {

      this.goGet(request,response);//调用doGet()方法

  }

}

另外,HttpServlet处理client请求方式还有doPut、doDelete、doTrace、doHead、doOptions。但使用的比較少。

android doGet和doPost的更多相关文章

  1. servlet中service() 和doGet() 、doPost() 学习笔记

    Sevlet接口定义如下: 与Sevlet接口相关的结构图: service() 方法是 Servlet 的核心.每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要 ...

  2. 【Servlet】doGet()与doPost()的区别

    doGet与doPost的区别 .Servlet接口只定义了一个服务方法--service .当发出客户端请求时,调用service方法并传递一个请求和响应对象 .使用时经常在doPost()中调用d ...

  3. doGet和doPost的区别

    1.doGet和doPost的区别,在什么时候调用,为什么有时doPost中套用doGet 2.提交的form     method=Post就执行DOPOST,否则执行GOGET 套用是不管meth ...

  4. doGet与doPost的区别

    转自:http://blog.csdn.net/luoweifu/article/details/7865243   目录(?)[-] 不同点一 不同点二 输入表单inputhtml Serlvlet ...

  5. SERVLET中的doGet与doPost两个方法之间的区别

    get和post是http协议的两种方法,另外还有head, delete等 这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串.post的参数是通过另外的 ...

  6. Servlet--HttpServlet实现doGet和doPost请求的原理

    转:https://blog.csdn.net/m0_38039437/article/details/75264012 一.HttpServlet简介 1.HttpServlet是GenericSe ...

  7. servlet中doGet()和doPost()的区别

    1.生成方式 get方法有四种: ①直接在URL地址栏中输入URL ②网页中的超链接 ③form中method为get ④form中method为空时,默认是get提交 post只知道有一种:form ...

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

    Servlet的doGet与doPost方法的区别与使用 2016年07月07日 13:05:13 阅读数:10222 一,区别 在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个 ...

  9. 1.2(学习笔记)Servlet基础(doGet、doPost、生命周期、页面跳转)

    一.doGet()与doPost() 我们在TestServlet类中重写doGet().doPost().service(). import javax.servlet.ServletExcepti ...

随机推荐

  1. MVC客户端验证的小示例

    MVC客户端验证的小示例 配置客户端验证的可用性: <configuration> <appSettings>  <add key="ClientValidat ...

  2. Java基础08 继承

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 继承(inheritance)是面向对象的重要概念.继承是除组合(composit ...

  3. github上的QT源码,必要的时候还是应该看一下,仅凭猜测很容易出错

    QCoreApplication::processEvents 他处理的时候拿的是current不是qAppqApp的话,才是和主线程密切相关的 一直觉得QT源码复杂,有点怕,所以没怎么看 我也看不懂 ...

  4. log4net使用流程

    前面大致介绍了一下log4net的概述和结构.既然都清楚了,下面我来介绍一下如何使用log4net. 使用流程 1.这里所说的使用流程就是使用log4net.dll,首先要根据你的平台来找出对应的版本 ...

  5. [免费活动通知]RAD Studio XE8 技术研讨会(上海、成都)

     活动类型:免费研讨会 报名链接: http://forms.embarcadero.com/AP15Q3CNRADStudioDeepDiveSeminar 上海 2015 年 8 月 13 日 ...

  6. 第12届北师大校赛热身赛第二场 B起床的烦恼

    题目链接:http://www.bnuoj.com/bnuoj/contest_show.php? cid=3570#problem/43572 题目大意: Nono从一開始数数,他每数一个数时会计算 ...

  7. netduino第一步,环境配置

    在netduino.com的官网介绍下,我很快就入门,现在的最新netduino的版本是4.3,但4.3是运行在win8下的,在codeplex.net上有,大部分人还使用的是win7,因此我现在采用 ...

  8. abap优化工具事务代码: ST05

  9. Stripe

    Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...

  10. 自己写一个jqery的拖拽插件

    说实话,jQuery比原生的js好用多了,本来想用原生写的,也写出来的,仅仅是,感觉不像插件,所以用jQuery实现了一版. 实现的功能:能够指定拖拽的边界,在拖拽过程中,能够触发几个自己定义事件 先 ...