request.getAttribute()和request.getParameter()的区别
request.getAttribute():这个函数需要配和request.setAttribute()使用,即request.getAttribute()获取的是我们自己服务端存到servlet容器中的数据。request.getParameter():这个函数是用来获取客户端传来的数据用的。实例:request.getAttribute()测试:protected void...
·
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);
}
更多推荐
已为社区贡献1条内容
所有评论(0)