I’m working with multiple files and servlets and I pass a variable as a flag in the following way :
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>
Now I go to 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);
}
}
Here I grab the hidden value and place it under the name managerQuery1 .
Then I’m being forwarded to 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>
And now I go to 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
...
...
}
}
And now the value of manager after grabbing managerQuery1 is null .
Why is it null ? I thought that session-variables are supposed to stay until the end of the program .
Thanks
You set the managerQuery1 in a session attribute, and try to fetch it from a request parameter.
fetch it from the session, and you’re done.