0

I give u an example to get what i mean. I have a simple site with just 2 pages. The main page includes another page (let's say that the page included is the page with the site menu).

I have to do some dynamic operation in that menu, so i want to control that menu by using a servlet.

After i did some "stuff" in the servlet i want to tell the servlet to "redirect itself" to a jsp page and print, on that page (that is included by the main page), what i passed from the servlet to the jsp page.

I can't do it with this code

request.setAttribute("var", var)
request.getRequestDispatcher(destinationPage).forward(request, response)

because, of course, all my site would be redirected to the page "destinationPage" and not just the include page!

Instead, is it correct to use this code in the servlet?

request.setAttribute("var", var)
request.getRequestDispatcher(destinationPage).include(request, response);

Can i handle many includes with the code above?

What's the right way to handle many includes in a jsp "main" page?

Thank you

MDP
  • 4,177
  • 21
  • 63
  • 119
  • use ajax to do this stuff. – Nirav Prajapati Jul 10 '14 at 12:58
  • Im studying java and jsp, i have to use java to learn! – MDP Jul 10 '14 at 13:00
  • well, IMO, the "right way" to use includes in JSP is to add repetitive static content such as headers and footers. If you want to reuse "components" (usually, forms), it's better to work with taglibs. – Leo Jul 10 '14 at 13:20
  • See [Including Content in a JSP Page](http://docs.oracle.com/javaee/1.3/tutorial/doc/JSPIntro8.html), [Include another JSP file](http://stackoverflow.com/questions/9110148/include-another-jsp-file), [What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files?](http://stackoverflow.com/questions/14580120/whats-the-difference-between-including-files-with-jsp-include-directive-jsp-in) – Nivas Jul 10 '14 at 13:37

2 Answers2

0

I assume :

  • you have JSP pages
  • you want to include one page in other pages (say it is the menu)
  • you need java dynamic operations to control the included page

You can of course include a servlet in the menu jsp, but that servlet code should not redirect. It simply set request attributes and return, to let the calling jsp do its that part.

But IMHO you should not use a servlet for that. If it only needs initial request and response, you should use a filter that has access to the request and response (and can set request attributes that will be used in all servlets and jsp). And in any other case, you can access static methods of an utility class, or use a ServletContextListener to put an utility object in servlet context attributes and use it from any of your jsp pages :

<% UtilityClass.putMenuElementsIntoRequest(...); %>

or

<% application.getAttribute("utilityObject").putMenuElementsIntoRequest(...); %>

or even (say Foo is one of String, List<String>, ...)

<% Foo foo = application.getAttribute("utilityObject").getFoo(...);
   pageContext.setAttribute("foo", foo) %>

and you can use ${foo} in your jsp file.

That way you are not limited to the service method and can pass arbitrary objects.

But if you want really to do serious things ... use frameworks like Apache Tiles to manage layout, Struts or Spring MVC for your controllers and hide your jsp views under WEB-INF ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

i had to do a project where I was required to do the same stuff!!This is what i came up with: You can load another jsp in you main jsp inside an "iframe".Now whenever you click something in the menu(some hyperlink like <a href="">/<a> ) set the target of that hyperlink to that iframe.

For eg you have an option in your menu as "Edit Profile" and you want to first call a servlet "EditProfileServlet" which will in turn dispatch the request to you EditProfile.jsp which will display the necessary stuff.

For this you can just use this:

<a href="EditProfileServlet" target="right_iframe">Edit Profile</a>

Here, "right_iframe" is the frame which will load your view. Inside the Servlet you can either use a RequestDispatcher or sendRedirect to forward the request to the required JSP.

Anmol
  • 303
  • 2
  • 14