获取session值的问题

七星院长 2009-05-10 12:42:52
我的session建在了登陆的类中,这个类是 servlet类。里面的request.getSession().setAttribute("username", users);
这是建的session值 。
但我又建了个自定义标签类。我想获取登陆的session的值该怎么做啊。我用jspContext session 不行啊,是空指针的问题。
...全文
549 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
诚待世事 2009-05-17
  • 打赏
  • 举报
回复
在页面上用${sessionScope.username}在类里用request.getSession().getAttribute("username")
tobeno2 2009-05-11
  • 打赏
  • 举报
回复
你的值没有set到session里面去吧?
if (rs.next()),换成while试试?
request.getSession().setAttribute 放在response.sendRedirect前面。。。
七星院长 2009-05-10
  • 打赏
  • 举报
回复
我的意思是在我新建的类中这样写。用jspContext session=null。然后
String sUserName1=(String) session.getAttribute("username");
但这样报错是空指针,
jxplus 2009-05-10
  • 打赏
  • 举报
回复
这样取试试:{session.username}
chenleicom 2009-05-10
  • 打赏
  • 举报
回复
你两个 request不是同一个请求
七星院长 2009-05-10
  • 打赏
  • 举报
回复
String users = request.getParameter("username"); //获取登陆页面的用户名
String pwd = request.getParameter("password"); //获取登陆页面的密码
LogonForm logonForm = new LogonForm(); //实例化登陆form
String sql = "select * from DIM_USER_09 where user_id='" + users+ "' and user_pwd='" + pwd + "'"; //从数据库表中查询所登陆的用户名及密码是否存在。
Statement stmt = null;
ResultSet rs = null;
try {
DataConn DBC = new DataConn(); //实例化DataConn,来连接数据库。
stmt = DBC.getConn().createStatement(); //通过connection连接数据库后,创建Statement语句。
rs = stmt.executeQuery(sql);// 返回查询结果
} catch (SQLException e) {
e.printStackTrace(); //输出SQL异常
}
try {
if (rs.next()) {
// response.sendRedirect("frammain.jsp");
response.sendRedirect("open_main1.html");
request.getSession().setAttribute("username", users);
} else {
response.sendRedirect("logon2.jsp");
}
} catch (SQLException e) {
e.printStackTrace(); //输出SQL异常
}
}
我又新建了个类,想获取上面设置的session参数,request.getSession().setAttribute("username", users);
即用户名,但我不知道该怎么获取了
注:我新建的不是servlet类,所以不能用request
七星院长 2009-05-10
  • 打赏
  • 举报
回复
String users = request.getParameter("username"); //获取登陆页面的用户名
String pwd = request.getParameter("password"); //获取登陆页面的密码
LogonForm logonForm = new LogonForm(); //实例化登陆form
String sql = "select * from DIM_USER_09 where user_id='" + users+ "' and user_pwd='" + pwd + "'"; //从数据库表中查询所登陆的用户名及密码是否存在。
Statement stmt = null;
ResultSet rs = null;
try {
DataConn DBC = new DataConn(); //实例化DataConn,来连接数据库。
stmt = DBC.getConn().createStatement(); //通过connection连接数据库后,创建Statement语句。
rs = stmt.executeQuery(sql);// 返回查询结果
} catch (SQLException e) {
e.printStackTrace(); //输出SQL异常
}
try {
if (rs.next()) {
// response.sendRedirect("frammain.jsp");
response.sendRedirect("open_main1.html");
request.getSession().setAttribute("username", users);
} else {
response.sendRedirect("logon2.jsp");
}
} catch (SQLException e) {
e.printStackTrace(); //输出SQL异常
}
}
我又新建了个类,想获取上面设置的session参数,request.getSession().setAttribute("username", users);
即用户名,但我不知道该怎么获取了
w382572397 2009-05-10
  • 打赏
  • 举报
回复
看不懂。。。
wad12302 2009-05-10
  • 打赏
  • 举报
回复
但我又建了个自定义标签类。


不明白你的意思主要说的是什么啊 最好 吧 以及你 的 自定义标签 主要 是用来干嘛的
最好有代码 贴上来

不然题目意思都没弄明白 别人也无从下手

81,095

社区成员

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

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