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);
	}

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐