1

I am writing a class in JSP to retrieve a bunch of config values from an XML file. My plan is to have a class "XMLConfig" that loads in the values from the file, and then uses access methods to get at the values in the config object.

My problem is that i cannot seem to call application.getRealPath() from within the class, since eclipse tells me that "application cannot be resolved". I suspect that I must change "application" to something else but I am unsure what.

My code for the class:

<%!
//Config object
public class XMLConfig {

 public boolean loadConfigFile(String strName) {
  String XMLfileName = application.getRealPath(strName);
  try {
   DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   Document doc = null;
   doc = db.parse(XMLFileName);
  }catch(Exception e)
  {
   System.out.println(e.getMessage());
   return false;
  }
  return true;

 }
}
%>
rsp
  • 23,135
  • 6
  • 55
  • 69
Kris M
  • 45
  • 1
  • 4
  • 2
    Defining a class inside a JSP is bad practice. I strongly recommend you rewrite this as a servlet. – skaffman Jul 21 '10 at 09:54
  • I can't get this code highlighting to work. Anyone give me a hand? – Kris M Jul 21 '10 at 09:56
  • Using `getRealPath()` is not highly recommended either as your webapp may not actually map to a real path on the file system depending on your servlet container. – krock Jul 21 '10 at 09:56
  • I see, could you suggest a viable alternative? Also, purely out of interest why does application.getRealPath() work outside of the class declaration, but not inside it? – Kris M Jul 21 '10 at 10:01
  • Because the application object is provided in the JSP context, our class is a sub-context. You can "fix" this by passing the application object as parameter to the `loadConfigFile()` method. (btw, `getRealPath()` is also present in the `ServletContext` interface.) – rsp Jul 21 '10 at 10:07
  • @rsp: `application` **IS** an instance of `ServletContext`. – BalusC Jul 21 '10 at 11:48
  • @BalusC: Oops, lol. I seemed to remember that JSP's had both application and context passed into the page. – rsp Jul 21 '10 at 16:03
  • @rsp: maybe you're confusing it with `pageContext` which is an instance of `JspContext`. – BalusC Jul 21 '10 at 16:15

2 Answers2

1

application isn't a global var. If you want to use it in your method then you'll need to pass it as a parameter.

Not sure why you're defining the class within the jsp though instead of just creating a 'normal' java class.

objects
  • 8,637
  • 4
  • 30
  • 38
  • Thanks for that tip. I read up on importing my own java classes and now have the class in a separate .java file. The getRealPath() problem is not really an issue anymore, but this was very useful. – Kris M Jul 21 '10 at 10:30
0

That's a job for a servlet instead of JSP. Create a class which extends HttpServlet and implement the doGet() method as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String strName = getOrDefineItSomehow();
    Document doc = loadConfigFile(getServletContext().getRealPath(strName));
    // Do whatever you want with it and then display JSP page.
    request.getRequestDispatcher("/WEB-INF/config.jsp").forward(request, response);
}

Map this servlet in web.xml on an url-pattern of for example /config and invoke it by for example http://example.com/context/config. It'll run the code in doGet().

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555