Pass variables from servlet to jsp

JspServlets

Jsp Problem Overview


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

Jsp Solutions


Solution 1 - Jsp

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}
    

Solution 2 - Jsp

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") %>.

Solution 3 - Jsp

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.

Solution 4 - Jsp

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>
<%
    }
%>

Solution 5 - Jsp

You could set all the values into the response object before forwaring the request to the jsp. Or you can put your values into a session bean and access it in the jsp.

Solution 6 - Jsp

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);%>

Solution 7 - Jsp

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);

Solution 8 - Jsp

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.

Solution 9 - Jsp

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeveloperView Question on Stackoverflow
Solution 1 - JspBalusCView Answer on Stackoverflow
Solution 2 - JspBalaView Answer on Stackoverflow
Solution 3 - JspBozhoView Answer on Stackoverflow
Solution 4 - JspTeemu LeistiView Answer on Stackoverflow
Solution 5 - JspMarc-Christian SchulzeView Answer on Stackoverflow
Solution 6 - JspSamson SunnyView Answer on Stackoverflow
Solution 7 - JspRushabh ShahView Answer on Stackoverflow
Solution 8 - JspMateus SarmentoView Answer on Stackoverflow
Solution 9 - JspJunKimView Answer on Stackoverflow