Link to home
Start Free TrialLog in
Avatar of stevedave
stevedave

asked on

Trying to use <jsp:include to include a text file I get by using request.getParameter

Here is my code:

<%@ page import="java.util.*" %>

<%
String theFile = request.getParameter("file");
%>
<html>
<head>
      <!-- MODIFY THE PAGE TITLE -->
      <title>JSP Template - Modify this TITLE</title>
      <script language="JavaScript" src="/pub/sso.js"></script>
      <script language="JavaScript">useCSS();</script>
</head>
<body>
      <script language="JavaScript">beginDoc("<%= app %>");</script>
      <table width="100%" valign=top height=400>
      <tr>
      <td colspan=2>
            <center><h1><em>The Software Factory</em></h1>
            <font color=red size=+2>This Page is not part of the<br>SWF Internal Site<br></font>
            <font color=red size=+2>SWF Data Definition Language</font>
            </center>
      </td>
      </tr>
      <tr>
      <td valign=top width="500", height="375">
            <form>
                  <textarea cols=60 rows=30><jsp:include page="<%=theFile%>" flush="true" /></textarea>
            </form>
      </td>
      </tr>
      </table>

      <script language="JavaScript">endDoc("<%= app %>");</script>
</body>
</html>
<%
}
catch(Exception e) {
throw e;
}
%>

We have an generic error page created that is displayed if a page ever has problems.  This is the page that is being displayed in the textarea when the page loads.  For the variable, that is working because I can print it out to the screen.  It just won't insert the variable into the jsp include statement.  It almost works, just something small.

Nate
Avatar of abuimad
abuimad

Do the file your trying to include reside in your web application or inside another web application in the same server?
There may be problems with the theFile. If the problem is not with the main page, it may be with the included page. Try accessing the theFile from your browser directly.
Instead of the <jsp:include statement, try

<% request.getRequestDispatcher(theFile).include(request, response); %>

note that theFile as to be constructed as a relative path, ie '/my_file.jsp' if it resides in the base folder of your webapp.
please show us the "theFile" value that u get from the getParameter() method.  What is shown in the textarea currently ? a java error ? if so, post the error to us.
Avatar of stevedave

ASKER

the file is located in the same directory as the jsp.  I can directly open the file by browsing to it, so I know it has the ability to be seen.  The problem with the error is that the place where I work implements an error page that is generic.  It prints that anytime a person gets an error, no matter what the error is.  So that is what shows up inside the textarea, not the actual error.  I can get around it, but not sure how it will affect the page.  I will try a few things and post some more information.  thanks.
I can use out.print(theFile) after using getParameter() and it will print out /ddl.txt which is the name of the file.  So it is getting the name correctly.  I have tried it with both the /ddl.txt and just ddl.txt with no slash.  I tried to get around the error page and have it print out the error, and it didn't print anything, just an empty box.

it is a problem with the variable. not sure if I am not formatting it correctly or what.
If I run the page now, I don't get anything in the box.  When I view source, this is what I get.

<html>
<head>
      <!-- MODIFY THE PAGE TITLE -->
      <title>JSP Template - Modify this TITLE</title>
      <script language="JavaScript" src="/pub/sso.js"></script>
      <script language="JavaScript">useCSS();</script>
</head>
<body>
      <script language="JavaScript">beginDoc("ssoint");</script>
      <table width="100%" valign=top height=400>
      <tr>
      <td colspan=2>
            <center><h1><em>The Software Factory</em></h1>
            <font color=red size=+2>This Page is not part of the<br>SWF Internal Site<br></font>
            <font color=red size=+2>SWF Data Definition Language</font>
            </center>
      </td>
      </tr>
      <tr>
      <td valign=top width="500", height="375">
            <form>
            <textarea cols=60 rows=30>



It stops right before the jsp include statement.                                    
ASKER CERTIFIED SOLUTION
Avatar of hisiva
hisiva

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am sure that there were some exceptions thrown on the server side. Pl. check the log or use the above code to get the error message.
I tried to use your debug tool above and it just showed up blank.  It didn't give me any error, but the page didn't show up either.

I just don't understand why it is having problems.  Everywhere I check, everything seems to be exactly right.  Is there anything I should be including?

Thanks for all your help.  If no one can answer this, I'll split the points up between everyone.
I tried to just make a very basic document that all it did was display a word using jsp tags and then do the jsp include and it would not work.  It would display the word and then it would hit the jsp include and just stop.  I have no clue why this would be, but thanks for everyones help.
Did you check the server log? which sever are you using?

I don't think it is good to award points when the problem is not solved. This would make the question to PAQ status and reduce the number of experts viewing the problem.
Could someone post a basic jsp document that would sit in the same directory as my ddl.txt that works for them to do an include.  Then I will copy it directly and see if it works for me.  That way I at least know I can do it on my system.
Avatar of rrz
I am curious to know if you tried changing the name of your file.
Try changing ddl.txt to ddl.jsp  just to see if include will work.
Here is the working code that I tried with Tomcat,

myjsp.jsp

This is myjsp.jsp -1
<BR>

<%
      String theFile = "ddl.txt";
%>

<jsp:include page="<%=theFile%>" flush="true" />

</BR>

This is myjsp.jsp -2


ddl.txt

This is ddl.txt


This is the result I got,

This is myjsp.jsp -1
This is ddl.txt
This is myjsp.jsp -2

HTH
what server u r using ? i tested on Tomcat 3.2.2, the include code is work !

<textarea cols=60 rows=30><jsp:include page="<%=theFile%>" flush="true" /></textarea>