-2

Hi I could get my session values in my jsp, now I want to compare the session value whether it matches the textbox, if it matches, it will redirect the user to another page else it will remain the same page, I am not sure how to proceed, please help . Much thanks!

JSP

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Factorial</title>
</head>
<body>
<form action="fact" method="POST">
Enter a number: <input type="text" name="num">
<input type="submit"/>
<%= session.getAttribute( "money" ) %>,
</form>
</body>
</html>

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    HttpSession session = request.getSession();

         String text = request.getParameter("money");

         int money = (Integer)session.getAttribute("money");   

         String testing = String.valueOf(money);

    if(text == testing)
    {

    RequestDispatcher rd = request.getRequestDispatcher("MainPage");
    rd.forward(request, response);
}
else
{
  response.redirect("Errorpage.jsp");
}
Programm3r
  • 13
  • 5

1 Answers1

0

Assuming that you have already got a Session attribute called "money", you would not need to access it from JSP

  <body>
    <form action="fact" method="POST">
    Enter a number: <input type="text" name="num">
    <input type="submit"/>
    </form>
  </body>




    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



            HttpSession session = request.getSession();

                 String text = request.getParameter("num");

                 int money = (Integer)session.getAttribute("money");   

                 String testing = String.valueOf(money);

            if(testing.equalsIgnoreCase(text))
            {

            RequestDispatcher rd = request.getRequestDispatcher("MainPage");
            rd.forward(request, response);
        }
        else
        {
          response.sendRedirect("Errorpage.jsp");
        }
        }

If you have not created a Session attribute called "money" yet and want to create it in jsp, you have to use Scriptlet.

        <body>
        <form action="abc.do" method="POST">
        Enter a number: <input type="text" name="num">
        <input type="submit"/>
        <% session.setAttribute("money",1000); %>,
        </form>
        </body>