setAttribute、getAttribute、getParameter方法的用法 /// Session的getSession()方法的使用小结
setAttribute、getAttribute、getParameter
- 一、setAttribute、getAttribute、getParameter
- **例如: servlet从请求页面中通过getParameter来获得参数**
- **例如:跳转页面通过getAttribute来获得请求的属性
- 二、Session的getSession()方法的使用小结
一、setAttribute、getAttribute、getParameter
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型
getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型
例如: servlet从请求页面中通过getParameter来获得参数
请求页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>查询点击页面</title>
</head>
<body>
<form action="/userList"><p><input type="text" value="queryAll" name="type"></p><p><input type="submit" value="点我查询所有用户"></p>
</form>
</body>
</html>
servlet类:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {**String type = req.getParameter("type");**if ("queryAll".equals(type)){queryAll(req,resp);}
**例如:跳转页面通过getAttribute来获得请求的属性
而该属性必须是要在servlet中通过setAttribute设置过的**
servlet类设置属性:
private void queryAll(HttpServletRequest req, HttpServletResponse resp){
ArrayList users = service.queryAll();
HttpSession session = req.getSession();
session.setAttribute( “users” ,users);
try {
resp.sendRedirect( " /login/userList.jsp");
}catch ( IOException e) {
e.printStackTrace();
}
页面接收属性:
<%
Object users = session.getAttribute("users");
if(null!=users&&users instanceof ArrayList){ArrayList<User> userList=(ArrayList< User>)users;for (User user : userList) {%>
<tr><td><%=user.getUser_name()%></td><td><%=user.getUser_name()%></td><td><%=user.getPassword()%></td>
</tr>
<%}
}
%>
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
authenticate.jsp
或者:
请输入用户姓名:在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);
%>
getAttribute是返回对象,getParameter返回字符串
二、Session的getSession()方法的使用小结
getSession()
HttpServletRequest.getSession(ture) 与 HttpServletRequest.getSession() 是一个意思;
HttpServletRequest.getSession(false) 等同于:如果当前Session没有就为null;
Session在网络应用中被称为会话。
具体到web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间,因此从概述上我们可以看到,session实际上是一个特定的时间概念。
需要注意的是:一个session的概念需要包括特定的客户端,特定的服务器端以及不中断的操作时间。A用户和C服务器建立连接时所处的session同B用户和C服务器建立连接时所处的Session是两个不同的session。
Session的工作原理:
(1)当一个session第一被启动时,一个唯一的标识被存储与本地的cookie中;
(2)首先使用session_start()函数,从session仓库中加载已经存储的session变量。
HttpRequest对象有两种形式的getSession的方法调用:
getSession()
getSession(boolen isNew)
这样,前者会检测当前时候是否有session存在,如果不存在则创建一个,如果存在就返回当前的。
因此,getSession( )相当于getSession(true);
参数为true时,若存在会话,则返回该会话,否则新建一个会话;
参数为false时,如存在会话,则返回该会话,否则返回NULL;