4

Is it possible to pass value of variable from Servlet in to JSP. Say i have a JSP something like welcome.jsp and I have some variable initialized in welcome.java servlet.

When I run welcome.jsp it should take value from welcome.java and display it on page.

When I set the RequestDispatcher and forward it its still going to show the servlet URL.But I want the URL to be JSP file.But the Variable I display in JSP file should be taken from servlet.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Java Beginner
  • 1,635
  • 11
  • 29
  • 51

2 Answers2

4

You can archive it using session attributes.

.java

request.getSession().setAttribute("attrName", "value");

request is instance of javax.servlet.http.HttpServletRequest

.jsp

${attrName}

To get more about session attributes, read docs. You can set attribute of any type.

request.getSession().setAttribute("attrName", new ArrayList());
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
0

You can put the variable in Session and redirect the user to welcome.jsp page.

Apurv
  • 3,723
  • 3
  • 30
  • 51