当前位置: 代码迷 >> Java Web开发 >> 怎么从session获取登录页的账号密码
  详细解决方案

怎么从session获取登录页的账号密码

热度:5791   发布时间:2013-02-25 21:05:26.0
如何从session获取登录页的账号密码
前台登录代码:
<s:form action="login" namespace="/" method="post" theme="simple">
<tr><td align="right" width="79">用户名:</td><td width="100"><input name="userName" type="text" class="txtitem" id="userName"/></tr>
<tr><td align="right">密 码:</td><td><input name="password" type="password" class="txtitem" id="password"/> <input name="method:login" value="1" type="hidden"/></td></tr>
<tr><td ><input type="submit" value="登录"></td></tr>

</s:form>

jsp页面登录后是通过struts重定向到另一个jsp页面:需要得到之前登陆页的 用户名和密码。记得这是存储在session中,如何获取里面的值。。。。。。
struts session jsp
HttpSession httpSession =ServletActionContext.getRequest().getSession();
String username = httpSession.getAttribute("userName");
String password= httpSession.getAttribute("password");可以直接在你的另一页面用小脚本来获取,session.getAttribute("userName");
和上面给的方法一样的!session.setAttribute("值的名字",值对象);//往session里放值的方法
session.getAttribute("值的名字");//从session里取值的方法
//不过通常你获取到的只有request对象
request.getSession().getAttribute("");//session是被封装在request里边的楼上的朋友说的没错,在action中将用户名和密码设置到session中
session.setAttribute("userName",request.getParameter("userName"));
session.setAttribute("password",request.getParameter("password"));
//在action中获取
request.getSession().getAttribute("userName");
request.getSession().getAttribute("password");
//在jsp页面获取
<input type="text" value="<s:property value='#session.userName'/>"/>
<input type="text" value="<s:property value='#session.password'/>"/>
  相关解决方案