×

setattribute用法jsp

setattribute用法jsp(JSP中setattribute与setParameter的区别)

admin admin 发表于2023-04-06 22:18:19 浏览55 评论0

抢沙发发表评论

本文目录

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返回字符串
request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取

JSP页面中能不能使用request.setAttribute()


可以使用的
request.setAttribute(“result“,username);
在request对象中加入名为result的属性并附值为username,因为request对象是可以更改的,你可以在同一个请求中象这样访问这个属性。
虽然类似session,但与session是有所区别的,request.setAttribute设置的属性只能在当前request只使用,比如你在Action中设置result属性,需要到jsp页面中读取:
request.setAttribute(“result“,username);
requests.getRequestDispatcher(“result.jsp“).forward(request, response);
jsp页面获取该值:
request.getAttribute( “result“);
因为一同将当前action的request与response对象都发送过来,相当于直接操作自身页面。

关于jsp中setattribute,getattribute的用法,该怎么处理


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

jsp中request.setAttribute不起作用吗


可以使用的
request.setAttribute(“result“,username);
在request对象中加入名为result的属性并附值为username,因为request对象是可以更改的,你可以在同一个请求中象这样访问这个属性。
虽然类似session,但与session是有所区别的,request.setAttribute设置的属性只能在当前request只使用,比如你在Action中设置result属性,需要到jsp页面中读取:
request.setAttribute(“result“,username);
requests.getRequestDispatcher(“result.jsp“).forward(request, response);
jsp页面获取该值:
request.getAttribute( “result“);
因为一同将当前action的request与response对象都发送过来,相当于直接操作自身页面。

怎么在jsp的body里面写request.setAttribute


在body通过《%%》执行request.setAttribute方法

《body》
《div》111《/div》
《%request.setAttribute(“mes“, “123“); %》  调用setAttribute方法
《/body》

补充:

《%%》:《%程序代码段%》,这里面的文本不是普通直接输出到客户端的文本,而是需要服务器来解释的,程序代码段可以是java的代码


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中的request对象的setAttribute()方法有什么用,例如 request.setAttribute(“name“,“Apache Tomcat“)


把request想象成一个篮子,可以装两种东西一种是Parameter,是由客户端提交的。
post和get方式提交的都在是Parameter,只能是String。
attribute就是你可以往request里放的东西(对象的引用)。
在服务器端servlet或jsp之间forward请求的时候用得到。

关于JSP的 session.setAttribute()方法是做什么用的|


比如你在aa.jsp中 session.setAttribute(“session1“,“这是保存的内容“);
那你在bb.jsp中可以String str=(String)session.getAttrinbute(“session1“);//那么str就等于“这是保存的内容“

JSP中setAttribute方法的问题


第一个 第二个值可以随便填的啊,你是不是直接把类的类型给当参数了,应该用类的对象
第二个 复写的时候生成的就是 mapping form request response啊,你是不是操作错了
第三个 window-》preferences-》myEclipse-》file and editors-》jsp里面有encoding改了就行了.