I read about bootstrapping here. How can I setup bootstrup file in my web application written with jsf,spring and hibernate.Is it necessary to setup bootstrup file in my application?
Java web applications are "bootstrapped" by the web container in which they are run (e.g. Tomcat) and you don't have to do it yourself.
However, if you want to add additional operations to be executed when the application is started up (and/or clean-up operations to be executed when the application is shut down), the servlet API provides the "context listener" mechanism.
Basically, you have to create a class that implements javax.servlet.ServletContextListener which has 2 methods, contextInitialized and contextDestroyed, that are executed when the application is started up, respectively shut down.
You must then add configure this class in web.xml, with something like that :
<listener>
<description>My Context listener</description>
<display-name>My Context listener</display-name>
<listener-class>
com.acme.myapp.MyContextListener
</listener-class>
</listener>
(Or in JEE6 you could use the javax.servlet.annotation.WebListener annotation instead of XML)
Google is you friend for the details, but here are some links to start with :
http://www.roseindia.net/servlets/ServletContextListener-example.shtml
http://docs.oracle.com/javaee/5/tutorial/doc/bnafi.html
Related
I have made a web application to be deployed in the Websphere server but i came up with a problem.
I have 20 servlets that use a common parameter so i have this declared on web.xml:
<context-param>
<param-name>filePath</param-name>
<param-value>C:\logs.txt</param-value>
</context-param>
I want this parameter to be easily edited in the Websphere console but doesn't work. I know this works on Tomcat but is there anything equivalent on websphere?
Thanks
You should edit web.xml only on the server, there is know interface for it in the Console, as I remember.
You can find this file here:{WAS_ROOT}/profiles/profilename/config/cells/cellname/applications/enterpriseappname/deployments/deployedname/webmodulename
Link to the documentation:
http://www-01.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/tweb_jsfengine.html
I'm using jetty container for application which needs certain attributes to be bound to request. I'm using RequestContextListener in web.xml:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
User class:
#Scope("request")
class User {
// some code
}
When deploying war on jetty, it fails with exception : java.lang.IllegalStateException: No thread-bound request found. And on issuing any calls to the server, it results in error 503. On the other hand, it works fine on tomcat. Tomcat gives errors on deployment but subsequent calls go through fine. Looks like it is an issue with jetty. I'm using stable-9 version of jetty (http://download.eclipse.org/jetty/ : jetty-distribution-9.2.10.v20150310) Any suggestions on how do resolve this with jetty?
Saw same issues in others queries, but did not find the answer, hence posting it afresh.
Created a sample Helloworld service in Rest and deployed it in Tomcat-Jersey. Not using Maven.
Injecting a spring bean define in applicationContext.xml which is present in WEB-INF directory.
Deployed the service in Tomcat within Eclipse.
But when I execute the rest service, cannot find file applicationContext.xml.
Once I copy the file to WEB-INF/classes only, it works.
What is the way to make it work by keeping it only in WEB-INF?
web.xml :
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Rest service code snippet :
ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
It seems reasonable to work when you drop the context descriptor under /WEB-INF/classes because this location is the root of classpath.
So try loading with below uri (without prefix) if you want to drop your applicationContext.xml under WEB-INF folder:
ctx = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
Otherwise, can you explain why are you trying to load your applicationContext.xml within your web application?
Alternatively, you can load your application context programatically as follows:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
BR.
If you are using Intellj IDEA and having problem with ApplicationContext.xml path;
Go to File and click Project Structure. Under the Project Settings, click Facets, at the Web Section you will see Web Resource Directories. You should define Web Resource Directory to your /WEB-INF folder. If your /WEB-INF folder is in the subpath like below;
/web/WEB-INF/...
Then choose /web directory to cover all your files.
In Spring I have slf4j with log4j to resolve logging.
I put relative path in log4j.xml configuration, but when i execute app in Netbeans Tomcat and independent Apache Tomcat, relative path is different, and I must change manually.
My idea is obtain context realpath from Spring Controller and set it in log4j configuration in execution time. But I dont know...
How can I change file path param of log4j from Spring Controller?
Two suggestions for you to try:
Add a WebAppRootListener to your web.xml - this will configure a system property pointing (default to webapp.root, but you can customize using a context-param - see the Javadocs link) to the root of your web application, which you can then use in the log4j.properties/xml file:
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener<listener-class>
<listener>
<!-- log4.appender.File=${webapp.root}/logs/web-app.log -->
Or use the Log4jConfigListener in your web.xml (which ultimately delegates to a Log4jConfigurer) - this is similar to the above, but allows you to define a custom log4j configu file and also allows for your web application to monitor the log4j config file for changes made at runtime and automatically update your loggers:
<context-param>
<!-- Configure Log4J from the following config file location -->
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener<listener-class>
<listener>
I would also recommend you read the Javadocs for the above in detail - there are some gotchas with regards to deploying multiple webapps in Tomcat and sharing of system properties, but this can all be worked around (providing a custom key for each webapp, rather than the default ${webapp.root}
Im currently working on a grizzly, spring and jersey project and i have encountered:
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)
... 32 more
Based on the stacktrace (and also by the results when i google), i should register a listener in the web.xml
<web-app ...>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>
So my question is, how will i register the listener to the grizzly server given that i dont have a web.xml?
Without seeing more of your code, my guess would be to use a ServletHandler - check out the example source in the top of the Javadocs for this class:
org.glassfish.grizzly/servlet.ServletHandler
A normal spring application would have a listener registered for context startup, and then the dispatch servlet for most other things, so you should be able to amend the example code to do this.
Something like (totally untested):
sa.setServlet(new org.springframework.web.servlet.DispatcherServlet());
sa.addListener(org.springframework.web.context.ContextLoaderListener.class.getName());
sa.addContextParameter("contextConfigLocation", "beans.xml");
sa.setServletPath("/*");