我将尝试解释我的问题。我有一个 login.jsp,在其中填写用户名和密码并将其发送到 servlet,该 servlet 检查数据库以及是否正确调用 userLogged.jsp,如果不正确则发送到 invalidLogin.jsp。这很好用,即使我获得了 userlogged.jsp,我也可以检索我在 servlet 中创建的会话数据。Userlogged.jsp 创建一个包含三个部分的 html 页面,一个按钮继续,一个表单更改密码和一个横幅。然后当我单击按钮继续时,我调用了 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 -->