how to load more than one xml bean files in spring IOC - spring-ioc

how to load more than one bean xml configuration file in springIOC container(either in BeanFactory or in AppilcationContext).

You can specify multiple config files when constructing your application context:
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"context1.xml",
"context2.xml","context3.xml"});

If it is a web application and if you are using WebApplicationContext then in your web.xml you can specify xml files under tag like below
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext1.xml
</param-value>
</context-param>

Related

spark deployed in OpenShift - resource not found

I've deployed a simple hello world spark maven app to OpenShift using git push. But when I try to call the url, it gives a HTTP 404.
In the localhost, it worked fine, though.
Here's my web.xml, it's based from Spark's doc (http://sparkjava.com/documentation.html#other-webserver):
<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"
metadata-complete="false">
<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.api.lwt.HelloWorld</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I'm really new to deploying java. So maybe I'm missing something. Any ideas?
How to deploy a spark Java web app?
Here it's mentioned that you have to exclude jetty libraries that come with spark as dependencies.Is that helpful?

How can I change the homepage address when I deploy my application in production and hosting server?

I am working on a Spring web application. In my localmachine, the address I was using to see the home page is localhost:8080/ProjectName/mvc/template. But now in production if some one enter www [dot] myWebsite [dot] com I want them to take to the home page. How can I fix this?
This is how my web.xml looks
<web-app version="2.2" id="WebApp_ID">
<!-- <display-name>Archetype Created Web Application</display-name> -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appContext.xml</param-value>
</context-param>
<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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/mvc/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/template.jsp</welcome-file>
</welcome-file-list>
</web-app>
If you are using tomcat you should set your deployment's context path.
First, open your tomcat web application manager and undeploy default tomcat page which path is "/".
Go deploy section in that page,
Write your context path "/".
And add your war file url to WAR or Directory URL box.
Deploy it.

Spring MVC static contents auto reload issue

Right now, I am using Spring MVC to build RESTful web service.
After web server started, index.html page are shown in browser.
But If I change the content of index.html, I must restart web service in order to see the update.(I have cleared browser cached, it's not worked either.)
Is there any way that I just need to refresh web page to get the update after I changed the content of index.html?
Do I need any configuration or code? By the way, I use Jetty as my web server.
Any information will be appreciated. Thanks a lot.
Below is my configuration files.
web.xml
<display-name>Spring MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>disableCacheFilter</filter-name>
<filter-class>com.springapp.mvc.setting.DisableCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>disableCacheFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
mvc-dispatcher-servlet.xml
<context:component-scan base-package="com.springapp.mvc"/>
<mvc:resources mapping="/**" location="/app/" />
<mvc:annotation-driven/>
You need to do a hard refresh, ctrl-f5 in windows. And you turn off caching for websites in you browser dev tools.
To confirm it is browser caching, you simply view the static file in the server's deployed dir.
Or if you are talking about hot deployment, that is ide specific - in intellij you must start in debug mode and use an exploded war. Eclipse/netbeans can't remember how you do it, but they can.

Cannot create resource instance in Tomcat7

I'm trying to configure connection pool in Tomcat 7. Here is code:
part of server.xml:
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource
name="jdbc/testDataSource"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/delta_server"
username="test" password="test"
validationQuery="select count (*) from session_contexts"
/>
web.xml configuration:
<resource-ref>
<description>
Sample JNDI DataSource resource reference
</description>
<res-ref-name>jdbc/testDataSource</res-ref-name>
<res-type>java.sql.DatSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and access from jsp page:
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:/comp/env");
conn = (Connection) envContext.lookup("jdbc/testDataSource");
But unfortunately I get exception:
javax.naming.NamingException: Cannot create resource instance
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
org.apache.naming.NamingContext.lookup(NamingContext.java:826)
org.apache.naming.NamingContext.lookup(NamingContext.java:145)
org.apache.naming.NamingContext.lookup(NamingContext.java:814)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
How can I fix it?
Thank you.
Your configuration looks pretty much OK (except for some typos in your web.xml file; the <res-type>java.sql.DatSource</res-type> should be <res-type>javax.sql.DataSource</res-type>).
But the problem I think is the fact that you are declaring the database resource inside the server.xml file.
Normally the application resources should be declared in the context.xml file of the application and only be declared in server.xml if they are shared between applications. So my suggestion is to declare the jdbc/testDataSource resource in your context.xml file. That should be one way to make it work.
If you must absolutely have a global resource then in your context.xml file you must add a resource link to it or it won't be visible by default.
This context is distinct from the per-web-application JNDI contexts described in the JNDI Resources HOW-TO. The resources defined in this element are not visible in the per-web-application contexts unless you explicitly link them with <ResourceLink> elements.
So the second way to make it work is to leave the resource declared in server.xml but then add something like this in your context.xml file:
<ResourceLink global="jdbc/testDataSource"
name="jdbc/testDataSource"
type="javax.sql.DataSource" />

Replacing a website on a Tomcat Server with a static HTML website

I made a small static website for my client and now they want me to replace their present dynamic website with the static one. They have Ubuntu with SSH installed on the remote location. Their existing website is running on a Tomcat6 server and the site root is in "/var/lib/tomcat6/webapps/ROOT/".
My website consists of just static HTML pages. How can I reconfigure/ replace the present website with the one I made? Should I just stop the server and replace the files in the site root with my files?
Adding the updated web.xml:
<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>Welcome to OneLearn</display-name>
<description>
Welcome to OneLearn
</description>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>linegraph</servlet-name>
<servlet-class>com.FlexiApps.graphs.LineGraphServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>linegraph</servlet-name>
<url-pattern>/linegraph</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>piechart</servlet-name>
<servlet-class>com.FlexiApps.graphs.PiechartServlet</servlet-class>
</servlet>
<servlet-mapping>
ssawqfxz<servlet-name>piechart</servlet-name>
<url-pattern>/piechart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>com.FlexiApps.utils.welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/log-1.0</taglib-uri>
<taglib-location>/WEB-INF/lib/taglibs-log.tld</taglib-location>
</taglib>
</jsp-config>
<listener>
<listener-class>
org.apache.commons.fileupload.servlet.FileCleanerCleanup
</listener-class>
</listener>
</web-app>
Even after adding a welcome-file and adding the suggested servlet mappings, tomcat doesn't seem to detect any new files added to the ROOT folder.
I found a work-around to my problem:
I installed Tomcat6 on my Eclipse in Windows.
I created a Dynamic Web Project.
Put all my static content in the WebContent folder.
Ran the server to verify everything is in order.
Exported a WAR file from the project, checked "Optimize for a specific server runtime" option, runtime being "Apache Tomcat v6.0".
I cleaned up the /var/lib/tomcat6/webapps/ROOT folder on the ftp linux server and reset all other settings to default.
Extracted the WAR file in ROOT folder.
Restarted tomcat6 using: /etc/init.d/tomcat6 restart
I could successfully see my static website under "http://myIP:8080/"
I wanted tomcat6 to work without this port number. The following link was very useful: http://bhou.wordpress.com/2012/03/09/how-to-install-and-configure-tomcat-6-in-ubuntu-server/
My static website could be navigated to by typing in "http://myIP/"
Eclipse generated web.xml:
<?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" id="WebApp_ID" version="2.5">
<display-name>StaticWebsite</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
http://www.java-only.com/LoadTutorial.javaonly?id=26
This blog seems to provide a step by step instructions on serving static content using tomcat.
If its the only site running on the server the easiest way is as mentioned to copy your files in the same location and to name the start site with the same name as their startsite, in this case you dont need to change the configuration.
Otherwise check this link: Tomcat 6: How to change the ROOT application
EDIT (from here):
The contents of the default Tomcat home page comes from the ROOT webapp servlet called org.apache.jsp.index_jsp. The page that you see in $CATALINA_HOME/webapps/ROOT/index.jsp has been precompiled into a class file (org.apache.jsp.index_jsp.class) stored in a JAR file (catalina-root.jar) in the ROOT webapp's WEB-INF/lib directory. Because of this servlet, Tomcat will not look at the contents of the ROOT web application's index.jsp file if you change it.
The easiest way to change the contents of the index.jsp page is to remove this index_jsp servlet from the ROOT webapp. Once you remove the index_jsp servlet and restart Tomcat, Tomcat will see the index.jsp file in the ROOT directory and compile it on the fly into a class file. You now will be able to edit the ROOT/index.jsp file and have those changes take effect immediately by reloading the "http://localhost:8080/" page.