Access to ServletContext

I want to read files and directories within the web application. For that I need to know the 'real path' of context directory in which the JSF application is installed.
This information I normally get from the ServletContext:
String filename = getServletContext().getRealPath("/WEB-INF/defs/myfile.xml");
Question:
How do I get the JSF servlet instance from within a backing bean?

FacesContext context =
FacesContext.getCurrentInstance();               
ServletContext servletContext =
(ServletContext)context.getExternalContext().getContex
t();
will work, but I am not sure if it is the right way
of doing things, preferable I would like to just use
the Faces context, but I create a object in a startup
servlet but at this point the faces context does not
seem to in initialised at this point as
FacesContext.getCurrentInstance() returns null. There
must be a better way of doing this????If you are creating the object in a servlet, you have access to the ServletContext via the standard means, leave the FacesContext out of it.

Similar Messages

  • Accessing the ServletContext from a class that is not a Servlet?

    Is there any way of accessing the ServletContext from a class that is not a
              Servlet? The class is being used as part of a Web Application.
              Thanks.
              

    http://www.mozilla.org/mirrors.html
    Mozilla has download mirrors around the globe. If it is on the list, it is trustworthy.

  • How to access ServletContext object of a webapp on remote server?

    Hi!
    I have a webapp, runs on tomcat, loads information from local db server, keeps them in servlet context, and publish these datas to web via jsp pages.
    I make run the same webapp on different location, different webserver and db server with different datas.
    Is it possible to link both ServletContext to each other, or access one from the another?
    What is the right solution, if I want to access all stored Objects of these independent webapplications, summarize and show them in one browser page, in one jsp?
    sorry for my poor english
    and thanks for any help,
    kind regards,
    henpanta

    It is not possible to access another servletcontext glueless using HTTP or any transfer protocol. Would be a potential security hole too. If you want to let communicate 2 webapps on different servers with each other, consider webservices.

  • JMX - Accessing ServletContext in an MBean implementation

    How do you access the ServletContext from a class that implements an MBean interface? My application stores logs in application scope (ServletContext) and I need to access those logs from my MBean implementation.
    public class Stats implements StatsMBean {
    // How do I access ServletContext here?
    }Any ideas?

    How aboutpublic class Stats implements StatsMBean {
      private ServletContext context;
      public Stats(ServletContext context) {
        this.context = context;
      // some methods that can now use the "context" variable.
    }

  • Can beans access ServletContext?

    I want to use my web deployment descriptor (web.xml) to store database connection parameters. Unfortunately, the only way that I know how to access the ServletContext is through the request.getServletContext() method. Since my bean classes don't have a handle on the request object, how can I access the ServletContext to get this information?
    Any good practice advice from anyone?

    Right but I have a bean that I want to have access the servletcontext. From the bean class itself. (not the setProperty tag in JSP).

  • Passing Objects: ServletContext versus JNDI

    I am currently developing a Web application that uses a variation of the (often recommended) model-view-controller architecture. Specifically, I have a single Front Controller Servlet that identifies what the request is for, and then delegates processing to an appropriate RequestHandler object. As implied by the name, the RequestHandler object has access to the original HttpServletRequest object.
    In its init() method, the Front Controller Servlet prepares a pool of database connections. Each RequestHandler will obtain a connection through a DataSource object (in accordance with "best practices" for using the JDBC 2.0 Optional Extension). Then, the RequestHandler will retrieve data from the database, instantiate a Java Bean, and pass the Bean on to a corresponding JSP page for display.
    Here is my dilemma: what is the best way to make the DataSource available to the RequestHandler objects?
    Most sources suggest registering the DataSource with JNDI, but - as far as I can tell - this seems to require "administrative" access to the Web application server. For example, performing this action with Tomcat 4.0 requires modification of the server.xml file (as described at http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html).
    I would prefer to just stuff the DataSource into the ServletContext so that it is accessible throughout my Web application. (Then my DataSource would not be accessible, via JNDI, to other applications on the same server - such as in a shared hosting environment.) However, the problem is that my helper classes - the RequestHandlers - are not Servlets or JSPs (just regular classes), and therefore do not have access to a ServletConfig and, consequently, cannot access the ServletContext. Of course, I could stuff a reference to the ServletContext in every HttpServletRequest before passing it on to the helpers, but that doesn't seem "clean."
    So, to summarize this lengthy inquiry,
    (1) How can helper classes (i.e., not Servlets) access the Web application context (ServletContext)?
    (2) What are the advantages/disadvantages to using JNDI to expose resources, versus simply storing objects in the ServletContext object, when trying to make resources available throughout a single Web application?
    Thanks!
    - Scott
    [email protected]

    Right you deal two problems:
    1 The passing of servletcontext to helper classes. You can avoid this by creating a web application and storing it in a war file. You place all your servlets/jsp/helper classes in the war file according to a given structure. Each war file has a web.xml file. The web.xml file has all mappings between names in the program and the references to the objects. The web.xml can also have references between jndi names and references using the <env-entry> tag. There is only one servletcontext per web application. You can place all the information that both helper classes and servlets use e.g. jndi references into the web.xml. It is possible for the servletcontext and helper classes to refer to the same web.xml.So you have no need to exchange a servletContext reference around. See information on servletcontext interface:
    http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletContext.html
    Second the question of isolating Datasources between applications. Could you not password protect the datasources so that access is only permissible if you have the correct password? Hence you can control the use of the datasource.
    There are a number of tools to create a war application and to add jndi references to a war file.
    You can use the graphical deployment tool from Sun's J2EE implementation.
    All details for creating a war file are: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html
    There are facilites to register environment variables like DataSources using the tool - look in the J2EE tutorial under configuring web clients.
    Or you can use the packager tool found in Sun's J2EE application:
    http://java.sun.com/j2ee/j2sdkee/techdocs/guides/ejb/html/Tools7.html
    You can then port these war files onto tomcat to be hosted.
    You will need to download the J2EE implementation.
    For information on how to structure your web application and create a war file see: http://java.sun.com/j2ee/j2sdkee/techdocs/guides/ejb/html/Tools7.html
    All information to create J2EE components:
    http://developer.java.sun.com/developer/onlineTraining/J2EE/Intro/
    Hope this helps.

  • Retrieve ServletContext from non-servlet class

    A servlet calls a method which is located in another class which is not a servlet. Within this method of the non-servlet class, i need to access the ServletContext of the servlet that has called the method. What i am currently doing is simply passing the ServletContext as a parameter to the method.
    I would like to avoid passing the ServletContext all the time, so i'm wondering if it's possible, from the non-servlet class, to retrieve the ServletContext of the servlet which has called the method of the non-servlet class.

    Thanks J-Fine, that's a smart suggestion. BTW in the meantime i figured out that passing the ServletContext is not that bad idea, after all it reflects the structure of the app. However if i'll change my mind again i'll do like you suggested.

  • JNDI properties not hard coded

    I'd like to sustain the portability of my web application so I don't want to hard code JNDI factory driver or provider URL into my application not even inside jndi.properties file. What are the solutions for not hard coding this information inside web application? Context init properties?

    That was my first approach and that's the solution if I need these parameters inside e.g. Servlet. But now I have one JavaBean functioning as ConnectionFactory that shares connection with all other Servlets and JSP-s but problem is that I don't know how to access the ServletContext of my application (for reading init parameters) from JavaBean (I don't want each time I need to use connection to send the same parameters). So, question is how to access the application init parameters from JavaBean?

  • Help with jsp

    How can one access the servletContext object from within a bean that was instantiated using <jsp:useBean ... />?

    try this:
    <jsp:useBean id="mybean" ... />
    <jsp:setProperty name="mybean" property="servletContext" value="<%= application %>" />
    of course, the bean needs a field to hold it.

  • What is Cross Context session.

    HI,
    What is Cross Context session?
    Iam using Tomcat .
    pls help
    Thanks in advance
    arvin

    Where have u heard about that concept? I don't know it.
    I know about "crossContext" feature of Tomcat which means other applications can gain access to ServletContext of your application (by using the appropiate API), but did know nothing about "cross context sessions"...
    Sorry.

  • How to run berkeley DB in backend

    Hi,
    I have a question on how to run berkeley DB in backend under Linux. I am running a web application using Struts. Upon users' request, the application needs to access BerkeleyDB. Since BerkeleyDB has the cache mechanism, I hope to keep it open in the backend, and when the request comes, it is can be handled with help of cache. I read the manual, but it seems that each time an action is invoked, the action has to open BerkeleyDB and access data. When the action finishes, BerkeleyDB needs to be closed. However, the cache will be lost.
    Basically, what I want is what RDBMS (e.g. oracle) behaves: they run in the backend. Application can use JDBC connect to it, and the cache is always there.
    Is there any way to get around this? or is there other way to leverage BerkeleyDB's cache in web application? I am not sure if the question is too naive, because I am quite new to the web application.
    thanks,
    Jeff

    Hi,
    The best approach is normally to keep the BDB JE Environment open for the lifetime of your web application. To do that, you'll need to save the Environment object as context information after you open it. And don't forget to close the Environment when your web application stops.
    All web frameworks provide some way to save context information between HTTP requests. For example, I'm sure that struts provides a way for you to access the ServletContext.
    This forum is not the best place to ask detailed questions about struts or servlets. I would ask those questions on a struts forum, or even better try to find this information in the servlet / struts documentation.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Accessing ServletContext from Java class Data Control

    I'm writing some business service classes as plain old java classes that will be bound to a JSP using ADF and STRUTS. The business logic requires the real path of the servlet context to access a file. How do I call servletContext.getRealPath("/WEB-INF/test.xml")?
    I know this is mixing web tier code into model code, but I don't know any other way to do this.
    Thanks in advance...

    Ok, it looks like I'll have to define my own action class and pass the pathname on to the data control class.

  • Can a bean access ServletContext?

    Hi!
    Please help me!
    I put initial beans to ServletContext in my servlets init() method.
    How can I get this beans (app scope) from another bean (session scope)?
    Can I reach ServletContext from a class that is not a servlet?
    Thaks!

    Please help me!
    I put initial beans to ServletContext in my servlets
    init() method.
    How can I get this beans (app scope) from another bean
    (session scope)?
    Can I reach ServletContext from a class that is not a
    servlet?Not in a normal JavaBean. You can do it with custom tags, but I guess that is not what you want.
    What you would have to do is create a setter method for the Bean:
    //This is part of the bean in session that requires access to one in servlet context
    public class SessionBeanClass ...
      private ServletBeanClass otherBean;
      public void setOtherBean(ServletBeanClass sbc) { otherBean = sbc; }
      public void getSomethingThatReliesOnOtherBean()
        if (otherBean == null) throw IllegalStateException("This Something can not be done until an object of type ServletBeanClass is assigned.");
        // rest of method
    }Then in the servlet you would need to assign the correct value, preferrably when the bean is first created.
    // somewhere in your servlet
        SessionBeanClass sessionBean = new SessionBeanClass();
        sessionBean.setOtherBean((ServletBeanClass)getServletContext().getAttribute("otherBean"));Or if you are using a JSP:
    <jsp:useBean id="sessionBean" class="my.package.SessionBeanClass" scope="session">
      <jsp:setProperty name="sessionBean" property="otherBean" value="${servletContext.otherBean}"/>
    </jsp:useBean>

  • Accessing ServletContext from non-servlet class

    How i can get information servletcontext from a normal java class?
    I am using Tomcat.
    I have put some Strings in to the servlet context and i want to get this information from a normal java class.
    I think there was a way to do it with getServlet() but this is deprecated.

    One way to do this would be to store the info in some class of your own as a static member(arraylist/hashmap or something) and then retrieve it from your java class.
    May you can populate static field i the init() method of one of your servlet and have that servlet load on tomcat startup.

  • Accessing ServletContext in a Action file

    Hi all,
    I have a servlet which i load on start up and put in the context. Now, i want to retrieve that servlet from the context and use it in a Action file, what should i do?
    The class Action, which every action file extends doesn't have any method to get the servlet context.
    please help,
    Seshu

    You can use request dispatcher to forward the request to the servlet, I guess.Since the request object is available to you in the Action class, u can get the request dispatcher object from the request and use it to forward the request to the servlet.

Maybe you are looking for

  • Image placeholder not working in internet explorer

    I'm using DreamweaverMX and I used the swap image behavior to make thumbnails appear in a larger placeholder below. When I load the site in Firefox the thumbnails come up and when you click them it displays properly below. However when I open it in i

  • F110 Payment program for Singapore Bank DBS

    Hello SAP Experts, Has anyone created Payment Medium Format IDEALu2122 We are banking with DBS (Singapore) and we need to create: 1) Payment file for DBS in Format IDEALu2122 2) SAP interface with DBS Any help would be most appreciated. Many thanks M

  • What is a restrictions passcode

    I don't know where to find the restrictions passcode to reset my ipad and return to Apple to repair a cracked screen. This lack of knowing the passcode is holding me up!

  • Urgent: How to modfy the Report program Corresponding to a Infoset

    Hi Friends, I have done an infoset. I have used TCURR table for Query. I saw the Standard program of that Query having select statement like Select single * from Tcurr where Gdatum = RDD. Where RDD is the value having Statndard date where Gdatum is C

  • Temp tablespace usage coming in Negative values

    Hi, We got the error in our alert.log stating "unable to extend temp segment by 128 in tablespace" TABLESPACE_NAME FILE_ID BYTES_USED/1024/1024 BYTES_FREE/1024/1024 TEMP1 2 622 878 TEMP1 3 -4826 5326 TEMP1 1 414 2586 TABLESPACE SIZE MB USED MB FREE M