0

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Bright
  • 63
  • 2
  • 6
  • 13
  • 4
    You tried "a lot"? Like what did you try? Did you try setting it as a request attribute? That's the standard approach. – Dave Newton Apr 30 '13 at 13:12
  • yeah i tried in .jsp page as <%=request.getAttribute("h")%>.. am i wrong..? – Bright Apr 30 '13 at 13:15
  • 1
    yes, you are wrong, because you didn't set any attribute named "h". see the answer bellow! – Sazzadur Rahaman Apr 30 '13 at 13:23
  • you can also use the session to store information on... `reguest.getSession().setAttribute("Attribute","Value");` and in jsp use `value = session.getAttribute("Attribute").toString();` – MaVRoSCy Apr 30 '13 at 13:23
  • As a side note, related to your `<%=request.getAttribute("h")%>`. Read here: [The use of scriptlets is highly discouraged](http://stackoverflow.com/a/3180202/814702) – informatik01 Apr 30 '13 at 18:52
  • Servlet: boolean success=s.Login(d); if(success){ RequestDispatcher view=getServletContext().getRequestDispatcher("/Success.html"); view.forward(request, response); } else { message="Invalid Password"; request.setAttribute("message", message); RequestDispatcher view=getServletContext().getRequestDispatcher("/Login.jsp"); view.forward(request, response); } Jsp: <%=request.getAttribute("message")%> ... Its showing null at the jsp before clicking submit button and i need to clear this null.. Help pls – Bright May 01 '13 at 13:42

3 Answers3

2

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.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

Try to store your data in some scope

In servlet

   request.setAttribute("Data", h);

in jsp

request.getAttribute("Data") 
user2302288
  • 305
  • 1
  • 5
  • 17
0

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 
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228