首页 新闻 会员 周边

HttpServletRequest实例的getAttribute(String name)方法到底获取的东西是什么?

0
悬赏园豆:10 [已解决问题] 解决于 2016-11-08 08:30

HttpServletRequest实例的getAttribute(String name)方法到底获取的东西是什么?

丶theDawn的主页 丶theDawn | 初学一级 | 园豆:194
提问于:2016-11-07 11:15
< >
分享
最佳答案
2

getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型,getAttribute() 方法通过名称获取属性的值。

收获园豆:10
鸣人幻 | 菜鸟二级 |园豆:219 | 2016-11-07 16:52
其他回答(2)
0

name对应的对象吧,你想问的是什么?

sun_qian | 园豆:156 (初学一级) | 2016-11-07 11:21

就是这个name对应的对象是个什么东西呢?知道它的返回值是一个Object,这个返回的Object在Web应用中表示的是什么东西?

支持(0) 反对(0) 丶theDawn | 园豆:194 (初学一级) | 2016-11-07 11:26

@丶三年以后的架构师: 地址栏参数,form表单中的数据项,上传的文件对象等吧,或者springmvc会做自动的数据绑定对象。大致就这些,使用的话,不用深究,原理我就给你讲清楚了,我也只是用用。

支持(0) 反对(0) sun_qian | 园豆:156 (初学一级) | 2016-11-07 11:29

@sun_qian: 恩。。。我想想。。。做个测验。。。

支持(0) 反对(0) 丶theDawn | 园豆:194 (初学一级) | 2016-11-07 11:56

@sun_qian: 写了一个简单的JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>有关getAttribute的测试</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/TestServlet" method="post">
    <input type="text" name="username" >
    <input type="submit" value="提交">
</form>
</body>
</html>

然后写了一个Servlet比较了下request的getAttribute()和getParameter()

@WebServlet(name="TestServlet",urlPatterns={"/TestServlet"})
public class TestServlet extends HttpServlet{
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setAttribute("username", "123456789");
        System.out.println(request.getAttribute("username"));
        System.out.println(request.getParameter("username"));
    }
}

原来这两个东西不是一回事,而且不相互影响,那么应该getAttribute获取的是由setAttribute设定的?只是范围是request吧?

支持(0) 反对(0) 丶theDawn | 园豆:194 (初学一级) | 2016-11-07 12:23

@丶三年以后的架构师:I`m sorry, 我说的竟然是request.getParamter,这个主要获取地址栏参数,form表单中的数据项,主要是客户端传来的数据,且为字符串。而getAttribute()确实是在服务器端,是由setAttribute设定的,范围为request。

支持(0) 反对(0) sun_qian | 园豆:156 (初学一级) | 2016-11-07 13:59
0

是在这个请求处理过程中, 通过setAttribute设置进去的一些值

 

加我微信214710841, 在线交流

小彬 | 园豆:947 (小虾三级) | 2016-11-07 14:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册