| Author |
In struts I am getting an error
|
ramesh kothakonda
Ranch Hand
Joined: Feb 14, 2006
Posts: 40
|
|
Hi ranchers, I placed driver name and url in conn.properties file At the time of retriving that file in Action class I am getting an error (File NOt found exception). I placed conn.properties file and CustomerDAO.class file ,MyAction.class in classes directory still also I am getting an error. I wrote some code in CustomerDAO.java like this : import java.util.*; import java.sql.*; import java.io.*; public class CustomerDAO { public static List getTableData(String str) { List list=null; list=new ArrayList(); try{ Properties p=new Properties(); FileInputStream fis=new FileInputStream("conn.properties"); p.load(fis); String driver=(String)p.getProperty("driver"); String url=(String)p.getProperty("url"); Class.forName(driver); Connection con=DriverManager.getConnection(url); Statement stmt=con.createStatement(); String query= "select * from "+str; ResultSet rs=stmt.executeQuery(query); while(rs.next()) { Customer cust=new Customer(); cust.setCustId(rs.getString(1)); cust.setCustName(rs.getString(2)); cust.setCustFname(rs.getString(3)); cust.setCustAddress(rs.getString(4)); list.add(cust); } return list; }catch(FileNotFoundException e) { System.out.println(e); return null; } catch(Exception e) { System.out.println(e); return null; } } } At the time of accessing the method CustomerDAO.getTableDate("customer"); I am getting an error FileNotFOund exception (for conn.properties). If i give absolute path of conn.propeties file I am getting successfully. Could you please tell me why this error occuring. thanking you , regrads k.ramesh
|
The object oriented way of becoming "RICH" is by INHERITANCE<br />Whoever said money can't buy happiness, didn't know where to shop.
|
 |
Sami Kosonen
Greenhorn
Joined: Sep 14, 2006
Posts: 13
|
|
Try adding '/' before that filename: If I remember correctly, this should help.
|
 |
ramesh kothakonda
Ranch Hand
Joined: Feb 14, 2006
Posts: 40
|
|
thanking you giving response i tried also like that in what way I am getting same error. if you give solvation i will be thank full to you. thanking you k.ramesh
|
 |
Sami Kosonen
Greenhorn
Joined: Sep 14, 2006
Posts: 13
|
|
|
Could you provide the directory structure?
|
 |
ramesh kothakonda
Ranch Hand
Joined: Feb 14, 2006
Posts: 40
|
|
hi, under webapps directory my webapplication name: StrutsProject under this directory : I have some static pages and jsp pages : I have WEB-INF DIRECTORY UNDER WEB-INF DIRECTORY: I have src ,classes, *.xml files ,*.tld files and lib directories Under classes diretory: I have my .class files and conn.properties file and application.properties file. under src folder: i have all .java files . under lib folder: all .jar files are available thanking you k.ramesh
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
The constructor for FileInputStream requires an absolute path. It's not a J2EE class, so it knows nothing about web contexts. If you want to use a properties file that is in your WEB-INF/classes directory without hard-coding the path, put the following code in your Action class: String propertiesPath = getServlet().getServletContext().getRealPath("/") + "/WEB-INF/classes/conn.properties"; The getRealPath("/") method finds the actual file path to your web context root. Once you have this, you can determine any other path relative to the context root. You can then pass this path into your DAO method.
|
Merrill
Consultant, Sima Solutions
|
 |
ramesh kothakonda
Ranch Hand
Joined: Feb 14, 2006
Posts: 40
|
|
hi , thanking you giving response. But I am using conn.properties file in CustomerDAO.java that class is like this: ========================================================= import java.util.*; import java.sql.*; import java.io.*; public class CustomerDAO { public static List getTableData(String str) { List list=null; list=new ArrayList(); try{ "File f=new File("/WEB-INF/classes/conn.properties); String abpath=f.getAbsolutePath(); System.out.println(" abpath: "+propertiesPath); Properties p=new Properties(); FileInputStream fis=new FileInputStream(propertiesPath); p.load(fis); String driver=(String)p.getProperty("driver"); System.out.println(driver); String url=(String)p.getProperty("url"); System.out.println(url); Class.forName(driver); Connection con=DriverManager.getConnection(url); Statement stmt=con.createStatement(); String query= "select * from "+str; ResultSet rs=stmt.executeQuery(query); while(rs.next()) { Customer cust=new Customer(); cust.setCustId(rs.getString(1)); cust.setCustName(rs.getString(2)); cust.setCustFname(rs.getString(3)); cust.setCustAddress(rs.getString(4)); list.add(cust); } return list; }catch(FileNotFoundException e) { System.out.println(e); return null; } catch(Exception e) { System.out.println(e); return null; } } } ============================= File f=new File("/WEB-INF/classes/conn.properties); String abpath=f.getAbsolutePath(); But at this time it is giving an absolute path: c:\WEB-INF\classes\conn.properties it is not correct. in what way i can get absolute path of that properties file . File f=new File("conn.propeties"); in this way also i am not getting absolute path of that file . If you give solution i will be thank ful to you thanking you , k.ramesh
|
 |
Purushoth Thambu
Ranch Hand
Joined: May 24, 2003
Posts: 425
|
|
|
From servlet/action/jsp you can't get the absolute path. You need to specify this a parameter to the Container. You can use context-param in web.xml to specify the absolute path to your webapp directory and use ServletConfig to get the init parameter.
|
 |
 |
|
|
subject: In struts I am getting an error
|
|
|