File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem with forward and include JSP tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with forward and include JSP tag" Watch "Problem with forward and include JSP tag" New topic
Author

Problem with forward and include JSP tag

HASAN SIF
Greenhorn

Joined: Nov 06, 2000
Posts: 7
Below are two examples where we r trying to pass value to next page but showing error in output.
First Example using include tag
<jsp:include page="two.jsp" flush="true" >
<jsp aram name="name" value="amit" />
</jsp:include>
Here is the file "two.jsp"
<% out.println(request.getParameter("name")); %>
Second Example using forward tag
<jsp:forward page="three.jsp" >
<jsp aram name="name" value="amit" />
</jsp:forward>
Here is the file "three.jsp"
<% out.println(request.getParameter("name")); %>
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi hasan,
Your program is nicely working and prints "amit" in my computer. I thing there may be some other problem. If you provide me exact error message and from where(dir) you are running this program, I may able to locate the error.
solaiappan
Humberto Hernandez
Greenhorn

Joined: Nov 01, 2000
Posts: 13
I am confused. My book (core servlets ...) says that you cannot use <jsp:include > in pages that have JSP code inside. However that is exaclty what you are doing.
In "Core Servlets and JSP" page 270 says:
(Speaking about using jsp:include)...On the other hand, the page has already been translated into a servlet by request time, so the including file cannot contain JSP.
Can anyone explain to me why the book says this? Can I include files with JSP content using <jsp:include>?
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi Humberto ,
I thing you are confused with the above example for explaining
"jsp:include" directive.

<jsp:include page="two.jsp" flush="true" >
<jsp aram name="name" value="amit" />
</jsp:include>
Here is the file "two.jsp"
<% out.println(request.getParameter("name")); %>
You have studied that
" the page has already been translated into a servlet by request time, so the including file cannot contain JSP."
the above statement is a not a completed one some thing is left out.
The general format of "jsp:include" action is
<jsp:include page="local URL" flush="true" >
<jsp aram name="name1"
value="value1" />
.................
<jsp aram name="namen"
value="valuen" />
</jsp:include >
the included page can be a static document, servle or another JSP page.
Even though you can include a JSP page there are restrictions apply, what not to use inside those JSP page for e.g It is not possible to forward to another page, cannot set Cookies or Headers.
But it can use "JspWriter" object
e.g out.println(request.getParameter("name"));
I hope it is clear.
solaiappan
srilatha sarma
Greenhorn

Joined: Nov 07, 2000
Posts: 3
Hi,
Could some body please explain me why i am getting null when i tried the above code.
Thanks.
sujatha mamidala
Greenhorn

Joined: Nov 20, 2000
Posts: 19
I am also getting null. SOLAIAPPAN could you please explain me why.
Thanks A Lot.
Suja.
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi all,
I am suspecting that you people might have made some mistake while copying & and pasting because of the "smiling face" appeared above code. Because of that you might have missed the parameter <jsp aram name="name" value="amit" /> check out please.
My code is below:
"one.jsp"
<jsp:include page="two.jsp" flush="true" >
<jsp aram name="name" value="amit" />
</jsp:include>
"two.jsp"
<% out.println(request.getParameter("name")); %>
"forward.jsp"
<jsp:forward page="three.jsp" >
<jsp aram name="name" value="amit" />
</jsp:forward>
"three.jsp"
<% out.println(request.getParameter("name")); %>
run first
"http://localhost:8080/one.jsp"
then
"http://localhost:8080/forward.jsp"
The above is my configuration. change as per yours.
solaiappan

[This message has been edited by P SOLAIAPPAN (edited November 21, 2000).]
sujatha mamidala
Greenhorn

Joined: Nov 20, 2000
Posts: 19
Hi,
Here is my code.
test1.jsp
<jsp:include page="test2.jsp" flush="true" >
<jsp aram name="name" value="amit" />
</jsp:include>
test2.jsp
<% out.println(request.getParameter("name")); %>
when i run the test1.jsp i am getting
"The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed".
so,i changed the name="name1". Now when i run the test1.jsp again i am getting some parser exception.
Then again i had to change the code in test1.jsp like this
<jsp:include page="test2.jsp" flush="true" />
<jsp aram name="name" value="amit" />
</jsp:include>
Now when i run the test1.jsp again "null is getting printed out.
I would appreciate if you could explain me this strange behaviour.

Thanks A Lot.
Suja.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hi Sujatha,
I have tested the code. Both 'include' and 'forward' work fine. They both print 'amit'.
If I am not wrong , I think your code is missing the ':' (colon) in this 2nd line of 'one.jsp' and 'forward.jsp'.

Now everything will be ok. Try it!. I am using Tomcat3.1 on win98.
regds
maha anna

[This message has been edited by maha anna (edited November 21, 2000).]
sujatha mamidala
Greenhorn

Joined: Nov 20, 2000
Posts: 19
Hi Maha,
Thanks for responding. That's exactly how i tried the code.
"jsp aram name="name" value="amit". But it's not working.
Thanks.
Suja.
sujatha mamidala
Greenhorn

Joined: Nov 20, 2000
Posts: 19
I don't know why that smiling face keeps showing up.
<jsp"colon"param name="name" value="amit" />
Maha please explain me what's going wrong here.
Thanks
suja.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Which servlet container are you using?.
1. First try the include alone. (one.jsp and two.jsp)
2. If you are using Tomcat there will be an 'examples' dir under webapps dir. Copy one.jsp and two.jsp to this dir. Mine looks like this :
C:\jakarta-tomcat\webapps\examples\one.jsp

C:\jakarta-tomcat\webapps\examples\two.jsp

3. Type http://localhost:8080/examples/one.jsp
Tell us what happens?

regds
maha anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with forward and include JSP tag