×

jsp中setattribute

jsp中setattribute(jsp中getAttribute和setAttribute的详细解释)

admin admin 发表于2024-07-13 00:59:02 浏览13 评论0

抢沙发发表评论

大家好,关于jsp中setattribute很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于jsp中getAttribute和setAttribute的详细解释的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

本文目录

jsp中getAttribute和setAttribute的详细解释

request对象也可以,只是只有下一个响应的页面才能得到,是一次性的,能用request尽量少用sessionsession是占用服务器内存空间的setAttribute("给这个变量取的名字,以后通过这个名字getAttribute,比如hello",变量);getAttribute("hello")就得到变量了变量可以是任意的类型,比如String,int,或自己定义的类对象,所以get的时候就涉及到类型转换的问题了这时强转一下就行了,如果set的时候放的是String类型的变量,则String s=(String)session.getAttribute("hello");

jsp网页中application.getAttribute与setAttribute的用法问题

application:全局作用范围,整个应用程序共享,就是在部署文件中的同一个webApp共享,生命周期为:应用程序启动到停止。 服务器启动后就产生了这个application对象,当客户再所访问的网站的各个页面之间浏览时,这个application对象都是同一个,直到服务 器关闭。但是与session不同的是,所有客户的application对象都是同一个,即所有客户共享这个内置的application对象。 《%@page contentType="text/html;charset=GB2312"%》 《html》 《head》 《/head》 《body》 《center》 《font size="5"》application对象的使用《/font》 《hr/》 《% Object o = null; String strNum = (String) application.getAttribute("Num"); //与session相同 int Num = 0; if (strNum != null) Num = Integer.parseInt(strNum) + 1; application.setAttribute("Num", String.valueOf(Num)); %》 application对象中的 《font color="blue"》Num《/font》 变量值为: 《font color="red"》《%=Num %》 《/font》 《br/》 《/center》 《/body》 《/html》

jsp 中setAttribute(String , Object)

setAttribute(String , Object)一般是在repuest和session中使用的小程序不写了简单给你2行代码解释一下你的2歌问题好了那session来说session.setAttribute("NAME", "bill");这时在你的session中表示是"NAME"对应的是billsession.setAttribute("NAME", "andy");这时在你的session中表示是"NAME"对应的是andy可以理解name是变量名称 后面的OBJECT是值取值也是一样 用getAttribute(String);返回的是一个OBJECT比如String name=(String)session.getAttribute("NAME");这时name的值是andy而不是第一次赋值的bill了.明白否??创建一个就是调用一次改变String就好了..他会自动在内存中创建一个新的地方表示就是定义的Stirng值就是OBJECT

JSP中setattribute与setParameter的区别

HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:《a href="authenticate.jsp?username=weiqin"》authenticate.jsp 《/a》或者:《form name="form1" method="post" action="authenticate.jsp"》请输入用户姓名:《input type="text" name="username"》《input type="submit" name="Submit" value="提交"》《/form》在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:《% String username=request.getParameter("username"); %》(3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:《%String username=request.getParameter("username");request.setAttribute("username",username);%》《jsp:forward page="hello.jsp" /》在hello.jsp中通过getAttribute()方法获得用户名字:《% String username=(String)request.getAttribute("username"); %》Hello: 《%=username %》从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,, request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段,这个的确是正解.getAttribute是返回对象,getParameter返回字符串***隐藏网址***

jsp中用request.setAttribute()后,在action中为什么取不到值

不能这么请求。因为页面上的request 和使用form提交的request 请求 不是一个对象。测试如下:首先在jsp页面中打印出来request对象《%request.setAttribute("test","test");System.out.println("request="+request+"返回页面对象");//这里打印出页面request对象%》部分action代码如下:HttpServletRequest request = ServletActionContext.getRequest();System.out.println("request="+request+"action里的request对象");String test = (String) request.getAttribute("test");System.out.println("test="+test);经过测试 输出结果如下:request=org.apache.struts2.dispatcher.StrutsRequestWrapper@199f0e5返回页面对象request=org.apache.struts2.dispatcher.StrutsRequestWrapper@19665e1action里的request对象test=nullrequest=org.apache.struts2.dispatcher.StrutsRequestWrapper@19665e1返回页面对象说明:输出结果第一行 是第一次加载请求页面时,调用当前页面的request对象(其实我们打开请求页面时产生的请求对象)第二行 是我用form 提交然后在action里获取的request对象,可以看到两个request对象的类型是一样的 但是值却不一样,这正好说明了 我们每次请求 都会重新产生一个request对象。到这里,第三行就毫无疑问了,既然重新生成了request对象 那当然不会有我们之前set的属性咯。第四行很关键,我们看到了那个action里的request对象 返回到了页面上,他们是同一个类型,同一个对象。这正好说明了 当我们的响应模式为dispatcher时 request参数也会返回到页面,因为request对象没有变。希望对你有所帮助!

jsp中关于setAttribute方法的问题

request和session都是jsp的内置对象,java的api没有整理jsp内置对象的资料,但是没有整理不代表没有,在百度上搜一下吧,有很多介绍

以上就是我们为大家找到的有关“jsp中setattribute(jsp中getAttribute和setAttribute的详细解释)”的所有内容了,希望可以帮助到你。如果对我们网站的其他内容感兴趣请持续关注本站。