ijava学习网> 知识分享> 其它分类> request.getAttribute()和request.getParameter()的区别

request.getAttribute()和request.getParameter()的区别

时间: 2018-10-11 22:02:00 标签

request.getAttribute():这个函数需要配和request.setAttribute()使用,即request.getAttribute()获取的是我们自己服务端存到servlet容器中的数据。

request.getParameter():这个函数是用来获取客户端传来的数据用的。

实例:

request.getAttribute()测试:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("name", "HRX");
		System.out.println(request.getAttribute("name"));		
	}

结果如下:

request.getParameter()实例:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String school = request.getParameter("school");
		response.setContentType("application/json; charset=utf-8");
		response.getWriter().append("Served at: ").append(request.getContextPath()+"-------name:"+name+"---school:"+school);
	}

 

 

版权说明| 关于ijava| 合作伙伴| 联系我们| 网站地图| 招贤纳士

Copyright © 2017 www.ijava.com All Rights Reserved 版权所有•ijava学习网 京ICP备14061482号-18         官方QQ:3325669927

ijava学习网提供免费java教程和大量java面试题库,给高级会员提供免费的java培训,同时提共一些java开发项目和java下载,java工程师,java菜鸟们快来哦。记住我们的网站:www.ijava.com