request.setAttribute存储数组到request里面
时间: 2024-05-09 09:22:00 浏览: 11
这是一个常见的操作,可以通过以下代码将数组存储到request中:
```java
String[] array = {"value1", "value2", "value3"};
request.setAttribute("arrayName", array);
```
然后就可以在JSP页面或Servlet中读取这个数组:
```java
String[] array = (String[]) request.getAttribute("arrayName");
```
请注意,如果你想将Java对象存储到request中,需要确保该对象实现了Serializable接口。
相关问题
request.setAttribute("ctlist", ctlist);
这段代码是将一个名为 "ctlist" 的属性添加到请求(request)对象中,属性值为 ctlist。在 Java Web 开发中,可以通过在请求(request)对象中设置属性,将数据传输到 JSP 页面或 Servlet 中,以便进行处理和展示。在这个例子中,ctlist 可能是一个 List 或者数组等数据结构,用于存储一些数据,并将这些数据传递到下一个处理页面。
HttpSession session = request.getSession(); session.setAttribute("yuanshishujuceshijieguoArray", psArray); response.sendRedirect("index.jsp");
这段代码的作用是将psArray数组存储在名为"yuanshishujuceshijieguoArray"的HttpSession对象中,然后将请求重定向到名为"index.jsp"的页面。具体的过程如下:
首先,通过调用request对象的getSession()方法获取到当前请求的HttpSession对象,然后调用其setAttribute()方法将psArray数组存储在名为"yuanshishujuceshijieguoArray"的属性中。这样,在后续的请求中,可以通过调用getSession()方法获取到相同的HttpSession对象,并调用其getAttribute()方法来获取"yuanshishujuceshijieguoArray"属性中存储的psArray数组。
接着,通过调用response对象的sendRedirect()方法将请求重定向到名为"index.jsp"的页面。这样,客户端的浏览器就会重新发送一个请求到"index.jsp"页面,同时该请求也会携带之前存储在HttpSession对象中的"yuanshishujuceshijieguoArray"属性,从而可以在"index.jsp"页面中使用该属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%-- 作者:GGG166 --%> <%@page contentType="text/html;charset=gb2312" %> <%@page import="java.util.*"%> <html> <body> <%! Vector v=new Vector();//动态数组 int i=0; ServletContext application; synchronized void leaveWord(String s){//方法声明,用于在添加评论 application=getServletContext(); i++; v.add("No"+i+","+s); application.setAttribute("Mess",v); } %> <% request.setCharacterEncoding("gb2312");//乱码处理 String name=request.getParameter("name");//接收姓名 String title=request.getParameter("title");//接收标题 String message=request.getParameter("message");//接收评论 if(name==null){ name="guest"+(int)(Math.random()*10000); } if(title==null){ title="无标题"; } if(message==null){ message="无信息"; } String s=name+"#"+title+"#"+message; leaveWord(s); out.print("你的评论已提交!"); %> 返回留言页面 </body> </html>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)