Basically I have servlet web-app and I have a directory out the my web-app project say D:\resources, here in my porject web.xml I need to specify that the resources that my web-app would need could be found at D:\resources, I would really appreciate if you tell me how to specify a directory to my project web.xml file.
Thanks in advance
Best way of achieving this is to configre your external resources with defining JNDI in your application server and then referring the same in your web.xml like for e.g.
<resource-description>
<res-ref-name>myAppResources</res-ref-name>
<jndi-name>myResource</jndi-name>
</resource-description>
Related
I'm migrating an old project from a web.xml approach to a complete Java-style Servlet 3.0 configuration.
But I can't understand how to translate part of the XML configuration in Java code. In particular the next snippet:
<jsp-config>
<taglib>
<taglib-uri>....</taglib-uri>
<taglib-location>....</taglib-location>
</taglib>
</jsp-config>
Any hint would be welcome!
As a secondary, more academic, question: do Servlet 3.0 API offer a full coverage of what you could do with XML, or not?
Stefano,
Since JSP 2.0, there is no need in put <taglib> tag in web.xml. From Head First Servlets and JSP book:
The Container automatically builds a map between TLD files and names, so that when a JSP invokes a tag, the Container knows exactly where to find the TLD that describes the tag.
How? By looking through a specific set of locations where TLDs are allowed to live. When you deploy a web app, as long as you put the TLD in a place the Container will search, the Container will find the TLD and build a map for that tag library.
So, all you have to do is to have a TLD file with the correct URI.
Places to put your TLD file:
Directly inside WEB-INF
Directly inside a sub-directory of WEB-INF
Inside the META-INF directory inside a JAR fi lethat’s inside
WEB-INF/lib
Inside a sub-directory of META-INF inside a JAR fi lethat’s inside
WEB-NF/lib
I have a Java web application using Wicket 6, Spring 3.2 and WildFly 8.2.0. Right now i'm setting the context root of my web application in the jboss-web.xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
<context-root>/myCustomContextRoot</context-root>
</jboss-web>
The jboss-web.xml file is compiled into the war. Now some clients want to change this context root to an empty context root. So i hace to recompile a version of my app per different context root. Is there a way to set the context root of my application from outside .war, programatically from a .properties file, or any other way for example in standalone.xml of WildFly 8.2.0?
Set the runtime name when deploying your web application. Suppose your WAR is called myapp-1.0.0-SNAPSHOT.war. Using a runtime name of foo.war, the context root will be /foo.
Using a runtime name of ROOT.war, the context root will be /.
The runtime name can be set when deploying via the Web Console or via the CLI.
Thanks for your Answer Harald Wellmann. It answers the question and pointed me into the right direction!
Some things I had to find out on my own and which may help others:
the exact syntax in jboss-cli to specify a runtime-name is:
deploy path_to_war_file --runtime-name=wantedName.war
This leads to a context-root of /wantedName/ for the webapp.
The runtime-name does not have any effect on the context-root, if the war file contains a jboss-web.xml in WEB-INF which in turn contains a context-root tag.
That is, if you want to control the context-root of your web-app in WildFly at deployment time, you must not specify any context-root in jboss-web.xml.
It is ok to have a jboss-web.xml without a context-root tag if you want to take advantage of the runtime-name to control the context-root.
I tested this on WildFly 9.0.1 and 9.0.2:
Hope this helps!
My structur is like
WebContent
META-INF
WEB-INF
jsp
index.jsp
web.xml
And web.xml file is as simples as can be
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>servlets</display-name>
<welcome-file-list>
<welcome-file>WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
But when I execute it using eclipse in browser i get 404 error, but when I move index.jsp to root directory it works correct.
EDIT:
After changes
jsp
--index.jsp
META-INF
WEB-INF
--web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>servlets</display-name>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
SOLUTION
I found out that my Tomcat server wasn't restarting when I was compiling. Now when tomcat restarts every time I compile servlet it works.
Thank you for your patient
you can't access file inside WEB-INF directly , that is only possible by servlets not by you.
This directory(WEB-INF) contains all resources related to the application that are not in the document root of the application. This is where your web application deployment descriptor is located. Note that the WEB-INF directory is not part of the public document. No files contained in this directory can be served directly to a client.
web.xml must be immediately inside WEB-INF( also not in a sub-directory of WEB-INF)
Also try changing this
<welcome-file>WEB-INF/jsp/index.jsp</welcome-file>
to
<welcome-file>jsp/index.jsp</welcome-file>
UPDATE:
As per your new directory structure, your <welcome-file> tag should be:
<welcome-file>jsp/index.jsp</welcome-file>
Firstly
The web.xml should be inside web-inf
Secondly
the path to index.jsp is incorrect in your web.xml.
As per your directory structure, index.jsp is inside webcontent\jsp\indiex.jsp why are you using path web-inf\jsp\index.jsp
as per your current directory structure it should be .\jsp\indiex.jsp
UPDATE : OP has updated the directory structure.
you should not put jsps in WEB-INF, you should put jsps in webcontent and the web.xml in WEB-INF.
reorganize your directory structure. put the web.xml inside web-inf and jsp folder inside webcontent then inside web.xml put path to welcome jsp as jsp\index.jsp
404 error comes when your URL is not proper.
Check if URL is proper.
Based on your directory structure your URL should be some thing like this : localhost:8080/jsp/index.jsp
Also make sure that web.xml is inside WEB-INF directory.
I created a custom tag library for my struts 2 project. Now I want to put all my source code into a jar file. It's working fine but my concern is I still need to configure my .tld file in the web xml for it to work:
<jsp-config>
<taglib>
<taglib-uri>/custom-tags</taglib-uri>
<taglib-location>/META-INF/mytaglib.tld</taglib-location>
</taglib>
</jsp-config>
Is there a way to get rid of this and configuring it in the jar file instead?
This shouldn't have anything to do with Struts2, really. To get away from configuring this in the web.xml, you should just be able to place your .tld file into a META-INF folder at the root of your jar and declare the uri inside the .tld using <uri>/custom-tags</uri>.
At the moment, the default I think, it copies to
target/myproject/WEB-INF/classes
so when deploying it does not pick up the context.
Also, i want to reference a server specific config file database.properties, I want to put it in tomcat/conf and then reference it in applicationContext.xml, how can I do this ?
Also(2), I am under the impression that this is a fairly standard and decent way to set things up - please correct me if I am wrong.
edit
for the server specific config file I user this
<context:property-placeholder
location="file:${catalina.home}/conf/database.properties"
ignore-unresolvable="true"
/>
If you need to keep applicationContext.xml as a classpath resource, you can configure ContextLoaderListener to pick it from the classpath by adding the following lines to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
It's much easier than configuring Maven to copy it to WEB-INF.
Regarding the second question, you can configure PropertyPlaceholderConfigurer or <context:property-placeholder> to load .properties file from a file system.
For your title question: Often in a .war maven module, you'll put web related resources under src/main/webapp instead of src/main/resources. Then the maven plugin will pick them up automatically because it matches convention. So, move your applicationContext.xml to src/main/webapp/WEB-INF
Another option is to configure the webResources as described in the documentation
For the second question you can look at a PropertyPlaceHolderConfigurer. You'll just have to get the path correct.