session.getAttribute和session.getid
时间: 2023-09-25 20:12:47 浏览: 63
`session.getAttribute()` 方法可以用于获取在当前会话中保存的对象的值,该方法需要传入一个字符串参数作为对象的 key 值。例如 `session.getAttribute("username")` 将返回在会话中名为 "username" 的对象的值。
`session.getId()` 方法返回当前会话的唯一标识符。每个新的会话都会创建一个新的唯一标识符。可以使用该方法来确定用户是否已经登录或创建一个新的会话。例如,我们可以将会话 ID 存储在数据库中,以便我们在会话过期后重新连接到会话。
相关问题
if (cx.equals(“管理员”)) { table = “admins”; Admins user = adminsService.login(username, pwd); if (user == null) { return showError(“用户名或密码错误”); } session.setAttribute(“id”, user.getId()); session.setAttribute(“username”, user.getUsername()); session.setAttribute(“cx”, cx); session.setAttribute(“login”, cx); session.setAttribute(“username”, user.getUsername()); session.setAttribute(“pwd”, user.getPwd()); JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(user)); jsonObject.put(“cx”, session.getAttribute(“cx”)); jsonObject.put(“username”, session.getAttribute(“username”)); jsonObject.put(“login”, session.getAttribute(“login”)); assign(“session”, jsonObject); } 把这段代码转换成伪代码
如果cx等于“管理员”,则执行以下操作:
- 将table赋值为“admins”;
- 调用adminsService的login方法,将username和pwd作为参数传入,将结果赋值给user;
- 如果user为null,则返回showError("用户名或密码错误");
- 将user的id、username、pwd等属性值分别存入session中;
- 将user转换为JSON格式,并将session中的cx、username、login属性值也存入JSON对象中;
- 将JSON对象存入一个名为session的变量中。
伪代码如下:
```
if (cx.equals("管理员")) {
table = "admins";
user = adminsService.login(username, pwd);
if (user == null) {
return showError("用户名或密码错误");
}
session.id = user.id;
session.username = user.username;
session.cx = cx;
session.login = cx;
session.username = user.username;
jsonObject = json(user);
jsonObject.cx = session.cx;
jsonObject.username = session.username;
jsonObject.login = session.login;
session = jsonObject;
}
```
@RequestMapping(value = "/do_login.do", method = RequestMethod.POST) public SverResponse<UserEntity> doLogin(String account, String password, HttpSession session) { SverResponse<UserEntity> response = userService.doLogin(account, password); if (response.isSuccess()) { // 登陆成功,将用户信息存入session // Cookie cookie = new Cookie("JSESSIONID", session.getId()); // respons.addCookie(cookie); // respons.addHeader("Set-Cookie", "JSESSIONID=" + session.getId()); // 设置Cookie session.setAttribute(ConstUtil.CUR_USER, response.getData()); // System.out.println(session.getAttribute(ConstUtil.CUR_USER)); } return response; } 在这端程序中设置cookie响应头
在 Spring MVC 中,可以通过 `HttpServletResponse` 对象来设置响应的 Header,从而设置 Cookie。具体来说,可以在控制器方法中通过注入 `HttpServletResponse` 对象来实现,代码如下:
```
@RequestMapping(value = "/do_login.do", method = RequestMethod.POST)
public SverResponse<UserEntity> doLogin(String account, String password, HttpSession session, HttpServletResponse response) {
SverResponse<UserEntity> response = userService.doLogin(account, password);
if (response.isSuccess()) {
// 登陆成功,将用户信息存入session
session.setAttribute(ConstUtil.CUR_USER, response.getData());
// 设置 Cookie
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(30 * 60); // 设置 Cookie 的过期时间为 30 分钟
cookie.setPath("/");
response.addCookie(cookie);
}
return response;
}
```
在上面的代码中,我们通过 `response.addCookie(cookie)` 方法来设置 Cookie。其中,`cookie` 是要设置的 Cookie 对象,`setMaxAge` 方法用于设置 Cookie 的过期时间(单位为秒),`setPath` 方法用于设置 Cookie 的路径。需要注意的是,如果不设置 Cookie 的路径,则默认为当前请求的路径。
除了通过 `addCookie` 方法来设置 Cookie,还可以通过设置响应头的方式来实现。具体来说,可以通过以下代码来设置响应头:
```
response.setHeader("Set-Cookie", "JSESSIONID=" + session.getId() + "; Path=/; Max-Age=1800");
```
其中,`setHeader` 方法用于设置响应头,`Max-Age` 用于设置 Cookie 的过期时间(单位为秒)。需要注意的是,如果要设置多个 Cookie,可以将它们用分号和空格隔开,例如:
```
response.setHeader("Set-Cookie", "cookie1=value1; Path=/; Max-Age=1800; " +
"cookie2=value2; Path=/; Max-Age=1800; " +
"cookie3=value3; Path=/; Max-Age=1800");
```
以上就是在 Spring MVC 中设置 Cookie 响应头的基本步骤。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)
HttpSession session = request.getSession(); List<NewInfor> list; NewsService newsService = new NewsService(); //查询所有新闻 list = newsService.getAllNews(); //存入 session.setAttribute("list", list); //跳转页面 request.getRequestDispatcher("/index.jsp").forward(request,response); List<NewInfor> list = (List<NewInfor>) session.getAttribute("list"); for (int i=0; i <%= newInfor.getTitle()%> <%= newInfor.getAuthor()%> <%= newInfor.getType()%> <%= newInfor.getTime()%> <% } %>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="search.user" import="search.ticket"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body bgcolor=pink>
录入成功!
<% user user=(user)session.getAttribute("user"); ticket[] tickets=(ticket[])session.getAttribute("tickets"); %> <% for(int i=0;i<tickets.length;i++){ out.println(tickets[i].getId()+" "+tickets[i].getWherestart()+" "+tickets[i].getGotowhere()+" "+ tickets[i].getStart_time()+" "+tickets[i].getAirEnterPrice()+"
"); } %> 发送</a--> </body> </html>如果要把tickets数组发送过去该怎么改
![](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)