我正在处理多个文件和 servlet,并通过以下方式将变量作为标志传递:
managerPage.jsp:
<fieldset>
<legend>To open a new account</legend>
<form action="employeeTransaction1">
<input type="hidden" name="hdField" value="managerFlagOn" />
<input type="submit" value="Press here to continue" />
</form>
</fieldset>
现在我转到employeeTransaction1 servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
synchronized(session)
{
String hiddenValue = request.getParameter("hdField");
// then the redirection was made from a Manager's page
if (hiddenValue.equals("managerFlagOn") == true)
{
session.setAttribute("managerQuery1", hiddenValue);
}
// forwards to the page employeeOpenNewAccount.jsp
request.getRequestDispatcher("/WEB-INF/results/employeeOpenNewAccount.jsp").forward(request, response);
}
}
在这里,我获取隐藏的值并将其放在 name 下managerQuery1
。
然后我被转发到employeeOpenNewAccount.jsp
:
员工OpenNewAccount.jsp:
<!-- EMPLOYEE OP 1 - open a new account -->
<!DOCTYPE html>
<html>
<head><title>Employee's transaction page - open a new account</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<h1>Employee's transaction page!</h1>
<h1>
Open a new Bank account
</h1>
<!-- from here redirecting to the servelet that's called "employeeOperation1" -->
<fieldset>
<legend>Please fill the followings</legend>
<form action="employeeOperation1">
First-name : <input type="text" name="firstName"><br>
Last-name : <input type="text" name="lastName"><br>
Address : <input type="text" name="address"><br>
ID-number : <input type="text" name="idNumber"><br>
User-name : <input type="text" name="userName"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="Register">
</form>
</fieldset>
</body></html>
现在我去employeeOperation1
servlet :
员工操作1:
@WebServlet("/employeeOperation1")
public class EmployeeServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
HttpSession session = request.getSession();
synchronized(session)
{
String manager = request.getParameter("managerQuery1"); // the value is null
...
...
}
}
而现在manager
抢后的价值managerQuery1
是null
。
为什么它是空的?我认为会话变量应该一直保留到程序结束。
谢谢