Bright
Bright

Reputation: 63

Passing values from Servlet to my JSP

I need to pass the String value h from Servlet to the jsp page. If login is success it will be redirected to success page else it need to display "Invalid login message" in login.jsp page

    if(success)
    {
        RequestDispatcher view=getServletContext().getRequestDispatcher("/Success.html");
        view.forward(request, response);
    }
    else
    {
        String h="Invalid Login";
        RequestDispatcher view=getServletContext().getRequestDispatcher("/Login.jsp");
        view.forward(request, response);
    }

I tried a lot, but its not working.

Upvotes: 0

Views: 12933

Answers (3)

Lightning Boy
Lightning Boy

Reputation: 5

You can store your data either in request or in session.Then access that value from your jsp page by using sessionScope or simply ${data}.

You can set values in request or session by using the code below in your Servlet.

request.setAttribute("data",h);
request.getSession.setAttribute("data",h);

In order to access this value in your jsp page. Add the below code in your jsp page

${data} //if you are storing data in request
${sessionScope.data} //If you are storing data in Session 

Upvotes: 0

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7116

Your else block will be like bellow:

else
{
   String h="Invalid Login";
   request.setAttibute("message",h);
   RequestDispatcher view=getServletContext().getRequestDispatcher("/Login.jsp");
   view.forward(request, response);
}

And in your Login.jsp file you should use:

${message}  //(El expression to access value)

And your message will be displayed.

Upvotes: 2

user2302288
user2302288

Reputation: 305

Try to store your data in some scope

In servlet

   request.setAttribute("Data", h);

in jsp

request.getAttribute("Data") 

Upvotes: 0

Related Questions