title: How to implement Form based authentication with struts2
body: <p>I want to implement form based authentication in struts2. </p>
  
  <p>My directory structure is :</p>
  
  <p><img src="https://i.stack.imgur.com/QgSQL.jpg" alt="enter image description here"></p>
  
  <p>My web.xml:</p>
  
  <pre><code>&lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;security-constraint&gt;
          &lt;display-name&gt;Example Security Constraint&lt;/display-name&gt;
          &lt;web-resource-collection&gt;
              &lt;web-resource-name&gt;Protected Area&lt;/web-resource-name&gt;
              &lt;url-pattern&gt;/protected/*&lt;/url-pattern&gt;
              &lt;http-method&gt;DELETE&lt;/http-method&gt;
              &lt;http-method&gt;GET&lt;/http-method&gt;
              &lt;http-method&gt;POST&lt;/http-method&gt;
              &lt;http-method&gt;PUT&lt;/http-method&gt;
          &lt;/web-resource-collection&gt;
          &lt;auth-constraint&gt;
              &lt;role-name&gt;manager&lt;/role-name&gt;
          &lt;/auth-constraint&gt;
          &lt;user-data-constraint&gt;
              &lt;transport-guarantee&gt;NONE&lt;/transport-guarantee&gt;
          &lt;/user-data-constraint&gt;
      &lt;/security-constraint&gt;
  
  
      &lt;!-- Default login configuration uses form-based authentication --&gt;
      &lt;login-config&gt;
          &lt;auth-method&gt;FORM&lt;/auth-method&gt;
          &lt;realm-name&gt;Example Form-Based Authentication Area&lt;/realm-name&gt;
          &lt;form-login-config&gt;
              &lt;form-login-page&gt;/login.jsp&lt;/form-login-page&gt;
              &lt;form-error-page&gt;/error.jsp&lt;/form-error-page&gt;
          &lt;/form-login-config&gt;
      &lt;/login-config&gt;
      &lt;security-role&gt;
          &lt;description&gt; An administrator &lt;/description&gt;
          &lt;role-name&gt;
              manager
          &lt;/role-name&gt;
      &lt;/security-role&gt;
  
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>my login.jsp :</p>
  
  <pre><code>&lt;form method="POST" action="j_security_check" &gt;
    &lt;table border="0" cellspacing="5"&gt;
      &lt;tr&gt;
        &lt;th align="right"&gt;Username:&lt;/th&gt;
        &lt;td align="left"&gt;&lt;input type="text" name="j_username"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th align="right"&gt;Password:&lt;/th&gt;
        &lt;td align="left"&gt;&lt;input type="password" name="j_password"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align="right"&gt;&lt;input type="submit" value="Log In"&gt;&lt;/td&gt;
        &lt;td align="left"&gt;&lt;input type="reset"&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>action in struts :</p>
  
  <pre><code>&lt;action name= "j_security_check" class="action.LoginAction" &gt;
              &lt;result name="success" &gt;protected/index.jsp&lt;/result&gt;
              &lt;result name="error" &gt; error.jsp &lt;/result&gt;
              &lt;result name="input" &gt; login.jsp&lt;/result&gt;
          &lt;/action&gt;
  </code></pre>
  
  <p>my LoginAction.java</p>
  
  <pre><code>    public class LoginAction extends ActionSupport {
  
          String j_username, j_password;
  
          public String getJ_password() {
              return j_password;
          }
  
          public void setJ_password(String j_password) {
              this.j_password = j_password;
          }
  
          public String getJ_username() {
              return j_username;
          }
  
          public void setJ_username(String j_username) {
              this.j_username = j_username;
          }
  
          @Override
          public String execute() throws Exception {
              if (getJ_username().equals(getJ_password())) {
                  return SUCCESS;
              } else {
                  this.addActionError("Error..!");
                  return ERROR;
              }
          }
  @Override
      public void validate() {
          if ((getJ_username() == null) || (getJ_username().length() == 0)) {
              this.addActionError("Username Empty");
          }
          if ((getJ_password() == null) || (getJ_password().length() == 0)) {
              this.addActionError("Password Empty");
          }
      }
  </code></pre>
  
  <p>With this i am When i insert same loginid and password, yet i redirected to the error page..</p>
  
  <p>Can someone give a good link for the same..?</p>
  
  <p>The example should contain a protected folder, an action class for login..</p>
  
  <p>thanks.. </p>