我可以在scriptlet中设置会话属性,但是当我尝试在java类中设置会话属性时,它显示诸如“无法解析会话”之类的错误。那么如何在Java中设置会话?
<%String username = (String)request.getAttribute("un"); session.setAttribute("UserName", username);%>
对于Java类,我假设您是指Servlet类,因为在任意Java类中设置会话属性没有意义。您可以在servlet的doGet / doPost方法中执行类似的操作
public void doGet(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String username = (String)request.getAttribute("un"); session.setAttribute("UserName", username); }