我将尝试解释我的问题。 我有一个login.jsp,你在其中填写用户名和密码并将其发送到servlet,这个servlet检查数据库,如果正确调用userLogged.jsp,如果不正确则发送到invalidLogin.jsp。 这很好,即使我得到userlogged.jsp,我也可以检索我在servlet中创建的会话数据。 Userlogged.jsp创建一个包含三个部分的html页面,一个按钮可以继续,一个表单可以更改密码,一个横幅。然后当我点击按钮继续时,我调用servlet,但是在这里我无法获得会话,总是为空。 当我单击按钮将会话信息发送到servlet时,如何通过? 我粘贴代码,如果您需要更多信息,请告诉我。谢谢。
SERVLET
private void getLogin(HttpServletRequest request, HttpServletResponse response, DataAccessUtils dao) throws ServletException, IOException
{
String userName = request.getParameter("username");
String userPassword = request.getParameter("password");
// DATABASE ACCESS
Login login = new Login();
String[] currentUser = login.validateLogin(userName, userPassword, dao);
if (!currentUser[0].equals("-1"))
{
String nextJSP = "/actions/userLogged.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
HttpSession session = request.getSession(true);
session.setAttribute("username", currentUser[0]);
session.setAttribute("agencyname", currentUser[1]);
session.setAttribute("branchname", currentUser[2]);
session.setAttribute("currenttravelfile", "N/A");
dispatcher.forward(request,response);
}
else
{
response.sendRedirect("actions/invalidLogin.jsp"); //error page
}
}
private void getGoToMenu(HttpServletRequest request, HttpServletResponse response, DataAccessUtils dao) throws ServletException, IOException
{
// HERE ALWAYS IS NULL
String username = (String)request.getSession().getAttribute("username");
String nextJSP = "/actions/menu.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
}
USERLOGGED.jsp(仅我调用servlet的部分)
<form action="./getjsprequestservlet" method="post" id="userlogged-form">
<fieldset>
<div style="margin-bottom:1em; margin-left: 11em;">
<input type=hidden name=action id=opr value="gotomenu"/>
<input type="submit" value="Go To Menu" class="button round blue image-right ic-right-arrow"></input>
</div>
<div class="information-box round">
<input type=hidden name=action id=opr value="change-password"/>
<p>
<label class="title">Change Password</label>
<label>Current Password</label>
<input type="password" name="password" id="login-password" class="round full-width-input" />
<label>New Password</label>
<input type="password" name="password" id="login-password" class="round full-width-input" />
<label>Confirm New Password</label>
<input type="password" name="password" id="login-password" class="round full-width-input" />
<input type="submit" value="Change" class="button round blue image-right ic-right-arrow"></input>
</p>
</div>
</fieldset>
</form>
<div class="banner-box round">
<label class="alt-label">Sponsored links </label>
<table id="raw">
<tr>
<td>
<a href="#" class="fr"><img src="./images/test.gif"/></a>
</td>
<td>
<a href="#" class="fr"><img src="./images/test2.gif"/></a>
</td>
<td>
<a href="#" class="fr"><img src="./images/test3.gif"/></a>
</td>
</tr>
</table>
</div>
</div> <!-- end content -->