[jsp] Pass variables from servlet to jsp

How can I pass variable from servlet to jsp? setAttribute and getAttribute didn't work for me :-(

This question is related to jsp servlets

The answer is


It will fail to work when:

  1. You are redirecting the response to a new request by response.sendRedirect("page.jsp"). The newly created request object will of course not contain the attributes anymore and they will not be accessible in the redirected JSP. You need to forward rather than redirect. E.g.

    request.setAttribute("name", "value");
    request.getRequestDispatcher("page.jsp").forward(request, response);
    
  2. You are accessing it the wrong way or using the wrong name. Assuming that you have set it using the name "name", then you should be able to access it in the forwarded JSP page as follows:

    ${name}
    

Use

request.setAttribute("attributeName");

and then

getServletContext().getRequestDispatcher("/file.jsp").forward();

Then it will be accessible in the JSP.

As a side note - in your jsp avoid using java code. Use JSTL.


Simple way which i found is,

In servlet:

You can set the value and forward it to JSP like below

req.setAttribute("myname",login);
req.getRequestDispatcher("welcome.jsp").forward(req, resp); 

In Welcome.jsp you can get the values by

.<%String name = (String)request.getAttribute("myname"); %>
<%= name%>

(or) directly u can call

<%= request.getAttribute("myname") %>.

Besides using an attribute to pass information from a servlet to a JSP page, one can also pass a parameter. That is done simply by redirecting to a URL that specifies the JSP page in question, and adding the normal parameter-passing-through-the-URL mechanism.

An example. The relevant part of the servlet code:

protected void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
    response.setContentType( "text/html" );
    // processing the request not shown...
    //
    // here we decide to send the value "bar" in parameter
    // "foo" to the JSP page example.jsp:
    response.sendRedirect( "example.jsp?foo=bar" );
}

And the relevant part of JSP page example.jsp:

<%
    String fooParameter = request.getParameter( "foo" );
    if ( fooParameter == null )
    {
%>
    <p>No parameter foo given to this page.</p>
<%
    }
    else
    {
%>
    <p>The value of parameter foo is <%= fooParameter.toString() %>.</p>
<%
    }
%>

You can also use RequestDispacher and pass on the data along with the jsp page you want.

request.setAttribute("MyData", data);
RequestDispatcher rd = request.getRequestDispatcher("page.jsp");
rd.forward(request, response);

If you are using Action, Actionforward way to process business logic and next page to show, check out if redirect is called. As many others pointed out, redirecting doesn't keep your original request since it is basically forcing you to make a new request to designated path. So the value set in original request will be vanished if you use redirection instead of requestdispatch.


When using setAttribute and getRequestDispatcher on doGet, make sure that you are accessing your pages with the urlPatterns ("/login" for example) defined for your servlet. If you do it with "/login.jsp" your doGet won't get called so none of your attributes will be available.


This is an servlet code which contain a string variable a. the value for a is getting from an html page with form. then set the variable into the request object. then pass it to jsp using forward and requestdispatcher methods.

String a=req.getParameter("username");
req.setAttribute("name", a);
RequestDispatcher rd=req.getRequestDispatcher("/login.jsp");
rd.forward(req, resp);

in jsp follow these steps shown below in the program

<%String name=(String)request.getAttribute("name");
out.print("your name"+name);%>