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. 字符设备驱动[深入]:linux cdev详解

    linux cdev详解  http://blog.chinaunix.net/uid-24517893-id-161446.html 用cdev_add添加字符设备驱动: //linux2.6中用c ...

  2. CentOS: make menuconfig error: curses.h: No such file or directory

    the problem  when use centos5 to build kernel or busybox step 1. Centos中关于 ncurses.h:no such file or ...

  3. 嵌入式环境:CentOS下添加用户并且让用户获得root权限

    CentOS下添加用户并且让用户获得root权限 http://www.centoscn.com/CentOS/config/2014/0810/3471.html 1.添加用户,首先用adduser ...

  4. WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)

    什么时候会触发WM_PAINT消息消息呢? 以下内容来自大名鼎鼎的<Windows程序设计(第五版)> 大多数Windows程序在WinMain中进入消息循环之前的初始化期间都要呼叫函数U ...

  5. Study notes for Latent Dirichlet Allocation

    1. Topic Models Topic models are based upon the idea that documents are mixtures of topics, where a ...

  6. 安全威胁无孔不入:基于Linux系统的病毒(转)

    虽然在Linux里传播的病毒不多,但也是存在一些.我从一些安全网站搜集了一些资料. 1.病毒名称: Linux.Slapper.Worm 类别: 蠕虫 病毒资料: 感染系统:Linux 不受影响系统: ...

  7. 【linux】 Makefile之make menuconfig /uImage

      欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http: ...

  8. sql: oracle, for update和for update nowait的区别

    1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...

  9. android 实现跳动频谱 DEMO

    package com.terry.AudioFx; import android.app.Activity; import android.content.Context; import andro ...

  10. android ListView和GridView拖拽移位实现代码

    关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:             首先 ...