java web中存入会话域的字符串如何取出来?
假如使用HttpSession的setAttribute()方法往会话域存入了一个字符串username,在另一个Servlet中想要读取这个字符串并使用应该如何操作?getAttribute()读出来的是Object对象,也不能强制转换成字符串,还没读出来的时候也不知道是什么对象,无法调用以及写好的方法。将数据存入会话域的Servlet
package edu.zjku.controller;
import edu.zjku.bean.User;
import edu.zjku.service.UserService;
import edu.zjku.service.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//这是处理登录的Servlet
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户登录的信息
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
//核实用户登录信息
UserService service = new UserServiceImpl();
List<User> list = service.selectAll();
//下面是一个用来记录用户是否存在的变量
boolean exist = false;
for (User u : list
) {
//判断数据库中是否存在用户名
if (username.equals(u.getUsername())) {
//若存在,判断密码是否正确
exist = true;
if (password.equals(u.getPassword())) {
resp.getWriter().write("欢迎登录," + username + "!");
System.out.println("登录成功!");
//将登录信息存入会话域
req.getSession().setAttribute("username", user);
break;
} else {
resp.getWriter().write("密码错误!");
System.out.println("登录失败!");
break;
}
}
}
//用户不存在,输出提示信息
if (!exist){
System.out.println("用户不存在!");
resp.getWriter().write("该用户不存在!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
想要读取并使用刚才存入会话域中的Servlet
package edu.zjku.controller;
import edu.zjku.bean.Blog;
import edu.zjku.bean.User;
import edu.zjku.service.BlogService;
import edu.zjku.service.BlogServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//这是写入博客的Servlet
@WebServlet("/writeServlet")
public class WriteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BlogService service = new BlogServiceImpl();
HttpSession session = req.getSession();
Object username = session.getAttribute("username");
if (username!=null){
//获取前台输入的信息
String title=req.getParameter("title");
String text=req.getParameter("text");
//从Session域读取username
String user=String(username);
Blog blog = new Blog(title,text,user);
service.insert(blog);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在另一个Servlet中读取存入会话域的字符串可以使用HttpSession的getAttribute()方法。由于getAttribute()方法返回的是Object对象,我们可以将其强制转换为字符串。
在想要读取的Servlet中,可以按照以下步骤操作:
1. 获取HttpSession对象:使用HttpServletRequest的getSession()方法获取当前请求的会话对象。
2. 使用getAttribute()方法获取存入会话域的字符串:调用HttpSession对象的getAttribute()方法,传入存入会话域的字符串的键名,即"username"。
3. 将返回的Object对象强制转换为字符串:由于getAttribute()方法返回的是Object对象,我们需要将其强制转换为字符串。可以使用String.valueOf()方法将其转换为字符串。
以下是修改后的代码:
package edu.zjku.controller;
import edu.zjku.bean.Blog;
import edu.zjku.bean.User;
import edu.zjku.service.BlogService;
import edu.zjku.service.BlogServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/writeServlet")
public class WriteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BlogService service = new BlogServiceImpl();
HttpSession session = req.getSession();
Object username = session.getAttribute("username");
if (username != null) {
// 获取前台输入的信息
String title = req.getParameter("title");
String text = req.getParameter("text");
// 将从Session域读取的username转换为字符串
String user = String.valueOf(username);
Blog blog = new Blog(title, text, user);
service.insert(blog);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
通过以上步骤,你可以在另一个Servlet中成功读取存入会话域的字符串,并将其使用在后续的逻辑中。
页:
[1]