• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher inside JSP

 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test.jsp


header.jsp


Can anybody explain me why <h1>Header</h1> doesn't appear inside html source code of test.jsp? It is display on the screen, but when i look to view source for test.jsp, the entire header.jsp file content is missing.
It's weird since, it appears on the screen, but not in source code.

Thanks
 
kurt hanni
Ranch Hand
Posts: 140
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"header.jsp" is called at runtime. just like when using a <jsp:include> tag
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code should work, in theory - it does exactly the same thing a <jsp:include /> would do, and I'm sure that works for you?

I suspect there may be some buffering going on here, since JSPs may declare their own output buffer for the page, while the RequestDispatcher will use the response Writer obtained from HttpServletResponse.

You should use the <jsp:include /> action (and perhaps look at the generated source code from that?) to avoid this problem.
 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies.

Yes, it is supposed to do same thing as <jsp:include ... />.
<jsp:include ... /> works as i expect and header.jsp content appears inside test.jsp view source.
I'm studying for web cert that's why i came with this.
The problem is, the header.jsp is rendered, but not present inside the HTML source, so response object doesn't contain it. How heck the browser render it on the screen if it doesn't know about it?
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: Here in your code you used RequestDispatcher to include the contents,hence in generated servlet code you can have only this
RequestDispatcher view = request.getRequestDispatcher("jsp/header.jsp");
view.include(request, response);
,this will use the response of the included file.



2: Using <jsp:include> action ,this is also include header.jsp at run time ,by making this call in generated java file for jsp. org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "jsp/header.jsp", out, false);

3: and if you use <%@ include file="jsp/header.jsp" %> (include directive) you will find something like this in your generated class file for jsp.
out.write("<img src=\"images/DSC00147.JPG\"> <br>\r\n");
out.write("<em><strong>This contends coming from included file\"header.jsp\"</strong>");
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How heck the browser render it on the screen if it doesn't know about it?
It cannot. It must be in the returned HTML source code. Do a View Source, open in a text editor, then do a find operation and search for the HTML you expect. It must be there.
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try it on a different browser other than what you are currently using? if yes, did the same issue occur?
 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem fixed.
Browser was the problem. Reinstalled it and now the entire source code appears.
Thank to everyone for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic