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. boost操作xml 5分钟官方教程

    Five Minute Tutorial This tutorial uses XML. Note that the library is not specifically bound to XML, ...

  2. C++-struct类的新特性当class用

    #include <iostream> #include <iomanip> #include <string> using namespace std; stru ...

  3. 操作引入xml文件的书包(定位到指定节点)

    定位到指定节点:e0.1 <chtml><we>@{_samples/test.xml:HtokID=e0.1}</we></chtml> 上述表达式表 ...

  4. Oracle的序列

    Oracle的序列 序列介绍 序列是Oracle提供的用于产生一系列唯一数字的数据库对象. 使用序列能够实现自己主动产生主键值.序列也能够在很多用户并发环境中使用.为所实用户生成不反复的顺序数字,并且 ...

  5. android multicast 多播(组播)问题

    有谁遇到过同样问题的可以探讨下,或者已经解决问题的,能够指导下我    获取组播锁 private  InetAddress   group; WifiManager  wm=(WifiManager ...

  6. QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?

    源地址:http://my.oschina.net/jinzei/blog/100989?fromerr=DhQJzZQe 相信大家很多和我一样,用多了微软给的便利,用人家的就十分不习惯.于是就琢磨原 ...

  7. Thymeleaf Javascript 取值

    <script th:inline="javascript"> var openid = /*[[${session.wxuser.openId}]]*/ </s ...

  8. [置顶] 单键模式的C++描述

    设计模式-单键(Signelton):其实单键的设计模式说来很简单,说的直白一点就是程序运行过程中保证只有一个实例在运行而已.在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例, ...

  9. 去掉Enter字符(\r)的几个方法

    数据:test.txt: f1:f2:f3:# Shell: #!/bin/bash while read line do echo $line result1=$(echo $line|awk -F ...

  10. 《Java从入门到精通》src9-25

    find . -name *.java |xargs  -i sh -c "echo {};cat {}" > ../all.java[op@TIM src]$ cat al ...