我正在使用多个文件和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);
}
}
在这里,我抓住隐藏的值并将其放在名称managerQuery1
下。
然后我被转发到employeeOpenNewAccount.jsp
:
employeeOpenNewAccount.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:
employeeOperation1:
@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
。
为什么它为空?我认为会话变量应该一直持续到程序结束。
谢谢
答案 0 :(得分:2)
您在manager属性中设置managerQuery1,并尝试从请求参数中获取它。
从会话中获取它,你就完成了。
String manager = session.getAttribute("managerQuery1");
答案 1 :(得分:1)
String manager = session.getAttribute("managerQuery1");
我认为你需要加入String
String manager =(String) session.getAttribute("managerQuery1");
您可以关注以获得更多想法。
javax.servlet.http.HttpSession session = request.getSession();
完成后,您可以设置如下会话对象: session.setAttribute( “姓名”, “值”);
要检索该值,请执行以下操作:
String foo = (String) session.getAttribute("name");