How can I Locate the correct filepath in a jsp page?

In http://localhost:8080/myweb/a.jsp, I will call a java bean file which is locate in http://localhost:8080/myweb/WEB-INF/classes/haha/DBConnection.class. This java file requires to read an external file which locates in http://localhost:8080/myweb/WEB-INF/classes/haha/db.txt
My question is how can I get the correct filepath in this environment?
inputStream = new BufferedReader(new FileReader(????????));
Only this will work
inputStream = new BufferedReader(new FileReader("D:\Program Files\Apache Software Foundation\Tomcat 5.5\db.txt"));
But no one will place the file here, and it is impossible to do this when upload to 3rd party hosting.
Any suggestion? thx.

Thx all, in a jsp page, calling this method is fine.
ServletContext s = getServletContext();
String a = s.getRealPath("/");
In a Servlet file, below method is ready to use:
String b = getServletConfig().getServletContext().getRealPath("/");
My problem is, I have a jsp page, which initialize a java bean , this java file is responsible for Database Connection, which will read the external text file to get the login, password and database name. So I won't need to recompile this file every time when I place in different platform with different configuration. I only need to edit the text file is ok.
But How can I get the absolute file path when that jsp page initialize that java bean file? Below is my error, any suggestions are thx.
package sql;
import java.sql.*;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBConnection extends HttpServlet{
public String sql = null;
public Connection con = null;
public Statement statement = null;
public ResultSet rs = null;
private BufferedReader inputStream = null;
private static ArrayList<String> arr = new ArrayList<String>();
  public DBConnection(){
    String fs = System.getProperty("file.separator");
    ServletContext s = getServletContext();
    String realpath = s.getRealPath("/");
    String filepath = realpath + "WEB-INF"+fs+"Properties"+fs+"db.txt";
    try{
      inputStream = new BufferedReader(new FileReader(filepath));
      String l;
      while ((l = inputStream.readLine()) != null) {
        arr.add(l);
      Class.forName("com.mysql.jdbc.Driver");
      this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+arr.get(0)+"?user=" +arr.get(1)+ "&password="+arr.get(2)+"&useUnicode=true&characterEncoding=utf-8");
      this.statement = this.con.createStatement();
    }catch (Exception e){
      System.err.println(e.getMessage());
    }finally{
}java.lang.NullPointerException
     javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)
     sql.DBConnection.<init>(DBConnection.java:20)
     html.ShowCategory.<init>(ShowCategory.java:17)
     org.apache.jsp.left_jsp._jspService(left_jsp.java:66)
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Similar Messages

  • How can I enlarge the graph in a BI JSP Page?

    I create a BI JSP Page, the graph is too narrow, so not enough wide. How can I change the sizew of the graph?
    Thanks in advance.

    Read this:
    http://help.apple.com/pages/ipad/2.2/#/tana29e86157

  • How can i pass the same parameter from 1 jsp page to many jsp page?

    hello.....
    how can i actually pass a parameter from 1 jsp file to many other jsp files? pls help......
    thanx

    Hi
    Save the information which u want to pass to various JSP's inside Session Object.
    There is an Object called HttpSession and u can store information inside which other jsp pages within the same domain and same browser instance can use it.
    commands which are for ur interests are
    <Session Object>.setAttribute("<object name>", <value>);
    Example
    HttpSession objsession =request.getSession(true); String strName="NEED_HELP":
    objSession.setAttribute("Name", strName);
    And then u can retreive that information in your n pages.
    Read any JSP/Servlet Books and u will know about them
    Bye.

  • I have one apple ID for both my Iphone 4S and Ipad. My Ipad was stolen from me and when I checked the iCloud, it can only locate the Iphone 4S. How can I locate the Ipad device?

    I have one apple ID for both my Iphone 4S and Ipad. My Ipad was stolen from me and when I checked the iCloud, it can only locate the Iphone 4S. How can I locate the Ipad device?

    Welcome to the Apple Community.
    You can only locate your device when it is logged into iCloud and 'Find My Phone' is enabled, additionally the device will need to be switched on and connected to a wifi or cellular network.
    Unfortunately, you cannot activate iCloud or 'Find My Phone' remotely.

  • Whenever I update my iPhone software, it asks me to sign in to iCloud with an old email address.  My other devices all have the correct address.  How can I get the correct address for my iPhone?  The only Apple ID that works for logging in is my new one.

    Whenever I update my iPhone software, it asks me to sign in to iCloud with an old email address.  My other devices all have the correct address.  How can I get the correct address for my iPhone?  The only Apple ID that works for logging in is my new one.

    To change the iCloud ID you have to go to Settings>iCloud, tap Delete Account, provide the password for the old ID when prompted to turn off Find My iPhone (if you're using iOS 7), then sign back in with the ID you wish to use.  If you don't know the password for your old ID, or if it isn't accepted, go to https//appleid.apple.com, click Manage my Apple ID and sign in with your current iCloud ID.  Tap edit next to the primary email account, tap Edit, change it back to your old email address and save the change.  Then edit the name of the account to change it back to your old email address.  You can now use your current password to turn off Find My iPhone on your device, even though it prompts you for the password for your old account ID. Then go to Settings>iCloud, tap Delete Account and choose Delete from My iDevice when prompted (your iCloud data will still be in iCloud).  Next, go back to https//appleid.apple.com and change your primary email address and iCloud ID name back to the way it was.  Now you can go to Settings>iCloud and sign in with your current iCloud ID and password.

  • How can i locate the resource file in the weblogic?

    hello
    in struts, i can leave some parameter in a resource file (such as "Application.properties")outside of my code,and get them in the "Action" class by the "getResource" method.
    in my data access tier,i still want to use such a machanism but i can't use the "getResource()" method,for example,there is a ConnectionPool class,i want to read some parameters from a resource file ("param.properties") that is under the same directory as the ConnectionPool class,the following is the code snippet:
    public static void init(DataSource ds){
    myself=new ConnectionPool(ds);
    java.util.Properties prop=new java.util.Properties();
    try{
    java.io.FileInputStream fin=new java.io.FileInputStream("./param.properties");
    prop.load(fin);
    }catch(Exception e){
    e.printStackTrace();
    System.out.println(prop.get("help.create"));
    the "ConnectionPool" class is in the "com.jhtop.task.forum.db.util" package,the "param.properties" is in the same package (com.jhtop.task.forum.db.util),
    but when i execute the program,it tell me that it can't find the file.if i use the absolute path,it works.
    i wonder how can i locate the file path in the weblogic,or what's my current path,or how may i setup my work directory?
    in tomcat,my current directory is bin directory,how about weblogic?
    who can help me?thank you!

    My english is bad and I'm not sure I've well understood your problem.
    How have you created your file ?
    Why do you put it in such a directory and not an other ?

  • I have downloaded a music file through a downloader . But the file is not found in the captured in music player . How can I locate the physical location and move the file . I cannot do via itunes also .

    I have downloaded a music file through a downloader . But the file is not found in the captured in music player . How can I locate the physical location and move the file . I cannot do via itunes also .

    Exactly what do you mean by a 'downloader'?
    Thanks
    Pete

  • How can i locate the properties files within a ejb container?

    when i develop servlet+javabean structure application,in order to imploement decoupling,i
    would like to write the config information in the properties files(key-value pair),in
    servlet,i use "getResourceAsStream(String relativePath)" method to retrieve the
    configuration information from the properties files(i even write the SQL clause
    in the files),it alwsys works well.
    now,i want to implement such function within the ejb container,that is to read
    a properties file from session bean,but i don't know how can i locate the file
    within the ejb container by using relative path,i wonder if there is the same
    method within ejb container as "getResourceAsStream(.......)" method within servlet
    container?
    thanks for any helps!
    the code snippet is appreciated!

    In general, you should look at using environment entries (variables) in the
    deployment descriptors (both the war and EJB jar rather than properties
    files for configuring J2EE applications. The reasons for this are many:
    1. This is the official way to do it according to the spec. Properties
    files are the J2SE way of doing things
    2. As you note, that it's not obvious how you would (legally) read a
    properties file inside an EJB.
    3. It's consistent between the web and EJB part of your code
    4. the weblogic console and tools have good capabilities to edit these
    fields.
    Kent
    "zhebincong" <[email protected]> wrote in message
    news:[email protected]..
    >
    when i develop servlet+javabean structure application,in order toimploement decoupling,i
    would like to write the config information in the propertiesfiles(key-value pair),in
    servlet,i use "getResourceAsStream(String relativePath)" method toretrieve the
    configuration information from the properties files(i even write the SQLclause
    in the files),it alwsys works well.
    now,i want to implement such function within the ejb container,that is toread
    a properties file from session bean,but i don't know how can i locate thefile
    within the ejb container by using relative path,i wonder if there is thesame
    method within ejb container as "getResourceAsStream(.......)" methodwithin servlet
    container?
    thanks for any helps!
    the code snippet is appreciated!

  • How can I locate the file /Library/Keychains/FileVaultMaster.cer to reset my master password? Thanks!

    How can I locate the file /Library/Keychains/FileVaultMaster.cer to reset my master password? Thanks!

    The files that weren't inside the media folder on the original machine need to be copied over to exactly the same paths as they had on the source machine. See this thread for an ongoing discussion of a similar problem. See also this post on migrating the iTunes library.
    tt2

  • How can I locate the original .CR2 /  Raw file from Photos in finder?

    How can I locate the original .CR2 /  RAW file from Photos in the finder window? I would like to know a easier way to open my files and use them in Photoshop than having to go to the Pictures folder, right clicking "show packaged contents", and manually searching for my CR2's / RAW's. It is also ridiculous to have to export the RAW file to my desktop, and then take it into Photoshop or another photo editing program.  I was able to do this in iPhoto and find I cannot do so in Photos. Please help? Can you please consider adding a right click function that says "Show Original file in Finder". That would make it so much easier and better for those of us who want a simple way to access the original files and use them in other editing programs. At least with iPhoto you could go to the top menu / preferences and find your items.

    http://www.apple.com/feedback/macosx.html is the place for feature requests and feedback
    Rather than use the 'Show package Contents route, the supported and safer way is to export the unmodfied original.

  • Hi, can you help?  My emails that I have received show the date received as either today, yesterday or July 28.  How can I get the correct date received?

    Hi, The emails that I have received show the date received as either today, yesterday or July 28.  How can I get the correct date received?

    That's very scary. They might do it, but I personally have never had Apple contact me re id's except on their site when signing in. I suggest you DO NOT respond until you have a phone conversation with Apple security. They can verify if they sent you the e-mail and why.
    While it's very possible it's true,again, I suggest you not respond until you speak to someone at Apple. I've often read here that when people sign in they are not allowed to because "someone else has used the id" type of statement.
    Also, you should be able to go to the app store and see what apps you have downloaded.
    Though it could have been a free app, have you checked to see if any were bought with your id account?
    I've gotten these types of e-mails from places like USPS and FedEx which look amazingly authentic and were from their website. I also received an odd e-mail from my cousin in Ireland last year that clearly was not from him. It was also sent to others in his family in Ireland. I e-mailed him that I only opened it because it had a .ie, but suggested he change... Days later I received mail from 'him' - same name, but at yahoo.com. 
    Again, I would call Apple. You can also report it at [email protected], but I would not settle for that given your situation. If nothing else, call Apple sales (sales depts. anywhere answer fast) and ask them to transfer you to security. Apple will not want anybody accessing their name, and while it's unlikely, it's not impossible that it is someone phishing.
    Hope this helps and can you let the support community know your results by posting on this thread when you're done? Thank you.
    Hope this helps.

  • Found an Ipod touch--how can I locate the owner?

    I found an Ipod touch--how can I locate the owner?  The Ipod requires a passcode.

    You can't. Turn the iPod into your local police department, or the station closest to where you found the iPod. They have resources for identifying and contating the owner, should they wish to take that time.
    Regards.

  • How can I use the "Correct camera distortion" filter and process multiple files in PSE 11?

    How can I use the "Correct camera distortion" filter and process multiple files in PSE 11?

    Did you check the help page for Correct Camera Distortion and Process multiple file
    Correct Camera Distortion: http://helpx.adobe.com/photoshop-elements/using/retouching-correcting.html#main-pars_headi ng_5
    Process multiple files: http://help.adobe.com/en_US/photoshopelements/using/WS287f927bd30d4b1f89cffc612e28adab65-7 fff.html#WS287f927bd30d4b1f89cffc612e28adab65-7ff6

  • HT201472 How can I locate the last location of my iPhone 5s because it is dead and cant be located offline?

    How can I locate the last location of my iPhone 5s because it is dead and cant be located when its offline?

    Apple does not offer a user-searchable log of where the phone has been. Not saying they don't keep one, but I think you'd need a search warrant or work for the NSA or FBI to get access to such data.

  • How can i get the footer bar to stay, whilst page scrolls?

    how can i get the footer bar to stay, whilst page scrolls?
    at the moment, on my site, if the page is longer than the screen, it goes behind the footer,
    but the foot bar moves up the page as yo scroll down to see the rest of the content.
    any way of getting the footer to stay put, while body/page contant scrolls ?
    thanks

    You can use some css to do it...
    #footer_id {
         position:fixed;
         bottom:0px;
    <div id="footer_id">Your footer content</div>
    The css above will lock your footer to the bottome of the browser window. As long as your footer <div> is last in your html, it will float over the top of everything on your page when the page is scrolled. If it's not last in the html, you'll need to add a z-index to get it on top of your other content in the stack.

Maybe you are looking for

  • Item category and pricing type

    Dear All, I have an requirement of special Sales order from end user. In our business scenario, while creating invoice, we click on update button (conditions tab) and choose pricing type B to get the present/updated price. But now  we have some speci

  • Mail 2.0.7 - Earthlink Passwords Rejected

    Hello, I would appreciate any help anyone can give with this problem. My Mail app worked fine last night with my Earthlink accounts; today, I receive repeated "password rejected" messages. I checked with an Earthlink online rep who had me re-enter my

  • Copy Multi block field value in single field

    HI! I have a multi block text item i want to copy its contents in single filed text item. 1. I am using Forms 6i. Thanks & Regards!

  • SRM - Contr Vs GOA , product cat.

    Hi Gurus, Can anyone please help me understand the following : ( I am using extended classic scenario) 1. <b>Can't we replicate contract to backend as that of GOA</b> ?     What is the actual difference between contract and GOA and in whcih case both

  • Install printer on a new lap top

    i need to install  hp office jet 6600 into my new dell inspiron 5735 and i don't know how to do that, please help!! susie