<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this chapter we will learn about the exception handling in struts2 framework. If any exception occur between the code it redirected to the dedicated page whatever you define or map into the configuration file of struts2.</p>
<p>In our web application, there might occur exception. To overcome this problem, struts 2 provides a mechanism of global exception handling where we can display a global result to the user.</p>
<h2><b>Example of Exception Handling in struts 2 &#8211;</b></h2>
<p>Struts makes the exception handling easy by the use of the &#8220;<b>exception</b>&#8221; interceptor. The &#8220;exception&#8221; interceptor is included as part of the default stack, so you don&#8217;t have to do anything extra to configure it. It is available out-of-the-box ready for you to use. Let us see a simple Hello World example with some modification in<i><b> LoginAction.java</b></i> file. Here we purposely introduced a <i><b>NullPointerException </b></i>in our <i><b>LoginAction </b></i>action code.</p>
</div>
<pre class="highlight">package com.dineshonjava.struts2.login; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@SuppressWarnings("serial") 
public class LoginAction extends ActionSupport{ 
 private String username; 
 private String password; 
 private String userid; 
 
 public String execute(){ 
 String x = null; 
 x = x.substring(0); 
 return SUCCESS; 
 } 
 
 public String getUsername() { 
 return username; 
 } 
 
 public void setUsername(String username) { 
 this.username = username; 
 } 
 
 public String getPassword() { 
 return password; 
 } 
 
 public void setPassword(String password) { 
 this.password = password; 
 } 
 
 public String getUserid() { 
 return userid; 
 } 
 
 public void setUserid(String userid) { 
 this.userid = userid; 
 } 
 
} 
</pre>
<div id="ads-id" align="center"></div>
<p><b>Let us keep the content of Login.jsp as follows:</b></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
 
<;!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>;Struts 2 - Login Application | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
<;h2>;Struts 2 - Login Application<;/h2>; 
<;font color="red">;<;s:actionerror />;<;/font>; 
<;s:form action="welcome.action" method="post">; 
 <;s:textfield name="username" key="label.username" size="20" />; 
 <;s:password name="password" key="label.password" size="20" />; 
 <;s:submit method="execute" key="label.login" align="center" />; 
<;/s:form>; 
<;/body>; 
<;/html>; 
</pre>
</div>
<p><b>Let us keep the content of Welcome.jsp as follows:</b></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!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>;Welcome<;/title>; 
<;/head>; 
 
<;body>; 
 <;h2>;Hello Welcome , <;s:property value="username" />;...! Dineshonjava.com<;/h2>; 
 
<;/body>; 
<;/html>; 
</pre>
</div>
<p>Your <b>struts.xml</b> should look like:</p>
<pre class="highlight"><;?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>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="welcome" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;/Welcome.jsp<;/result>; 
 <;result name="error">;/Login.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 
<;/struts>; 
</pre>
</div>
<p>Now right click on the project name and click <b>Export >; WAR</b> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access</p>
<p><b>URL <i>http://localhost:8080/doj/Login.jsp.</i></b><i> </i></p>
<p>This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/22222-3.png" border="0" /></div>
</div>
<p><b>Enter a value of user name and password and submit the page. You should see the following page:</b></p>
<p><b>http://localhost:8080/doj/welcome.action;jsessionid=8043CCAD28F8A538283A322BFF761C11 </b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/121111-1024x575.png" border="0" /></div>
</div>
<p>As shown in the above example, the default exception interceptor does a great job of handling the exception. Let us now create a dedicated error page for our Exception. Create a file called <b>globalerror.jsp</b> with the following contents:<br />
<i><b>globalresult.jsp</b></i></p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;title>;<;/title>; 
<;/head>; 
<;body>; 
 This is my custom error page 
 <;form>; 
 <;input type="button" value="back" onclick="history.back()">; 
 <;/form>; 
<;/body>; 
<;/html>; 
</pre>
</div>
<p>For exception handling, we specify the global-result and global-exception-mappings in the struts.xml file.<br />
<b>struts.xml</b></p>
<pre class="highlight"><;?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>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;global-results>; 
 <;result name="myresult">;/globalresult.jsp<;/result>; 
 <;/global-results>; 
 
 <;global-exception-mappings>; 
 <;exception-mapping result="myresult" exception="java.lang.Exception">;<;/exception-mapping>; 
 <;/global-exception-mappings>; 
 
 
 <;action name="welcome" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;/Welcome.jsp<;/result>; 
 <;result name="error">;/Login.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 
<;/struts>; 
</pre>
</div>
<p>The <b>global-results</b> sub-element of package specifies the global-result for this package.</p>
<p>The result sub-element of <b>global-result </b>specifies the result page that will be rendered to the user as a view.</p>
<p>The global-exception-mappings sub-element of package specifies the exception mapping for all the actions of this package.</p>
<p>The exception-mapping sub-element of global-exception-mapping maps the given result for the given exception type. In this example, we are using the Exception which the parent of many exception classes such as <b>IOException</b>, <b>ArithmeticException </b>etc. It means if any exception occurs, specified result will be invoked.</p>
<p><b><i>Note</i>: Notice that <i>global-results</i> must be specified before <i>global-exception-mappings</i> as we are using global result in<i> global-exception-mappings</i>.</b></p>
</div>
<p>As shown in the example above, now we have configured Struts to use the dedicated<i><b> globalerror.jsp</b></i> for the <b>NullPointerException</b>. If you rerun the program now, you shall now see the following output:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/3333-5.png" border="0" /></div>
</div>
<p> ;</p>
</div>
<h2><b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2Exception.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2Exception.zip</a></b></h2>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/themes-in-struts-2/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/annotations-in-struts2-with-example/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/themes-in-struts-2/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/annotations-in-struts2-with-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '379'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…