Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of getResourceAsStream() ??????????

 
Shanthi Mari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Stefan Evans
Bartender
Posts: 1690
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanthi Mari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with ClassLoader, ResourceBundle and getResourceAsStream()
All the three worked .

Thanks for your explanation Stefan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic