0

:) So I've bought my hosting finally, and I am trying to deploy my tomcat project on it. Server is mochahost.com, JSP and Tomcat support is enabled. My project runs fine on local machine, however when I deploy it on web-server it says:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /List/index.jsp:38

35: <br>    
36: <%
37: String file = application.getRealPath("/") + "default_list.txt";
38: FileReader filereader = new FileReader(file);
39: BufferedReader br = new BufferedReader(filereader);
40: String eachLine = br.readLine();
41: while (eachLine != null) {

What can be wrong? My guess is that it cant access the file, but how do I fix it? Thanks!

Update: problem was caused by server pathing, solved by using Server.MapPath method.

BObov
  • 140
  • 1
  • 10

2 Answers2

1

Right, this is a problem with trying to use file paths in web apps: you can't know where you'll be deployed.

You have a few things you can try:

  1. Write the real path out to the log so you can see it. How else can you know what to do?
  2. Read files from the CLASSPATH using getResourceAsStream() from the servlet context.
  3. "default_list" suggests that this is reference data. You might try embedding it as JSON or XML since it's constant.
  4. You shouldn't be using scriptlet code in JSPs. Have a servlet pass that data to the JSP.
  5. Learn JSTL. You'll be glad you did.
  6. Put the reference data in a database and read it from there.
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks! But what you mean by #4? What are scritplets and where are they in my JSP page? – BObov Mar 08 '12 at 10:32
  • Scriptlets are anything in a JSP that's between <% ... %> tags. They've a late 90s technology that have fallen out of favor. They're hard to read and maintain, and they make it easy to put things in JSPs that don't belong there. All they should be doing is rendering the data they're given. Another object, like a servlet, should be getting that default text data and giving it to the JSP to render. – duffymo Mar 08 '12 at 10:38
  • I see, thanks alot! But I am using 2 scriplets on my JSP page to output my text file. How else can I display it on same page without using scriplets? thanks again =) – BObov Mar 08 '12 at 10:47
  • I've already answered this above: write a servlet that passes the data to the JSP. – duffymo Mar 08 '12 at 10:57
  • Ah, i see. But how do I send parameters from servlet to JSP? – BObov Mar 08 '12 at 11:03
  • http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets7.html#63090 – duffymo Mar 08 '12 at 13:05
0

The program breaks on the line 38. That means that the problem is in the path that you are getting from the getRealPath method. I suggest that you analize what path are you getting with this method and if it makes sense (that is, if the txt file you are trying to get is found in that path).

Th0rndike
  • 3,406
  • 3
  • 22
  • 42