java web学习_浅谈request对象中get和post的差异

作者:jingxian 时间:2022-10-28 00:25:20 

阅读目录(Content)

•1.get与post的区别

•1.1 get方法 jsp中的代码form表单代码

•1.2 action包中servlet的doGet方法中的代码

•2.运行结果

•2.1 输入数据

•2.2 打印出数据

•3.post方法

•4.对比

•4.1 在输出页面按下F12查看

•5.分析

1.get与post的区别

Get和Post方法都是对服务器的请求方式,只是他们传输表单的方式不一样。

下面我们就以传输一个表单的数据为例,来分析get与Post的区别

1.1 get方法  jsp中的代码form表单代码

java web学习_浅谈request对象中get和post的差异

1.2 action包中servlet的doGet方法中的代码


protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 response.setContentType("text/html;charset=gbk");//设置响应正文的mime类型
 request.setCharacterEncoding("gbk");//设置请求的编码格式
 response.setCharacterEncoding("gbk");

String username = request.getParameter("userName");//
 String password = request.getParameter("password");
 String sex = request.getParameter("sex");
 String classes = request.getParameter("class");
 String hobby[] = request.getParameterValues("hobby");// 获取checkbox的数据保存到hobby数组中

PrintWriter out = response.getWriter();

if (hobby != null) {
  for (String x: hobby) {
   out.println("doGet被调用");
   out.println("name:"+username+"password:"+password+"sex"+sex+"classes"+classes);
   out.println("hobby:" + x);  
  }
 }else{
  out.println("此人没爱好!");
 }
}

注意:action包中servlet命名与form表单action的名字相同:

java web学习_浅谈request对象中get和post的差异

2.运行结果  2.1 输入数据

java web学习_浅谈request对象中get和post的差异

  2.2 打印出数据

   java web学习_浅谈request对象中get和post的差异

3.post方法

   只需要将table表单中method改为post:

java web学习_浅谈request对象中get和post的差异

servlet中有这样的一行代码:

java web学习_浅谈request对象中get和post的差异

同样能打印出:只是出现了乱码

java web学习_浅谈request对象中get和post的差异

4.对比   4.1 在输出页面按下F12查看

java web学习_浅谈request对象中get和post的差异

 

   

   java web学习_浅谈request对象中get和post的差异

   post跟get的差异,优先选post

    post的缺点:

    java web学习_浅谈request对象中get和post的差异

  更新web版本可以避免错误

servlet代码分析

java web学习_浅谈request对象中get和post的差异

5.分析

servlet作为控制器是不应该输出内容的,我们应该把要打印的内容放到jsp文件中

标签:java,request,post,get
0
投稿

猜你喜欢

  • Springmvc加ajax实现上传文件并页面局部刷新

    2023-07-31 09:26:47
  • JavaWeb中Servlet的深入理解

    2021-11-04 17:06:58
  • Android Studio实现补间动画

    2022-07-01 11:09:44
  • Android ActionBarActivity设置全屏无标题的方法总结

    2023-01-23 04:49:37
  • 安卓监听屏幕的横竖翻转实现方法

    2023-04-01 07:37:12
  • Java continue break制作简单聊天室程序

    2022-12-20 01:23:01
  • 浅谈MyBatis中@MapKey的妙用

    2023-09-25 10:42:02
  • Android 三级NestedScroll嵌套滚动实践

    2022-11-12 07:45:21
  • Java多态中动态绑定原理解析

    2021-08-01 06:39:23
  • SpringBoot绿叶显示yml和端口问题及解决方法

    2023-12-09 00:29:13
  • java以json格式向后台服务器接口发送请求的实例

    2023-05-01 01:47:08
  • ThreadLocal的set方法原理示例解析

    2023-11-09 15:06:09
  • Android图片色彩变换实现方法

    2022-03-21 07:23:32
  • 学习C#静态函数及变量的一个精典例子与代码

    2021-10-08 18:53:52
  • Android开发之android_gps定位服务简单实现

    2023-07-31 20:02:25
  • SpringMvc web.xml配置实现原理过程解析

    2023-04-14 01:42:27
  • C# WinForm自动更新程序之文件上传操作详解

    2022-09-11 21:42:11
  • 使用Logback设置property参数方式

    2022-07-28 01:06:01
  • 一文快速掌握Spring Cloud Stream

    2023-09-01 23:09:38
  • MyBatis绑定错误提示BindingException:Invalid bound statement (not found)的解决方法

    2021-10-22 03:58:44
  • asp之家 软件编程 m.aspxhome.com