| Author |
Use of getResourceAsStream() ??????????
|
Shanthi Mari
Greenhorn
Joined: Jul 24, 2005
Posts: 24
|
|
I created "site.properties" file in package "com.servlets". Whenever I run the "index.html" I getting the error displayed as "Couldn't open /site.properties" from getProperties()ie it couldn't find the resource.But the file is existing there. package com.servlets; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Properties; // // Interprets requests from input form // public class DispatchDemoServlet extends HttpServlet { protected ServletConfig _config; protected ServletContext _ctx; protected Properties _p; public DispatchDemoServlet() { _config = null; } // Initialize servlet instance public void init(ServletConfig config) { _config = config; // Load properties from file in war _ctx = _config.getServletContext(); _p = null; } // doGet and doPost do the same thing public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doPost(req, res); } // Handle post request public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String command = req.getParameter("command"); if (command.equals("forward")) { doForward(req, res); } else if (command.equals("redirect")) { doRedirect(req, res); } else if (command.equals("include")) { doInclude(req, res); } } // Given a symbolic name of a site, forward to that site protected void doForward(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String name = req.getParameter("name"); // Look up the site by name Properties p = getProperties(); String url = (String)p.get(name); if (url == null) { url = "errorPage.html"; } // Get the request dispatcher -- request will be dispatched // to this URL. RequestDispatcher rd = req.getRequestDispatcher(url); // Forward to requested URL rd.forward(req, res); } // Lazy load of properties protected Properties getProperties() { if (_p == null) { InputStream is = _ctx.getResourceAsStream("/site.properties"); if (is == null) { System.err.println("Couldn't open /site.properties"); } else { _p = new Properties(); try { _p.load(is); is.close(); } catch (IOException iex) { System.err.println("Error: " + iex.toString()); } } } return _p; } }
|
Sun certified Programmer (SCJP1.4)<br />SCWCD
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1008
|
|
You have a basic misunderstanding of the ServletContext getResourceAsStream method. It returns resources relative to the WEBSITE ROOT, not to the package/class you are currently in. Probably you need _ctx.getResourceAsStream("/WEB-INF/classes/com/servlets/site.properties"); alternatively use the ClassLoader.getResourceAsStream, which searches classpath: getClassLoader().getResourceAsStream("com/servlets/site.properties"); or load it as a resource bundle: ResourceBundle rb = ResourceBundle.getBundle("com.servlets.site"); Hope this helps, evnafets
|
 |
Shanthi Mari
Greenhorn
Joined: Jul 24, 2005
Posts: 24
|
|
The structure of my Project is: Servlet Project classes com servlets site-properties public-html src I am not having site.properties Under WEB-INF. can anyone help???
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by Shanthi Mari: The structure of my Project is: Servlet Project classes com servlets site-properties public-html src can anyone help???
Either fix the path in getResourceAsStream so that it is relative to your context root or (as Stefan mentioned) read up on ResourceBundles.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Shanthi Mari
Greenhorn
Joined: Jul 24, 2005
Posts: 24
|
|
I tried with ClassLoader, ResourceBundle and getResourceAsStream() All the three worked . Thanks for your explanation Stefan.
|
 |
 |
|
|
subject: Use of getResourceAsStream() ??????????
|
|
|