如何在Action中获取用户session中的值?

6fish 2007-08-08 12:27:30
@Override

public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
buzzLog.info("-@Override-list---");
int ret = 1;

HttpSession session = request.getSession();
String val = (String) session.getAttribute("test");
if ( val == "yes") {
super.list(mapping, form, request, response);
return mapping.findForward(LIST);
}
val取到的值是空的

在用户登录的时候已经setAttribute("test","yes");
...全文
3108 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
6fish 2007-08-09
  • 打赏
  • 举报
回复
确实是这里出的问题request.setAttribute("test", "yes");

<c:redirect url="/aue/calendar.do"/>

这句话,重定向了在session中可以取到值

谢谢诸位
zjf405 2007-08-09
  • 打赏
  • 举报
回复
顶ls,楼主在存对象的时候存错了位置
南南北北 2007-08-09
  • 打赏
  • 举报
回复
request.setAttribute("login", "yes");
request.setAttribute("test", "yes");

val = (String) session.getAttribute(attribute);

-----------------------------------------------------
存在reqeust中,去session中能取到才怪。

-----------------------------------------------------
<c:redirect url="/aue/calendar.do"/>

这句话,重定向了,不在同一session内。
coolwzjcool 2007-08-09
  • 打赏
  • 举报
回复
把struts-config.xml贴出来,我怀疑是不是你这个action配置的时候,没有写scope="request"
ws9958 2007-08-09
  • 打赏
  • 举报
回复
不好意思多写了一个request
应该是:


我在action 里就是这么接的:
String str = (String)request.getSession().getAttribute("user_id");
存的时候:
request.getSession().setAttribute("user_id", "123");

ws9958 2007-08-09
  • 打赏
  • 举报
回复
我在action 里就是这么接的:
String str = (String)request.request.getSession().getAttribute("user_id");
存的时候:
request.getSession().setAttribute("user_id", "123");
6fish 2007-08-09
  • 打赏
  • 举报
回复
login 成功后跳转到 index_login.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/commons/taglibs.jsp" %>

<html>
<head>
<link href="${ctx}/styles/admin/admin.css" type="text/css" rel="stylesheet">
<%@ include file="/commons/meta.jsp" %>
<title> Welcome to System.</title>
<META content=JavaScript name=vs_defaultClientScript>
</head>
<SCRIPT>

function switchBar(){
if (switchPoint.innerText==""){
switchPoint.innerText=" "
document.all("leftFrame").style.display="none"
}else{
switchPoint.innerText=""
document.all("leftFrame").style.display=""
}}


function ComfirmExit(action){
if(action==1)
{
myconfirm = confirm("确实要关闭窗口,退出系统吗?");
if (myconfirm==true){
top.location.href="CloseWin.aspx?Action=1";
}
}
if(action==2)
{
myconfirm = confirm("确实要重新登陆吗?");
if (myconfirm==true){
top.location.href="CloseWin.aspx?Action=2";
}
}
}

</SCRIPT>

<STYLE type=text/css>.navPoint {
FONT-SIZE: 12px; CURSOR: hand; COLOR: white; FONT-FAMILY: Webdings
}
</STYLE>

<BODY bottomMargin=0 leftMargin=0 topMargin=0 rightMargin=0 scroll="no">
<TABLE id=Table1 height="100%" cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD style="HEIGHT: 50px" align=right colSpan=3>
<TABLE id=Table3 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD style="WIDTH: 291px" width=300
background="../pics/middle.gif"></TD>
<TD width="100%" background=../pics/middle.gif></TD>
<TD align=right width=292
background="../pics/middle.gif"><IMG height=68
src="../pics/lefttitle.gif" width=292 useMap=#Map
border=0></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD id=leftFrame style="WIDTH: 164px" vAlign=center noWrap align=middle
name="leftFrame"><IFRAME id=carnoc style="WIDTH: 164px; HEIGHT: 100%"
name=carnoc src="left.jsp" frameBorder=0
scrolling=yes></IFRAME></TD>
<TD style="WIDTH: 9pt" bgColor=#6699cc>
<TABLE id=Table2 height="100%" cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD title=打开/关闭全屏 style="WIDTH: 4px; CURSOR: hand; HEIGHT: 100%"
onclick=switchBar() vAlign=top
background="../pics/middlebgpic.gif"><IMG
src="../pics/middletop.gif">
<BR><BR><BR><BR><BR><BR><BR><BR><BR><SPAN id=switchPoint
title=打开/关闭全屏></SPAN><IMG src="../pics/middledot.gif">
</TD></TR></TBODY></TABLE></TD>
<TD style="WIDTH: 100%">
<TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IFRAME id=main
style="VISIBILITY: inherit; WIDTH: 100%; HEIGHT: 100%" name=main
src="main.jsp" frameBorder=0
scrolling=yes></IFRAME></TD></TR>
<TR>
<TD bgColor=#ff6633 height=20><IFRAME
src="../getReference.htm" frameBorder=0
width="100%" scrolling=no
height="100%">
</IFRAME>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</body>
</html>

其中的main.jsp
<%@ include file="/commons/taglibs.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<%@ include file="/commons/meta.jsp" %>
<link href="${ctx}/styles/admin/admin.css" type="text/css" rel="stylesheet">
</head>
<body>
<c:redirect url="/aue/calendar.do"/>
</body>
</html>

calendar.do对应的CalendarAction.java
public class CalendarAction extends StrutsEntityAction<Calendar, CalendarManager> {
@SuppressWarnings("unused")
private CalendarManager calendarManager;
private CustomerManager custm = new CustomerManager();

public void setCalendarManager(CalendarManager calManager) {
this.calendarManager = calManager;
}

private static final Log buzzLog = LogFactory.getLog(Constants.BUSINESS_LOG);

@Override

public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
buzzLog.info("-@Override-list---");
int ret = 1;

ret = custm.check("login", request);
buzzLog.info("-st---"+request.getAttribute("login"));

if (ret == 0){
super.list(mapping, form, request, response);
// request.setAttribute(getEntityListName(), calendarManager.doFind("6fish"));
return mapping.findForward(LIST);
}
else
return mapping.findForward("accessDenied");

}
public int check(String attribute,HttpServletRequest request){
int ret = 1;
String val;
HttpSession session = ((HttpServletRequest) request).getSession();
if (session == null)
buzzLog.info("---session is null ---");
val = (String) session.getAttribute(attribute);
if (attribute == "login" && val == "yes")
ret = 0;
if (attribute == "test" && val == "yes")
ret = 0;

buzzLog.info("- check--"+attribute+" is "+val+" ret="+ret);
return ret;
}
6fish 2007-08-09
  • 打赏
  • 举报
回复
login.jsp
<html>
<head>
<%@ include file="/commons/meta.jsp"%>
<link href="${ctx}/styles/admin/admin.css" type="text/css"
rel="stylesheet">
<title>Login</title>
<style type="text/css">
<!--
.style1 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style>
</head>

<body>
<form action="commons/login.do" method="POST">
<input type="hidden" name="method" value="loginForm"/>
<input type="hidden" name="level" value="0"/>
<table width="631" height="421" border="0" align="center" background="pics/shot_login.jpg">
<tr>
<td width="310" height="95"> </td>
<td width="311"> </td>
</tr>
<tr>
<td height="320"> </td>
<td><table width="100%">
<tr>
<td height="26" colspan='2'><div align="center" class="style1">Login</div></td>
</tr>
<tr>
<td height="27"><span class="style1"> User ID: </span></td>
<td><input name='number' type='text' id="number">
</td>
</tr>
<tr>
<td height="25"><span class="style1"> Password: </span></td>
<td><input name='password' type='password' id="password">
</td>
</tr>
<tr>
<td height="30"> </td>
<td></td>
</tr>
<tr>
<td colspan='2'><div align="center">
<input name="submit" type="submit" value="Submit">
</div></td>
</tr>

</table></td>
</tr>
</table>
</form>

</body>
</html>


LoginAction.java
public class LoginAction extends StrutsEntityAction<User,UserManager> {

@SuppressWarnings("unused")

private TypeConvert tct = new TypeConvert();

public void setUserManager(UserManager uManager) {
this.customerUser = uManager;
}

public ActionForward loginForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

final Log buzzLog = LogFactory.getLog(Constants.BUSINESS_LOG);
buzzLog.info("--- LoginAction Begin ---");

String userid;
String passwd;
int userlevel;
int ret = 1;

userid = request.getParameter("number");
passwd = request.getParameter("password");
userlevel = tct.stringToInt(request.getParameter("level"));

ret = loginCheck(userid,passwd,request,response);

String get = (String) request.getAttribute("login");
buzzLog.info("-level--"+userlevel+"-- Login number---"+get);
if (ret==0){
if ( userlevel== Constants.LEVEL_ADMIN){
buzzLog.info("---Welcom administrator----");
return mapping.findForward("adminlogin");
}
else{
buzzLog.info("---Welcom user----");
return mapping.findForward("login");
}
}
else
return mapping.findForward("loginerror");
}
public int loginCheck(String userid, String passwd,HttpServletRequest request,
HttpServletResponse response){

int ret = 1;
String hql;
String passwd2;
PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
passwd2 = passwordEncoder.encodePassword(passwd, null);

buzzLog.info("---Login Check Begin ---"+passwd2);

hql = "from User u where u.number=? and u.passwd=?";
try {
List userList = find(hql,userid,passwd2);
if (userList!=null && userList.size() > 0) {

User user = (User) userList.get(0);
String number=user.getNumber();
String name=user.getName();
String zone=user.getZone();
int level=user.getLevel();

HttpSession session = request.getSession();
if (session == null)
buzzLog.info("---session is null ---");


session.setMaxInactiveInterval(6000);
session.removeAttribute("login");
session.removeAttribute("zone");
session.removeAttribute("level");

request.setAttribute("number", number);
request.setAttribute("name", name);
request.setAttribute("zone", zone);
request.setAttribute("level", level);
request.setAttribute("login", "yes");
request.setAttribute("test", "yes");
ret = 0;
}
}
catch(Exception e){
buzzLog.info("---find error ---");
e.printStackTrace();
ret = -1;
}
return ret;
}


}
6fish 2007-08-08
  • 打赏
  • 举报
回复
关键是val取到的值是空的 null
awusoft 2007-08-08
  • 打赏
  • 举报
回复
不好意思,想着application了.
不是==啦...是val.equal("yes")
zjf405 2007-08-08
  • 打赏
  • 举报
回复
判断写错了吧,应该是
if (val.equals("yes")){
super.list(mapping, form, request, response);
return mapping.findForward(LIST);
}
awusoft 2007-08-08
  • 打赏
  • 举报
回复
HttpSession session = request.getServletConext().getSession();
awusoft 2007-08-08
  • 打赏
  • 举报
回复
代码.........
南南北北 2007-08-08
  • 打赏
  • 举报
回复
贴代码吧。
6fish 2007-08-08
  • 打赏
  • 举报
回复
我将frame改为了iframe,结果还是一样 null
南南北北 2007-08-08
  • 打赏
  • 举报
回复
一个页面有几个frame,不同的frame是不同的session.你可以改成iframe试试。
6fish 2007-08-08
  • 打赏
  • 举报
回复
我是在LoginAciton中用return mapping.findForward("login");跳转到的一个用了frame的页面
是否还是在一个session里

不知如何解决这个问题
南南北北 2007-08-08
  • 打赏
  • 举报
回复
看看你前面的页面有没有frame或者你使用了 response.sendRedirect();

window.location忘了是不是还在同一会话了。
awusoft 2007-08-08
  • 打赏
  • 举报
回复
val.equals("yes")这样会报错就证明了是空的了。你要确定你的页面是在同一个session里啊。
6fish 2007-08-08
  • 打赏
  • 举报
回复
我用val.equals("yes")会报错
ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/aue].[action]] - <Servlet.service() for servlet action threw exception>
java.lang.NullPointerException

用val == "yes"就没事

改为HttpSession session = ((HttpServletRequest) request).getSession();
debug出来的 val is null
加载更多回复(3)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧