How to read parameters from web.xml in Struts2 action class
- Details
- Written by Nam Ha Minh
- Last Updated on 31 July 2019   |   Print Email
Sometimes we need to retrieve values of parameters which are configured in web.xmlfile from our Struts action class. For example, we define a parameter named host in the web.xml file as follows:
<context-param> <param-name>host</param-name> <param-value>192.168.123.233</param-value> </context-param>
To read this parameter from within an action class, it requires doing the following things:
- Make the action class implementing the org.apache.struts2.util.ServletContextAware interface.
- Implement the setServletContext() method to obtain reference of the ServletContext object which is passed by Struts (for any action class that implements the ServletContextAware interface).
- In action method, we can read value of any parameter defined in the web.xmllike this:
String host = (String) servletContext.getInitParameter("host");
Let’s see a complete example.
Code of the web.xml file:
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2WebParam</display-name> <context-param> <param-name>host</param-name> <param-value>192.168.123.233</param-value> </context-param> <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> </web-app>
Code of the Struts action class:
package net.codejava.struts; import javax.servlet.ServletContext; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport implements ServletContextAware { private String host; private ServletContext servletContext; public String execute() { host = (String) servletContext.getInitParameter("host"); System.out.println("host = " + host); return SUCCESS; } @Override public void setServletContext(ServletContext context) { this.servletContext = context; } public String getHost() { return this.host; } }
Note that in this action class, we also declare a property named host along with its getter method, so that it can be accessed in the result page.
Code of the result JSP page:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Result</title> </head> <body> <center> <h3>Host = ${host}</h3> </center> </body> </html>
Code of the struts.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="Struts2WebParam" extends="struts-default"> <action name="test" class="net.codejava.struts.MyAction"> <result name="success">/Result.jsp</result> </action> </package> </struts>
Output:
Here is the output when running the application at http://localhost:8080/Struts2WebParam/test:
Related Struts Tutorials:
- How to configure Struts framework in web.xml
- Passing parameters from struts.xml to action class
- Splitting (modularizing) Struts configuration file
- How to access HttpServletRequest and HttpServletResponse within Struts2 action class
Other Struts Tutorials:
- Introduction to Struts 2 framework
- Struts beginner tutorial (Eclipse + Tomcat + XML)
- Struts Beginner Tutorial with Annotations
- Struts beginner tutorial with Convention Plugin (zero-configuration)
- How to handle exceptions in Struts
- Send e-mail with attachments in Struts
- Struts File Upload Tutorial
- Struts - Spring - Hibernate Integration Tutorial
Comments