Create web.xml and struts-config.xml

Create web.xml and struts-config.xml

Previous Home Next

 

To make struts based application. We need minimum two XMLs that are web.xml and struts-config.xml .The web.xml is a deployment descriptor file which is most impartent file. For Struts based application we need one more file struts-config.xml file to configure actions of struts.

 The web.xml file provides configuration and deployment information for the Web components that comprise a Web application. Web components are:-
  • Servlet parameters,
  • Servlet and JavaServer Pages (JSP) definitions, and
  • Uniform Resource Locators (URL) mappings.
In deployment descriptor we can describe following deployment descriptor elements.
  • ServletContext init parameters
  • Localized context
  • session configuration
  • Servlet/JSP definitions
  • Servlet/JSP mappings
  • Tag Library
  • MIME type
  • Welcome file list
  • Error pages
  • Security information
A Sample Example of web.xml is for Servlet 2.4 application : The Servlet 2.4 application r4r WelcomeServlet welcome /r4r.welcome r4r.welcome 404 /error404.html .The web.xml file must palaces into WEB-INF directory.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<web-app>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcomeServlet</url-pattern>
</servlet-mapping>
</web-app>

In struts based application we have to configure
  • The Struts ActionServlet
  • The struts tag libraries files
  • The welcome files
  • The error pages. 
and action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml validating true 1 action *.do index.jsp /taglibs/struts-tiles /WEB-INF/taglibs/struts-tiles.tld 404 /error404.html. The most important configuration in web.xml is configuring struts-config.xml. We must have configured struts-config.xml (using the init-param tag) in web.xml with in ActionServlet configuration. e.g. action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml …………….. ………Here we are mapping action servlet using two different url patterns.. 
  • This will receives all requests coming from URL Pattern .do extension .This request is directed to ActionServlet action *.do e.g. http://localhost:8080/r4r/home .do this is directed to ActionServlet and ActionServlet Lookup home (URI )into struts-congif.xml called appropriate action.
  • This will receives all requests coming from URL Pattern /pages/*.This request is directed to ActionServlet action /pages/* e.g. http://localhost:8080/r4r/pages/home. 
Note: These two ways are just only you can see URL pattern in browser and using this URL pattern you can request to server.
When request is directed to ActionServlet then working will be same in both cases. struts-config.xml is most important file in struts based application .We can use more than one struts-config.xml file into struts based application. The struts-config.xml can be placed in WEB-INF folder.In struts-config.xml file we can configure ActionForm, Action ,DynaActionForm etc. All struts based components are configured here. 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.r4r.struts.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="login" path="/login.do"/>
</global-forwards>
<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" name="loginForm">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.r4r.struts.ApplicationResources"/>
</struts-config>

Previous Home Next