TL;DR: <load-on-startup> does not seem to be causing my Jersey web services to load on startup
I’m trying to set up a simple RESTful web service that will act as a registry for the other RESTful services in the container.
To do this I had planned on registering each other web service with the registry service in a static initialize block. Everything was going well until I remembered that servlets aren’t loaded until their first access.
A quick web search turned up the <load-on-startup> tag that can be added to the servlet definition in web.xml. Unfortunately, this was already set in my configuration and adding some logging to the static blocks verified that the classes were indeed not being loaded until the first access of each service.
I’m developing these servlets using Jersey annotations and deploying them to Apache Tomcat (both 7.0.22 embedded in netbeans and 6.0.33 deployed remotely fail in the same way).
Besides this perceived misbehavior, the servlets have been performing correctly.
Any ideas on what I could be doing wrong?
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>CORSFilter</filter-name>
<filter-class>com.foo.bar.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo.bar.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
It was a couple years ago and a couple years of Java Web Stack knowledge later.
I believe this was a case of mistaken identity. I had a non container managed resource that I was trying to get to load on startup. The jersey servlet container was most assuredly loading on start up as the web.xml was specifying. The classes that I wanted to preload were not being referenced by the container itself so were not being loaded with the container.
Related
I'm using Spring Boot on weblogic 10.3.6; however we usually use the embedded tomcat during development.
I'd like to enable a specific Spring profile when on weblogic to accomodate for different configuration/beans to be used when on wl. Since I'm forced to use a web.xml on that ancient version of the application server, I'd like to use web.xml to activate the weblogic profile I need.
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.class.annotated.with.SpringBootApplication</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
So far I've seen suggestion to use
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</context-param>
or
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</init-param>
in the DispatcherServlet configuration.
I even tried configuring an ApplicationContextInitializer, but SpringBootContextLoaderListener provides his own initializer.
Is there a proper way to accomplish what I want? I can't put weblogic configuration in main application.yml because that would break the embedded tomcat configuration we are using for development.
im actually facing a problem related to memory leak using JSF2.2(Mojarra) and Tomcat7, i will proceed to the details:
The webpage contains many datatables, and have some options to select and export the data. Everything is stored in session (i readead all the good practices for JSF but in this case i been forced to use it)... unfortunately all the datatables are obligatory i know that this can consume too much memory, but i try to face it with session-timeout under 15 minutes. But even using it the memory grows to the max allowed of Tomcat7 and never release the memory back to the server, even after hours.
The webpage is actually working OK but with that big memory leak.
What im trying to achive is to release the memory of the sessions that already been timed out to the server again.
I have a little suspect that the problem is related with a WARNING that appears when the session is timed out and you try to do something in the app, on the logs print this:
de nov. 16, 2017 2:55:46 PM
org.springframework.web.context.request.FacesRequestAttributes
registerDestructionCallback WARNING: Could not register destruction
callback
[org.springframework.beans.factory.support.DisposableBeanAdapter#532ecd5b]
for attribute 'mainBean' because FacesRequestAttributes does not
support such callbacks
de nov. 16, 2017 2:55:46 PM
org.springframework.web.context.request.FacesRequestAttributes
registerDestructionCallback WARNING: Could not register destruction
callback
[org.springframework.beans.factory.support.DisposableBeanAdapter#5d5d8e9e]
for attribute 'otherBean' because FacesRequestAttributes does not
support such callbacks
I have a main bean that contains other beans with #Autowire all the beans are scoped in session #Scope=("session"):
mainBean.java
#Component
#Scope("session")
#Qualifier("mainBean")
public class MainBean {
#Autowired
private OtherBeanClass otherBean;
...
}
otherBean.java
#Component
#Scope("session")
#Qualifier("otherBean")
public class OtherBean {
...
}
web.xml of WEB-INF (I readed that i can just add the listener org.springframework.web.context.RequestContextListener but it results as ClassNotFoundException)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>MainApp v3.3.0</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>MyWebApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyWebApp</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>main-css</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
<param-value>/WEB-INF/resources</param-value>
</context-param>
</web-app>
web.xml of Tomcat (Because it is a very big file i put only what i think is related to my problem):
<session-config>
<session-timeout>10</session-timeout>
</session-config>
If you want me to show something more to get a better approach just ask.
Thanks for reading and I hope you can help me.
EDIT: After the kolossus's helping tip
I managed to find the RequestContextListener inside my org.springframework.web-3.0.6.release.jar the correct declaration is:
(notice: .context.request.RequestContextListener and not .context.RequestContextListener)
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Finally my #PreDestroy method is being called, but my server memory still on the max allowed.
To publish one java-web application in tomcat, I copy the project that named 'demo-mvc' to the tomcat's webapps folder.Then I visit "http://localhost:8080/demo-mvc/xx.jsp" in the chorme browser after starting tomcat but it prompts "The requested resource is not available".I tried editing the server.xml as follows
<Context docBase="D:\apache-tomcat-7.0.57\webapps\demo-mvc" path="/demo-mvc" reloadable="true" source="org.eclipse.jst.jee.server:website"/>
Finally it still does no effect.I am confused about where the problem is.
Try to clean your project from Project clean option and make sure you are mapping all your resources
In your web.xml the code should be like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
when I am removing listener part from web.xml my project is running fine. Please let me know why?
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>migration</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
This is a spring web application in eclipse.
ContextLoaderListener is optional. If all you're beans are in the (child) web context (DispatcherServlet) you can remove it.
How it works basically, Spring load a root WebApplicationContext via Spring’s ContextLoaderListener and a child WebApplicationContext via Spring’s DispatcherServlet. This results in a parent-child context hierarchy.
If you don't need anything in the root hierarchie then just keep the DispatcherServlet.
Further reading :
Role/Purpose of ContextLoaderListener in Spring?
ContextLoaderListener or not?
and of course http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
Only if you have two config xml files. Say, SerivesContext.xml, ContextDAO.xml
If you have configured everything in one spring config file you don't need the
ContextLoaderListener, just the dispatcher servlet is sufficient.
If you use different ones , you can configure it as ,
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,WEB-INF/spring-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently. One is the Application Context and the other is the WebApplicationContext.
Purpose of ContextLoaderListener :
ContextLoaderListener is used to initialize Application Context. In this case, you will tell Spring to load all *-.context.xml files from the classpath. There is only one application context per application.
So you can configure this in web.xml as
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
Purpose of contextConfigLocation :
On the other hand, you have WebApplicationContext. WebApplicationContext is the child context of the Application Context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. You can initialize the WebApplicationContext as
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
NOTE : Here name of the XML must be like <servlet name>-servlet.xml. In our case name of servlet is dispatcher therefore, name of XML file is dispatcher-servlet.xml.
I hope this clears you when to use what & why.
I have Vaadin web application with spring security integration for authentication. The configuration of the Vaadin servlet is pretty simple:
<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.example.SpringApplicationServlet</servlet-class>
<init-param>
<param-name>applicationBean</param-name>
<param-value>mainApplication</param-value>
</init-param>
<init-param>
<param-name>widgetset</param-name>
<param-value>com.example.widgetset.CustomWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The servlet initializes the Spring Context and returns the Vaadin application. I have also configured the security for that and have a custom login form configured like this:
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/jsp/login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login_error</servlet-name>
<jsp-file>/jsp/loginError.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login_error</servlet-name>
<url-pattern>/login_error</url-pattern>
</servlet-mapping>
The login form is styled with an external css and there are also some images. Basically the images are located in /jsp/img and the stylesheet in /jsp/login.css. So the WAR structure looks like:
/jsp
/META-INF
/VAADIN
/WEB-INF
Neither the images nor the css gets loaded, because obviously all those requests are mapped to the vaadin servlet. How can I define some static resources directory, which wouldn't be served by the Vaadin servlet? I have tried the spring mvc:resources but that didn't work. Thank you for your help.
Bye,
Filip
I have figured this out. Although it is rather a workaround. I have mapped the Vaadin Application Servlet to something like /app/* instead of to /* (Remember that in this case you also have to map the same servlet to /VAADIN/*). With this configuration I am able to access the jsp directory from my webapp and everything works fine. I have deleted the whole Spring Resources configuration, as this just didn't work.
So once more, I am still pretty not pretty comfortable with this solution and would rather have my RESOURCES dir configured other way, but the client is happy :). If anyone has got the right solution I would appreciate to read it.
Use a url rewrite filter to get more contro on url mapping.
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
then map Vaadin application to /vaadin for example and configure url maping in urlrewrite.xml
<rule>
<from>/styles/**</from>
<to last="true">/styles/$1</to>
</rule>
<rule>
<from>/images/**</from>
<to last="true">/images/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/vaadin/$1</to>
</rule>
<outbound-rule>
<from>/vaadin/**</from>
<to>/$1</to>
</outbound-rule>
EDIT
Other option is put static files in /VAADIN/ directory.
I have figured this out. Although it is rather a workaround. I have mapped the Vaadin Application Servlet to something like /app/* instead of to /* (Remember that in this case you also have to map the same servlet to /VAADIN/*). With this configuration I am able to access the jsp directory from my webapp and everything works fine. I have deleted the whole Spring Resources configuration, as this just didn't work.
So once more, I am still pretty not pretty comfortable with this solution and would rather have my RESOURCES dir configured other way, but the c
Might be late but for who is still having problems with serving static content while using vaadin /* mapping, the solution I found was using apache's default servlet org.apache.catalina.servlets.DefaultServlet, so a web.xml will have something like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>com.ex.myprj.MyUI</param-value>
</init-param>
<!-- If not using the default widget set-->
<init-param>
<param-name>widgetset</param-name>
<param-value>com.ex.myprj.AppWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Static content Servlet</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Static content Servlet</servlet-name>
<url-pattern>/customer/*</url-pattern>
</servlet-mapping>
</web-app>
So in the example above, despite having vaadin at /*, the /customer/* part will be served as static content by the DefaultServlet