16.4 - cookie Interceptor

Introduction:

  • The chapter will explain the interceptor “cookie” in Struts 2 with an example program.

cookie Interceptor:

  • Cookies are textual information of user that is stored on user’s browser. For performing this application, make sure that you enable cookies in your browser from cookie settings.
  • In this web application, the information of user name is stored in user’s browser. So the application will retrieve name and password from user.
  • So the first.jsp can be coded as:
// first.jsp

<%--
    Document   : first
    Created on : Nov 20, 2014, 12:34:17 PM
    Author     : Admin
--%>
<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title> Main Login Page </title>
    </head>
    <body>
      <s:form action = "click" method = "POST">
          <h1> Enter details </h1>
          <hr/>
         <s:label value = "Enter Username"/>  <s:textfield name = "name"/>
         <s:label value = "Enter password"/>  <s:textfield name = "password"/>

         <s:submit name = "SUBMIT"/>
         <s:reset name = "RESET"/>
      </s:form>
   </body>
</html>
  • The next.jsp will gather the information from first.jsp, and will allow login as per credentials are matched.
  • So, next.jsp can be written as follows:
//next.jsp
<%--
    Document   : next
    Created on : Nov 20, 2014, 12:50:20 PM
    Author     : Admin
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title> After Login </title>
    </head>
    <body>
       <h1> Welcome Page </h1>
       <hr/>
       <h4>
        <s:label value = "Hello  "/>  <s:property value = "name"/> 
       </h4>
    </body>
</html>

 

  • The first.jsp will run as follows:

 

Figure: First dry run of application

Figure: First dry run of application

  • The action class will only verify user’s credentials. So Myaciton1.java will be coded as:
// Myaction_1.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action_class;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.CookiesAware;
/**
 *
 * @author Admin
 */
public class Myaction_1 extends ActionSupport implements CookiesAware
{
    private String name,password;
    private Map cookiesMap;

    public Map getCookiesMap()
    {
        return cookiesMap;
    }
    public String getPassword()
    {
        return password;
    }
    public String getName()
    {
        return name;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String execute()
    {
      if(name == “abc” && password == “xyz”)
        {
            setName("Hello" + getName());
            return SUCCESS;
        }
        else
        {
                return “failure”;
        }
    }

    public void setCookiesMap(Map cookiesMap) {

        this.cookiesMap = cookiesMap; 
    }
  }

}
  • Now to combine our JSP’s with action class, we need to configure our struts.xml.
//struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name = "default" namespace = "/" extends = "struts-default">
     <action name = "click" class = "action_class.Myaction_1">
        <interceptor-ref name = "cookie">
            <param name = "cookiesName"> name </param>
            <param name = "cookiesValue"> name </param>
        </interceptor-ref>
         <result name = "success"> jsp/next.jsp </result>
     </action>
  </package>
</struts>
  • The cookie interceptor element is given under action  element with following syntax:

       <action name = "Name of Action" class = "Name of action class">

                 <interceptor-ref name = "cookie">

          <param name = "cookiesName1"> name of cookie1 </param>

          <param name = "cookiesValue1"> value of cookie1</param>

         <param name = "cookiesName2"> name of cookie2 </param>

          <param name = "cookiesValue2"> value of cookie2</param>

               </interceptor-ref>

      </action>

  • N number of cookies can be declared and used in any web application. Make sure that the value of cookies must be some field name, that is, name of a text field or any of HTML controls.
  • Also, <param> element with cookies name and value is mandatory. So in our application we are storing only user name in cookies form.
  • And at last, web.xml can be given as:
//web.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name> struts2 </filter-name>
        <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
    </filter>
    <filter-mapping>   
        <filter-name> struts2 </filter-name>
        <url-pattern> /* </url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file> jsp/first.jsp </welcome-file>
    </welcome-file-list>
</web-app>
  • So the application will run as follows:

 

Figure: First run of application

Figure: next.jsp page after verifying credentials and storing name as cookie.

Like us on Facebook