Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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

replied

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

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question