File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes whats wrong with this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "whats wrong with this code?" Watch "whats wrong with this code?" New topic
Author

whats wrong with this code?

Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57
i have made folder named prop in folder where i have all my jsp files
in prop i have notice.txt file


errorrg.apache.jasper.JasperException: An exception occurred processing JSP page /notice1.jsp at line 19

16: </head>
17: <body>
18: <%
19: FileInputStream fis=new FileInputStream("prop/notice.txt");
20: Properties p=new Properties();
21: p.load(fis);
22: Enumeration e=p.propertyNames();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:407)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.io.FileNotFoundException: prop\notice.txt (The system cannot find the path specified)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:106)
java.io.FileInputStream.<init>(FileInputStream.java:66)
org.apache.jsp.notice1_jsp._jspService(notice1_jsp.java:71)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

Two big problems:
  • There is Java code in your JSP. Bad bad practice.
  • The relative file path is almost guaranteed not to work as web applications have no deterministic concep of a "current directory".


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Jatin sachdev
    Ranch Hand

    Joined: Jan 21, 2010
    Posts: 57
    where to place the text file (in netbeans) then if i want my program to read it???
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61190
        
      66

    Whether you are using Netbeans or not is moot. Where is it placed in regard to the context root?

    When you know that, you can use ServletContext.getRealPath() to get its proper file path.

    And, move the Java code into a Java class where it belongs. It has no business in a JSP.
    Pat Farrell
    Rancher

    Joined: Aug 11, 2007
    Posts: 4655
        
        5

    Jatin sachdev wrote:where to place the text file (in netbeans) then if i want my program to read it???


    A web application reads relative to the context root, which is defined in the WAR file. Usually, you can put it in the
    web/WEB-INF/classes directory and it will be visibile. You still have to reference it as Bear describes
    Jatin sachdev
    Ranch Hand

    Joined: Jan 21, 2010
    Posts: 57
    its running now......i have placed my properties file in classes/com and read it using...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: whats wrong with this code?