0

I am working on a JSP project. I have a page that calls another JSP.

Now the problem is, how to pass or use a variable in the called JSP page in its calling page?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
RAVITEJA SATYAVADA
  • 2,503
  • 23
  • 56
  • 88
  • 3
    I don't get it. The previous page is not accessed anymore, so you don't get to execute any code there. – Bozho May 13 '11 at 10:05

3 Answers3

1

Your design should be

  • take one input param on page1.jsp
  • post it to some servlet , process it there, forward request to page1 jsp and pass param taken from page 1 as attribute

See Also

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
1

You can save the variable in the HTTPSession or ServletContext object.

And in the calling JSP page, use or check session attribute for the variable.

session.setAttribute(objectId, Object); to set the variable.

session.getAttribute(objectId); to get the variable.

Ankit
  • 2,753
  • 1
  • 19
  • 26
  • 1
    also use `session.removeAttribute(objectId);` after the variable is not needed. – Harry Joy May 13 '11 at 10:10
  • @Ankit: You must go for session scope when needed, not for every other thing. That might be the reason you got downvoted. But I'm not sure, that was just my idea. And of course, what you suggested is indeed a possibility. – Adeel Ansari May 13 '11 at 10:56
  • @Adeel : Yes you are but I answered keeping in mind that he can also use that varable in all the jsp pages where needed. And as harry Joy mentioned in comment it can be removed. But your point is also right. – Ankit May 13 '11 at 11:09
  • @Ankit: Give a man a fish and he will eat for the day. Teach a man to fish and he will eat for life. – Adeel Ansari May 14 '11 at 19:13
1

I have a page that calls another jsp page

The problem lies here in this sentence.

Try to follow MVC. Use JSP just for rendering the View, and Servlet as a Controller.

Here, simple.souther.us, you find simple and awesome tutorials for newbies.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133