{"id":1834,"date":"2011-03-24T18:53:33","date_gmt":"2011-03-24T13:23:33","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1834"},"modified":"2011-03-24T18:53:33","modified_gmt":"2011-03-24T13:23:33","slug":"step-by-step-simple-login-application-in-struts-2","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/step-by-step-simple-login-application-in-struts-2\/","title":{"rendered":"Step by Step Simple Login Application in Struts 2"},"content":{"rendered":"<p><a title=\"What is struts and how it works\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/struts\/what-is-struts-2-and-how-it-works\/\" target=\"_blank\">We have seen that what is struts and how it works. <\/a>I will guide the new developer, step by step to create the login application using struts 2 in this article.<\/p>\n<p><strong>Step 1 : <\/strong><a title=\"Download Struts jar file\" href=\"http:\/\/struts.apache.org\/download.cgi\" target=\"_blank\">Download the struts jar file <\/a>or sample application from the official struts website.<\/p>\n<p><strong>Step 2 :<\/strong> Create a web project for jsp and open web.xml. In web.xml add the following lines of code in between &lt;web-app&gt; tag.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n\n    &lt;filter&gt;\n\t&lt;filter-name&gt;webwork&lt;\/filter-name&gt;\n\t&lt;filter-class&gt;\n\t    org.apache.struts.action2.dispatcher.FilterDispatcher\n\t&lt;\/filter-class&gt;\n    &lt;\/filter&gt;\n\n    &lt;filter-mapping&gt;\n\t&lt;filter-name&gt;webwork&lt;\/filter-name&gt;\n\t&lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\n    &lt;\/filter-mapping&gt;\n\n<\/pre>\n<p><!--more--><br \/>\nAbove configuration declares <strong>FilterDispatcher <\/strong>for struts 2 which is boot strap component for your website.<\/p>\n<p><strong>Step 3 :<\/strong> Copy all the jar files of struts 2 application downloaded in Step 1 into lib folder of your application.<\/p>\n<p><strong>Step 4 : <\/strong>Create<strong> struts.xml<\/strong> file at the root level of the classes and add following lines :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE struts PUBLIC\n    &quot;-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN&quot;\n    &quot;http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd&quot;&gt;\n\n&lt;struts&gt;\n\n    &lt;constant name=&quot;struts.enable.DynamicMethodInvocation&quot; value=&quot;false&quot; \/&gt;\n    &lt;constant name=&quot;struts.devMode&quot; value=&quot;false&quot; \/&gt;\n\n    &lt;include file=&quot;Login.xml&quot;\/&gt;\n\n&lt;\/struts&gt;\n<\/pre>\n<p>As you can see in above configuration, I have included other configuration file named &#8220;Login.xml&#8221; Instead of writing all the configuration in single struts.xml , we can split the xml configuration file and include in main struts.xml. I will explain the creation of file &#8220;Login.xml&#8221;\u009d later in this tutorail series.<\/p>\n<p><strong> Step 5 : <\/strong>Now create a jsp page in folder &#8220;pages&#8221;\u009d under WebContent folder with Name &#8220;Login.jsp&#8221;\u009d.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=ISO-8859-1&quot;\n    pageEncoding=&quot;ISO-8859-1&quot;%&gt;\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd&quot;&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=ISO-8859-1&quot;&gt;\n&lt;title&gt;Login&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;form action=&quot;LoginAction.action&quot; &gt;\nUserName : &lt;input type=&quot;text&quot; name=&quot;uName&quot; \/&gt;&lt;br\/&gt;\nPassword : &lt;input type=&quot;password&quot; name=&quot;pwd&quot; \/&gt;&lt;br \/&gt;\n&lt;input type=&quot;submit&quot;&gt;&lt;\/input&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p><strong>Step 6 :<\/strong> Create one <strong>Model Class<\/strong>, where the logic is written to validate the User. Here in this series no database is envolved. We have simply hardcoaded the tutorial for easy understanding of the concept.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Model;\n\npublic class LoginValidate {\n\n\tpublic static boolean isLoginValid(String uName, String pwd)\n\t{\n\t\treturn &quot;admin&quot;.equals(uName) &amp;&amp; &quot;admin&quot;.equals(pwd)?true:false;\n\t}\n}\n<\/pre>\n<p><strong>Step 7: <\/strong>Now create the Action Class.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Actions;\n\nimport com.G2.Model.LoginValidate;\nimport com.opensymphony.xwork2.ActionSupport;\n\npublic class LoginAction extends ActionSupport {\n\n\tString uName,pwd;\n\n\tpublic String getuName() {\n\t\treturn uName;\n\t}\n\n\tpublic void setuName(String uName) {\n\t\tthis.uName = uName;\n\t}\n\n\tpublic String getPwd() {\n\t\treturn pwd;\n\t}\n\n\tpublic void setPwd(String pwd) {\n\t\tthis.pwd = pwd;\n\t}\n\n\t@Override\n\tpublic String execute() throws Exception {\n\t\treturn LoginValidate.isLoginValid(uName, pwd) ? SUCCESS : INPUT;\n\t}\n\n}\n<\/pre>\n<p><strong>Points to remember while creating Action classes in Struts 2:<\/strong><\/p>\n<ol>\n<li>Every Action Class extend the Class &#8220;<strong>com.opensymphony.xwork2.ActionSupport<\/strong>&#8220;\u009d.<\/li>\n<li>override the execute() method.<\/li>\n<li>There is no compulsion on extending the ActionSupport class, and not necessary that only execute() method works. Any method, which return string can work. That&#8217;s the powerful feature of new Struts 2 framework. <strong>But if we will not extend &#8220;ActionSupport&#8221;\u009d, then we will miss much of the default capabilities of Struts like Validation features.<\/strong><\/li>\n<li>V<strong>ariable name must be same as expected request parameter name with getter and setters<\/strong>. In this case we expect two request parameter named uName and pwd.<\/li>\n<li>Any String result can be returned from execute() method of the struts2, but it is recommended to return the standard defined result like SUCCESS or INPUT.<\/li>\n<\/ol>\n<p><strong>Step 8: <\/strong>Create configuration file which is included in step 4 of name &#8220;Login.xml&#8221;\u009d. Instead of creating this file, we may also add the code lines in struts.xml, but it is good practice to have the separate xml file of separate modules in application.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE struts PUBLIC\n        &quot;-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN&quot;\n        &quot;http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd&quot;&gt;\n\n&lt;struts&gt;\n\n    &lt;package name=&quot;Login&quot; extends=&quot;struts-default&quot;&gt;\n\n        &lt;action name=&quot;LoginAction&quot; class=&quot;com.G2.Actions.LoginAction&quot;&gt;\n            &lt;result name=&quot;input&quot;&gt;\/pages\/Login.jsp&lt;\/result&gt;\n            &lt;result name=&quot;success&quot;&gt;\/pages\/LoginSuccess.jsp&lt;\/result&gt;\n        &lt;\/action&gt;\n\n    &lt;\/package&gt;\n&lt;\/struts&gt;\n\n<\/pre>\n<p>Package in Java used to group together similar type of class. In struts 2, package tag is used to group together similar types of actions along with interceptors.<br \/>\nThe action &#8220;LoginAction&#8221;\u009d, may return <strong>two types of result, either &#8220;input&#8221;\u009d or &#8220;success&#8221;\u009d<\/strong>, depending on that the view is decided.<\/p>\n<p>Instead of writing &#8220;jsp file path&#8221;\u009d in &lt;result&gt; , it is recommended to write the action, so that in future, if the page name changes then instead of changing jsp file path everywhere, we have to change only tag.<br \/>\nSo, the above configuration can be re-written as:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE struts PUBLIC\n        &quot;-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN&quot;\n        &quot;http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd&quot;&gt;\n\n&lt;struts&gt;\n\n\t&lt;package name=&quot;Login&quot; extends=&quot;struts-default&quot;&gt;\n\t\t&lt;action name=&quot;LoginInput&quot;&gt;\n\t\t\t&lt;result&gt;\/pages\/Login.jsp&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t\t&lt;action name=&quot;LoginSuccess&quot;&gt;\n\t\t\t&lt;result&gt;\/pages\/LoginSuccess.jsp&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t\t&lt;action name=&quot;LoginAction&quot; class=&quot;com.G2.Actions.LoginAction&quot;&gt;\n\t\t\t&lt;result name=&quot;input&quot; type=&quot;chain&quot;&gt;LoginInput&lt;\/result&gt;\n\t\t\t&lt;result name=&quot;success&quot; type=&quot;chain&quot;&gt;LoginSuccess&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t&lt;\/package&gt;\n&lt;\/struts&gt;\n\n<\/pre>\n<p>Note here, &#8220;<strong>type<\/strong>&#8220;\u009d attribute is introduced. There are many result types available in struts.<\/p>\n<p>Mostly used types are: <strong>chain, redirect and dispatcher<\/strong><\/p>\n<p><strong>Difference in &#8220;chain&#8221;\u009d and &#8220;redirectAction&#8221;\u009d<\/strong><\/p>\n<p>In &#8220;Chain&#8221;\u009d result type, the next action class gets request object with all the parameters of the previous actions class whereas in &#8220;redirectAction&#8221;\u009d, new request is generated and therefore next Action class will not get the previous request and its parameters.<\/p>\n<p>All available types can be read from: <a title=\"Result Types in Struts 2\" href=\"http:\/\/struts.apache.org\/2.x\/docs\/result-types.html\" target=\"_blank\">http:\/\/struts.apache.org\/2.x\/docs\/result-types.html<\/a><br \/>\nFinal application structure is:<\/p>\n<figure id=\"attachment_1835\" aria-describedby=\"caption-attachment-1835\" style=\"width: 252px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Final-Structure-of-Struts-2-login-application.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1835\" title=\"Final Structure of Struts 2 login application\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Final-Structure-of-Struts-2-login-application.jpg?resize=252%2C367&#038;ssl=1\" alt=\"Final Structure of Struts 2 login application\" width=\"252\" height=\"367\" \/><\/a><figcaption id=\"caption-attachment-1835\" class=\"wp-caption-text\">Final Structure of Struts 2 login application<\/figcaption><\/figure>\n<p><strong><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/struts2-Login.zip\">Download War file of Source code (Change extension from zip to war)<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example of Simple Login Application in Struts 2<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[16],"tags":[335],"class_list":["post-1834","post","type-post","status-publish","format-standard","hentry","category-struts","tag-struts"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1829,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/what-is-struts-2-and-how-it-works\/","url_meta":{"origin":1834,"position":0},"title":"What is Struts 2 and how it works","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"What is Struts 2 and how it works?","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"How Struts 2 Works","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/How-Struts-2-Works.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1851,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/dynamic-results-in-struts-2\/","url_meta":{"origin":1834,"position":1},"title":"Dynamic Results in Struts 2","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Example and sourcecode of creating Dynamic Results in Struts 2","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1864,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/explain-dynamic-method-invocation-in-struts-2\/","url_meta":{"origin":1834,"position":2},"title":"Explain dynamic method invocation in Struts 2","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Explain dynamic method invocation in Struts 2","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2034,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/tutorial-of-simple-jsp-tiles-application-without-struts\/","url_meta":{"origin":1834,"position":3},"title":"Tutorial of Simple JSP Tiles application without Struts","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"Example of Simple JSP Tiles application without Struts","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"Simple JSP Tiles without Struts","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Simple-JSP-Tiles-without-Struts.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1860,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/error-resolved-form-action-defaulting-to-action-attributes-literal-value\/","url_meta":{"origin":1834,"position":4},"title":"Error Resolved- Form action defaulting to &#8216;action&#8217; attribute&#8217;s literal value","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Error Resolved in Struts 2- Form action defaulting to 'action' attribute's literal value","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1664,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/step-by-step-dwr-application-simple-ajax-in-java\/","url_meta":{"origin":1834,"position":5},"title":"Step by Step DWR Application &#8211; Simple AJAX in JAVA","author":"Jitendra","date":"March 15, 2011","format":false,"excerpt":"Step by Step DWR Application - Simple AJAX in JAVA","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"How DWR works in Java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/How-DWR-works-in-Java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1834"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1834\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}