博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
doget和dopost的区别
阅读量:4159 次
发布时间:2019-05-26

本文共 2297 字,大约阅读时间需要 7 分钟。

doget和dopost的区别

get和post是http协议的两种方法
这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串。post的参数是通过另外的流传递的,不通过url,所以可以很大,也可以传递二进制数据,如文件的上传。 
在servlet开发中,以doGet()和doPost()分别处理get和post方法。 
首先判断请求时是get还是post,如果是get就调用doGet(), 如果是post就调用doPost()。都会执行这个方法。 
1.doGet
GET 调用用于获取服务器信息,并将其做为响应返回给客户端。当经由Web浏览器或通过HTML、JSP直接访问Servlet的URL时,一般用GET调用。 GET调用在URL里显示正传送给SERVLET的数据,这在系统的安全方面可能带来一些问题,比如用户登录,表单里的用户名和密码需要发送到服务器端, 若使用Get调用,就会在浏览器的URL中显示用户名和密码。
例:
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
它用于客户端把数据传送到服务器端,也会有副作用。但好处是可以隐藏传送给服务器的任何数据。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()方法
  }
}
 

分享: 

转载地址:http://aejxi.baihongyu.com/

你可能感兴趣的文章
unix高级编程之-命令行参数(实践一)
查看>>
无线网络加密方式对比 .
查看>>
linux中cat命令使用详解
查看>>
Static 作用详述
查看>>
透析ICMP协议(三): 牛刀初试之一 应用篇ping(ICMP.dll)
查看>>
透析ICMP协议(四): 牛刀初试之二 应用篇ping(RAW Socket)
查看>>
再次写给我们这些浮躁的程序员
查看>>
Linux下重要日志文件及查看方式(1)
查看>>
Linux下重要日志文件及查看方式(2)
查看>>
Ubuntu系统root用户密码找回方法
查看>>
Linux驱动程序中比较重要的宏
查看>>
芯片驱动问题定位思路总结之一单板重启的问题
查看>>
S3C2440看门狗定时器
查看>>
LDD3源码分析之llseek分析
查看>>
linux read 用法
查看>>
LDD3源码分析之llseek分析(二)
查看>>
printk及控制台的日志级别
查看>>
Linux驱动加载实例
查看>>
详解数据库设计中的三大范式理论
查看>>
JDBCUtils工具类
查看>>