cancel
Showing results for 
Search instead for 
Did you mean: 

Determine current directory/folder of JSP file in a CR Web Project.

Former Member
0 Kudos

Afternoon all.

Very sorry if this is a stupid question or wrong forum but am at my wits end and finally thought I would ask help.

I am very new to Java and Eclipse and Crystal Reports but have been given task of creating Reports that

are accessible via the web. Ill try explain my situation but forgive me if I get terminology wrong and all that.

I have got my report working (well sort of, still learning lots), the one point I am stuck on is referring to a

text file which holds my database connection details (username, passwordu2026)

I do not want to use an absolute path for the location of this text file (Conn.txt). I want to refer to Conn.txt

depending on its location in relation to the directory which holds my webcontent. (C:\Tomcat\webapps\Grasslands)

I use a function to extract the contents of my file into an array.


getContents(u201CC:\\Tomcat\\webapps\\Grasslands\\Conn.txtu201D)

It works fine. The problem is that I donu2019t know for certain that the Grasslands folder (which contains all the

jsp, rpt u2026 files) is going to be in the Tomcat\webapps folder.

Basically if I know for a fact that the Conn.txt file will always be in the same directory as the JSP file

which loads the report, how do I determine what directory the JSP file is located in.

I have tried using:


getContents(System.getProperty("user.dir") + "\\Conn.txt")

but that always points to the Tomcat bin folder.

I tried using a system variable - PATH but could not work out how best to go about this.

Also I am not confident this is the best way.

Surely there must be a way to determine which folder my JSP file is located in?

Again I apologise if this is a question already asked else where but Im getting desperate and google can

only help so much

Thanks all.

Cheers

Darren

Edited by: Darren Jackson on Sep 9, 2008 5:05 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Examples:

You can put the following into web.xml which needs to be under \webapps\yourapp\WEB-INF folder.

<code>

<context-param>

<param-name>JDBCDriver</param-name>

<param-value>oracle.jdbc.driver.OracleDriver</param-value>

</context-param>

<context-param>

<param-name>DatabaseDLL</param-name>

<param-value>crdb_jdbc.dll</param-value>

</context-param>

</code>

You need to write a servlet to access the parameters and pass them to the JSP if it's needed as:

<code>

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException

{

request.setAttribute("DBDriver", getServletContext().getInitParameter("JDBCDriver"));

getServletContext().getRequestDispatcher(

"/Dispatcher.jsp").forward(request,

response);

}

</code>

Where Dispatcher.jsp is the jsp used to open CrystalReportViewer and consume all connection info.

wish it helps.

Dennis

Former Member
0 Kudos

Thanks Dennis,

I will try working thorugh what you have given me.

Its not exactly what I was looking for but might be an alternative. I probably should not have meantioned

that the txt file holds the connection details. I am really wanting to work out how to access any txt file with

any information in it.

From what you have mentioned am I correct in understanding that you can hold any variables (might not be

correct choice of words) in the web.xml file and then I should be able to access these values with that

bit of script you supplied?

It might take me a little while to work through your code and determine what its doing and then how I can

use its concept else where.

I know working things out is better but could you give me more of an example of how I would use your

doGet() function that would be great. As I said I am still learning to walk and am teaching myself so there

are still basic concepts that I have more than likely over looked.

Would you be able to explain exactly what the doGet is doing. What info it needs to run correctly and what it will produce.

If it is too difficult to explain or will take up too much time dont worry. I know you not here to do my work and am very happy to try work it all out myself.

Thanks again for your help.

-


I have just been thinking about your root folder comment.

What you said is exatly what I thought but I dont know how to refer to the root folder in code.

As I mentioned I tried using System.getProperty("user.dir") but that points to the eclipse home folder when testing in eclipse and then the Tomcat bin folder when running it via tomcat. Is this a variable for the root folder similar to "System.getProperty("user.dir")" which will tell my servlet to look in the root folder.

Cheers

Edited by: Darren Jackson on Sep 10, 2008 12:19 PM

Former Member
0 Kudos

Darren,

Yes, you can put any values into web.xml and access with the code.

For a web app, the server side folder/path can only be accessable to the server side code (servlet). The JSP pages are client side code.

Let me work u through the process:

The following code is used to invoke a servlet (GetDbConectionInfoServlet) which gets all propertis regarding database connection info defined in web.xml (originally from your conn.txt)

GetDbConnectionInfo.jsp

<code>
<br>
<form method="post" action="GetDbConnectionInfoServlet" name="myForm">
<input type="submit" value="Open">
</form>
</code>

The following code is used to display the db connection info.

DisplayDbConnectionInfo.jsp

<br>

<form method="post" name="myForm">

<br>Database Driver: <%=request.getAttribute("DBDriver") %>

<br>Database Host: <%=request.getAttribute("DBHost") %>

</form>

The following code is for GetDbConnectonInfoServlet. you need to compile it and put the class into WEB-INFO\classes folder or jar it into a jar file and put the jar file under WEB-INFO\lib folder of your web app.

public class GetDbConnectionInfoServlet extends HttpServlet

{

public GetDbConnectionInfoServlet()

{

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException

{

request.setAttribute("DBDriver",getServletContext().getInitParameter("DBDriver") );

request.setAttribute("DBHost",getServletContext().getInitParameter("DBHost") );

getServletContext().getRequestDispatcher(

"/DisplayDbConnectionInfo.jsp").forward(request,

response);

}

/**

  • Process the HTTP Post request

*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException

{

doGet( request, response);

}

}

Basically GetDbConnectionInfo.jsp invokes GetDbConnectionInfoServlet to get properties defined in web.xml and GetDbConnectionInfoServlet put all properties as request attributes and forward the request to DisplayDbConnectoinInfo.jsp which displays all values.

Wish it helps.

Dennis

Former Member
0 Kudos

Darren,

Answer your question for the second part.

The properties "user.dir" pointing to the default home folder of current login user.

For a web app, usually the folder is refering to virtual folder. For instance when you type in http://localhost/myApp/ in a browser, it doesn't have to mean you have folder named myApp exists. You can assign any folder to be "myApp" on the machine your web server is running on.

Did you try:

getContents("/Conn.txt");

or in your servlet,

getContents(getServletContext().getRealPath("/Conn.txt"));

getServletContext().getRealPath("/Conn.txt") will return "http://serverIP/yourApp/Conn.txt"

Not sure what getContents does.

Dennis

Former Member
0 Kudos

Denis,

Thanks so much for that. I have managed to work out everything you have posted.

Basically I need to start learning from the begining again, seems I have missed out on some of the basic concepts.

I have fulled my web.xml with all the details and now am able to retrieve all that info and will be able to get anything else from their in the future. It works perfectly.

All I need to do now is learn how best to use servlets and try have everything structured properly.

Thanks again for your help.

Cheers

Darren.

Answers (1)

Answers (1)

Former Member
0 Kudos

As a web app, its path alwasy start from its root. For instance under tomcat \webapps\yourApp, this is the root foloder for your application by default.

Since your application is a web application, why don't you use system properties to manage your db connection info? You can put your properties into web.xml and access them as system properties.

For CR reports, you can use CRConfig.xml to define the relative foler for your reports.