20120417

getAttribute getParameter 区别
.
2009-04-02 16:52221人阅读评论(0)收藏举报


getAttribute getParameter 区别2009-02-16 08:26当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getAttribute方法来取得要共享的数据。而MVC中用的就是Web组件之间的转发啊!真是笨,怎么当时没有想到呢?
      下面整理一下getParameter和getAttribute的区别和各自的使用范围。

      (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

       (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

      <a href="authenticate.jsp?username=wolf">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()方法是获取http提交过来的数据。


All we need to do now is to add the jsp:useBean tag and the jsp:setProperty tag!  The useBean tag will look for an instance of the "user.UserData" in the session.  If the instance is already there, it will update the old instance.  Otherwise, it will create a new instance of user.UserData (the instance of the user.UserData is called a bean), and put it in the session.

http://www.jsptut.com/Forms.jsp

public abstract class ClassLoaderextends ObjectA class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html

<%page language="java" contentType="text.html; charset=UTF-8"

pageEncoding="UTF-8"%>
<%
//this is declaration
int counter;
public void jspInit(){
count++;
}
%>
<html>
<head><title>title</title></head>
<body>
<%
//this is scriptlet
log("this lifecycle jsp has received a request");
counter++;
%>
<h2>jsp lifecycle:request counter</h2>
<p>this page has been called<%=counter%>times</p>
</body>
</html>

http://www.servletworld.com/jsp-tutorials/jsp-implicit-objects.html

today Java is used in many types of platforms, that includes Personal computers, Main frames, Hand held portable devices, Mobiles, Smart cards, Games and many more thing. But the biggest stream of java development that creates more jobs than any other stream is the JavaEE platform. The JavaEE platform is used to create the enterprise applications (It includes web applications as well).

Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,

这个最好动手做一下比较实在!
面试中的servlet生命周期,jsp内置对象、几种跳转的区别---j2ee新人面试中必考。

我们接触的Web容器或Jsp/Servlet本质就是一个多线程,

状态的生命周期在J2EE中目前有三种:Request/Session和Application,Request是每个客户端发出的一次请求,这是J2EE系统中最基本的事件激活单元, 当服务器端推出一个页面到客户端时,意味着这个Request的结束。那么如果我们的状态保存在Request中,意味着在request结束之前,这个请求经历的任何一个环节都可以对这个状态(对象)进行操作。(掌握这个原理,对于你学习Struts和JSF很有帮助)

  如果是Session,则一直和该客户端有关,只要是该客户端发出的每次request的任何环节都可以对这个状态(对象)进行操作。

  如果是Application,则意味着这个状态是当前Web项目的全局状态。

  这三种状态形式都是以将状态保存在内存中形式存在的,是和持久化状态相对的。是一种内存活动状态。
http://www.jdon.com/artichect/state.htm

Jdon是什么?回答这个问题可以直接借助他的创始人板桥写在其官方网站上的介绍:Jdon Framework(简称JF)是一套适合开发中小型J2EE/JavaEE应用系统的轻量Web框架(Lightweight Java Web Framework)。JF诞生于2004年底,是国人独立开发的中国人自己的框架产品,2005年入选全球SUN公司网站java.net正式企业应用目录。

http://hi.baidu.com/bjyss/blog/item/4c31b5c554fa15cc39db49ec.html   初始化参数----------------------------------------------------------
每个servlet都会有一个唯一的ServletConfig引用。
一旦有了ServletConfig的引用就可以调用getInitParameter()方法来
取得我们在servlet中设置的初始化参数。
servlet的ServletConfig对象拥有该servlet的ServletContext的一个引用,所以可这样取得上下文初始化参数;
getServletConfig().getServletContext().getInitParameter()
也可以在servlet中直接调用getServletContext().getInitParameter(),两者是等价的。


网上搜了些资料
-----------------------------------------------------------------------------
B/S架构中,客户端与服务器连接,在服务端就会自动创建一个session对象. session.setAttribute("username",username); 是将username保存在session中!session的key值为“username”value值就是username真实的值,或者引用值.这样以后你可以通过session.getAttribute("username")的方法获得这个对象. 比如说,当用户已登录系统后你就在session中存储了一个用户信息对象,此后你可以随时从session中将这个对象取出来进行一些操作,比如进行身份验证等等.
---------------------------------------------------------------------------
1、request.getSession()可以帮你得到HttpSession类型的对象,通常称之为session对象,session对象的作用域为一次会话,通常浏览器不关闭,保存的值就不会消失,当然也会出现session超时。服务器里面可以设置session的超时时间,web.xml中有一个session time out的地方,tomcat默认为30分钟 2、session.setAttribute("key",value);是session设置值的方法,原理同java中的HashMap的键值对,意思也就是key现在为“user”;存放的值为userName,userName应该为一个String类型的变量吧?看你自己的定义。 3、可以使用session.getAttribute("key");来取值,以为着你能得到userName的值。 4、注意:getAttribute的返回值类型是Object,需要向下转型,转成你的userName类型的,简单说就是存什么,取出来还是什么。 5、setAttribute和getAttribute就是基于HashMap的put方法和get方法实现的,一般叫键值对或者key-value,即通过键找到值。例如你的名字和你的人的关系,只要一叫你的名字,你就会喊到,通过你的名字来找你的人,简单说这就是键值对的概念。

猜你喜欢

转载自wwwjjq.iteye.com/blog/1488341
今日推荐