I have created a war file for my web application.I deployed it on Tomcat,it is working fine. My question is that if I have 3 jsp files like index.jsp,one.jsp,two.jsp, what if I want to run one.jsp first(not index.jsp) when I am running war file. Presently index.jsp is running primarily.I am using netbeans,is there any option in netbeans???
I think you can set that in web.xml in the WEB-INF folder by setting the welcome-file-list
normally it looks like
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
change it to
<welcome-file-list>
<welcome-file>one.jsp</welcome-file>
</welcome-file-list>
Related
I am working with eclipse and I run my project with Apache Tomcat.
I have the following files:
1. applicationContext.xml
2. dispatcher-Servlet.xml
3. web.xml
4. pom.xml
I also have the following packages
controller, dao, impl, model.
I would like my homepage to be "MyHomePage". So when I run the project my first url will be : http://localhost:8080/MyHomePage.
In which of the above files/packages I have to define this mapping?
If you meant http://localhost:8080/MyHomePage.html
then you specify the home page in web.xml like this
<web-app>
....
<welcome-file-list>
<welcome-file>MyHomePage.html</welcome-file>
</welcome-file-list>
</web-app>
You will also need to place an html file by the name MyHomePage.html at the root folder of the web site, which, in the case of Tomcat, is WebContent:
WebContent
|-- META-INF
|-- WEB-INF
| `-- web.xml
`-- MyHomePage.html
I created a basic rest service using Spring MVC with xml config. My app name is: myservice. When i create a war from it, I receive a war named myervice-1.0.0. In conclusion, I have to access my application through http://localhost:8080/myservice-1.0.0/resource. I'd like to be just 'myservice', like project name. What can I do? Thanks a lot. I'm using tomcat + gradle.
web.xml:
<servlet>
<servlet-name>webappservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webappservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
You can rename the war name. If you are using maven as build tool. Then you can use below solution.
<build>
<finalName>myservice</finalName>
. . .
</build>
It will better if you can share some configuration details like pom.xml,web.xml etc.
If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.
see more about it here
I've created a Struts2 project which is working fine when I place my struts.xml file inside src directory.
Below is my web.xml configuration.
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>struts2project</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
If I put my struts.xml inside WEB-INF directory instead, it is not getting loaded.
Please do not give answers as "it is irrelevant" or something else. I'm just asking whether (and how) we can change the default location of struts.xml or not.
To be completely clear, you don't even need struts.xml, the only needed configuration file is web.xml.
From Configuration Files
From a Struts developer point of view, the one required configuration
file used by the framework is web.xml. From here, you have full
control over how Struts configures both itself and your application.
By default, Struts will load a set of internal configuration files to
configure itself, then another set to configure your application,
however it is possible to build an entire Struts application without
writing a single configuration file other than web.xml.
[...]
File Optional Location (relative to webapp) Purpose
-----------------------------------------------------------------------------------------
web.xml no /WEB-INF/ Web deployment descriptor to include
all necessary framework components
-----------------------------------------------------------------------------------------
struts.xml yes /WEB-INF/classes/ Main configuration, contains
result/view types, action mappings,
interceptors, and so forth
To answer your question, however, it is possible to add configuration files other than the default ones by using the config initialization parameter in web.xml.
From web.xml
Key Initialization Parameters
config - a comma-delimited list of XML configuration files to load.
So it is enough to specify the new struts.xml in web.xml to achieve your goal:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<!-- Edit after #AleskandrM's comments/answer because
1) every possible configuration file must be specified,
otherwise they will be hidden by this setting and
2) settings are relative to /WEB-INF/classes/, and hence
the /WEB-INF/ must be replaced by ../ and
3) the order is important as well. You cannot extend
struts-default package in your struts.xml if it isn't loaded yet.
-->
<param-value>/WEB-INF/struts.xml</param-value>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>
That said, I generally avoid this kind of customizations: you will earn basically nothing, apart from potential drawbacks that you might be the only one in the world to get, due to having left the main road.
The correct configuration to put struts.xml directly into WEB-INF folder is:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>
Read Andrea answer and this one to find out why.
Also the order in which you define xml files is important as well. E.g. You cannot extend struts-default package (from struts-default.xml) in your struts.xml if it isn't loaded yet.
struts.xml should only be in the classpath of the application . Its location is irrelevant.
Struts 2 loads default configuration files (struts-default.xml, struts-plugin.xml, struts.xml) using StrutsPrepareAndExecuteFilter (org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter).
To understand about loading of these files by framework -
These filenames are defined as constant in Dispatcher.class (used in StrutsPrepareAndExecuteFilter) file of Struts framework.
DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
init_TraditionalXmlConfigurations() method present in Dispatcher.class loads these xmls.
private void init_TraditionalXmlConfigurations() {
String configPaths = initParams.get("config");
if (configPaths == null) {
configPaths = DEFAULT_CONFIGURATION_PATHS;
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files) {
if (file.endsWith(".xml")) {
configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}
To Override the default path setting for these xmls you can provide the paths of these XMLs while defining the StrutsPrepareAndExecuteFilter in web.xml as answered by other users here.
I have jsp file named index.jsp in root folder (webcontent), till now I simply used to access the page using the following URL:
http://localhost:8080/Sample/index.jsp
Where Sample is the project name.
Now I was asked to change the URL pattern to the following without changing the location of index.jsp file (i.e. still the index.jsp be under webcontent folder):
http://localhost:8080/Sample/test/index.jsp
How to achieve this by configuring the web.xml?
This can be achieved by the following way
<servlet>
<servlet-name>BeerAppServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>BeerAppServlet</servlet-name>
<url-pattern>/test/index.jsp</url-pattern>
</servlet-mapping>
In <servlet> instead of mapping it to <servlet-class> map it to <jsp-file>
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.