后台代码
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PersonDaoImpl pdi = new PersonDaoImpl();
List<Person> li = pdi.getResult();
System.out.println(li);
request.setAttribute("values", li);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
前台代码:
<%
System.out.println(request.getAttribute("values"));
%>
getAttribute:以Object的形式返回指定属性的值;如果不存在指定名称的属性,则返回null。
属性可以通过两种方式设置。 servlet容器可以设置属性以提供关于请求的可用定制信息。 例如,对于使用HTTPS进行的请求,可以使用属javax.servlet.request.X509Certificate来检索客户端证书上的信息。 也可以使用ServletRequest.setAttribute以编程方式设置属性。 这允许
要在RequestDispatcher调用之前嵌入到请求中的信息。
属性名称应遵循与包名称相同的约定。 这个规范
保留与java。,javax。和sun。*匹配的名称。
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using ServletRequest.setAttribute. This allows
information to be embedded into a request before a RequestDispatcher call.
Attribute names should follow the same conventions as package names. This specification
reserves names matching java., javax., and sun.*.
public void setAttribute(java.lang.String name,
java.lang.Object o)
Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.
Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and com.sun.*, are reserved for use by Sun Microsystems.
If the object passed in is null, the effect is the same as calling removeAttribute(java.lang.String).
It is warned that when the request is dispatched from the servlet resides in a different web application by RequestDispatcher, the object set by this method may not be correctly retrieved in the caller servlet.
Parameters:
name - a String specifying the name of the attribute
o - the Object to be stored