I'm trying to set a parameter that's not allocated to a specific servlet but rather available globally throughout the web application. My current code gets the following as null:
<context-param>
<param-name>example</param-name>
<param-value>This is an example parameter value</param-value>
</context-param>
and
getServletConfig().getInitParameter("example");
Figured it out. Just had to change:
getServletConfig().getInitParameter("example");
to:
getServletContext().getInitParameter("example");
Related
Is there a way to specify the execution order of ServletContextListeners that are defined in web.xml and web-fragment.xml?
I have two listeners in my application:
The first is declared in the web-fragment.xml
<listener>
<listener-class>com.example.RunFirst</listener-class>
</listener>
and a second one in web.xml of my Application
<listener>
<listener-class>com.example.RunSecond</listener-class>
</listener>
additionally my web-fragment contains the following ordering element:
<ordering>
<before>
<others />
</before>
</ordering>
but the RunSecond is still executed before RunFirst. Any idea how i can change this order?
Actually it is not possible at all.
As stated in oracle docs: "The web.xml descriptor is always processed first."
What you could do is create another web-fragment.xml with your RunSecond listener and set the relative order between the fragments using "ordering".
Hope it helps.
Is it possible to use static variables in my project to store data for all Servlets (they are in one .war file) and different requests? (It's not data that belongs to a distinct session)
data for all Servlets
You can use ServletContext for this.
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
For example: in web.xml
<context-param>
<param-name>param</param-name>
<param-value>Myname is web xml</param-value>
</context-param>
In your servlet
public class ParameterServlet extends HttpServlet {
---
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = getServletContext();
name= context.getInitParameter("param");
}
A complete example here.
For Objects
setting
getServletContext().setAttribute("myObj", obj);
getting
MyObj attribute = (MyObj)getServletContext().getAttribute("myObj");
you can access those objects across servlets.
Yes you can do that.
However it is better to define these constants in your web.xml using the <context-param> tag.
Servlets can then retrieve constants defined using the <context-param> tag using the call:
context.getInitParameter()
Example of name-value pairs in web.xml:
<context-param>
<param-name>name</param-name>
<param-value>Joe</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>password</param-value>
</context-param>
in web.xml you can define
<context-param>
<description>My variable</description>
<param-name>variable.name</param-name>
<param-value>value</param-value>
</context-param>
And then access it from Servlet code:
String variable = getServletContext().getInitParameter("variable.name");
Yes you can, the static variable will be accessible for all the servlet threads. But about using the static variable, you should make a proper decision depending on the factors like the life time of the data you want to store and the amount of data you are going to store.
And since it is used in servlets context, make sure that its thread-safe.
How to disable #Webservice loading during start up to save some loading time in xfire?
I have a bunch of services with #Webservice annotation. They are all being loaded during the startup and causing a slow startup. I don't want to load these if I test non-services in dev instance. I am wondering if there is a way to disable this by setting system property or something .
Sure, in your web.xml, where you configure the xfire servlet:
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>-1</load-on-startup>
</servlet>
A negative value for load-on-startup means the servlet is only initialized when the container 'feels like it', and for most containers, this means when the first request comes in.
From the servlet specification:
The load-on-startup element indicates that this servlet should be
loaded (instantiated and have its init() called) on the startup of the
web application. The optional contents of these element must be an
integer indicating the order in which the servlet should be loaded. If
the value is a negative integer, or the element is not present, the
container is free to load the servlet whenever it chooses. If the
value is a positive 128 integer or 0, the container must load and
initialize the servlet as the application is deployed. The container
must guarantee that servlets marked with lower integers are loaded
before servlets marked with higher integers. The container may choose
the order of loading of servlets with the same load-on-start-up value.
So consult the documentation for your web container to make sure this has the desired effect in your case.
I have a authorization managed bean to fetch restriction rules to be applied to the tags within every jsf of the application. The managed bean requires to know the name of the requested jsf on initiation so it can fetch restrictions specific to the tags within that jsf. What is the best way this can be achieved ?
Declare it in web.xml as follows:
<context-param>
<param-name>paramName</param-name>
<param-value>PARAM_VALUE</param-value>
</context-param>
Access it in ManagedBean as follows:
FacesContext.getCurrentInstance()
.getExternalContext().getInitParameter("paramName")
Hope this helps to solve your problem.
You can get init-params defined in web.xml by:
FacesContext.getCurrentInstance().getExternalContext()
.getInitParameter("paramName");
I have some questions about dispatching a request in a servlet.
To sum it up, I deployed a website on a public server which forces me to have a url-pattern like /servlet/* for all my servlets (I heard that it was a default configuration anyway).
The problem is that while developping the application I didn't have such restrictions and therefore didn't built it to support such patterns....now, my application just doesn't work because of the urls. Let's say me servlet is declared and mapped like this :
<servlet>
<servlet-name>MainController</servlet-name>
<servlet-class>controller.controllers.MainController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainController</servlet-name>
<url-pattern>/servlet/MainController</url-pattern>
</servlet-mapping>
The problem is that I use this code in my Servlet :
this.getServletContext()
.getRequestDispatcher(destination).forward(request, response);
The destination parameter is always a *.jsp at the root of my webapp juste like "/index.jsp", "home.jsp", etc.
When I was using my application on localhost my servlet had this url pattern :
<url-pattern>/MainController</url-pattern>
and everything was working fine because the request dispatcher was always searching the .jsp at the root of the webapp. But now with my new url-pattern, it tries to serch all my .jsp at servlet/* just like servlet/index.jsp and, of corse throw me a
HTTP Status 404 - /servlet/index.jsp
I perfectly understand why it's acting like that as, if I recall well, Servlets cannot extend outside their current context.
But my question is, am I doomed ? Isn't there a way to tell to the request dispatcher to go to the .jsp I'm asking without taking care of the "/servlet/*" pattern ?
I absolutely need the request's object because I work with it before forwarding it.
I do really don't know how to get through this, so I'm seeking some help here hoping that someone had already faced this situation or at least have a clearer vision of the situation than me.
Thank's for taking the time to read this and for helping me.
Best regards,
Sampawende.
So, destination doesn't start with / which makes its location to be dependent on the path of the calling servlet?
Fix it accordingly:
request.getRequestDispatcher("/" + destination).forward(request, response);
By the way, if you'd like to prevent direct access to JSP as well (enduser could change the URL to point to the JSP without calling the controller first), then consider placing the JSPs in /WEB-INF folder. Don't forget to change the RequestDispatcher path accordingly: "/WEB-INF/" + destination.