Example:
index.jsp
Note: To set a token in your form, you should use the token tag. This tag is required and must be used in the forms that submit to actions protected by this interceptor. Any request that does not provide a token (using the token tag) will be processed as a request with an invalid token.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="AddEntry">
<s:textfield label="Name" name="name"/>
<s:textfield label="Address" name="address"/>
<s:textfield label="Phone Number" name="phoneNumber"/>
<s:submit value="ADD"/>
<s:token name="clientToken"></s:token>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Name: <s:property value="name" />
<br>
Address: <s:property value="address" />
<br>
Phone: <s:property value="phoneNumber" />
</body>
</html>
AddEntry.java
package phoneBook;
import com.opensymphony.xwork2.ActionSupport;
public class AddEntry extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name = null;
private String address = null;
private String phoneNumber = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String execute() throws Exception{
System.out.println("Name\t\t:" +name);
System.out.println("Address\t\t:" +address);
System.out.println("Phone Number\t:" +phoneNumber);
return SUCCESS;
}
}
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" extends="struts-default">
<action name="AddEntry" class="phoneBook.AddEntry">
<interceptor-ref name="token" />
<interceptor-ref name="basicStack" />
<result name="success">/success.jsp</result>
<result name="invalid.token">/index.jsp</result>
</action>
</package>
</struts>
web.xml
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyBlankProj</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
ok, the coding is over, now its time for test.......
Lets start our application by typing the url or by eclipse. The below page (index.jsp) would be displayed. I've filled-out the form as below. and pressed submit.
The result page would appear (success.jsp). The thing here to notice is I've written code to print in the console also. In eclipse you can see the result appeared in the console just like below.
It means it will again appear if I refresh the page (i.e., resubmit the page). Lets do it....
No, it will never happen, it will go to the index.jsp page without resubmitting the page. You can check the console after refreshing the page and there you will find that only one occurrence of the result is showing and there will be an Warning stating that "com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn WARNING: Form token JVP8Z7FXG8UYSN0QMT6WZ3SOYT63QD5G does not match the session token null.".
So, this is the advantage of Token Interceptor, by which you can stop resubmitting of the form by the user.
So, this is the advantage of Token Interceptor, by which you can stop resubmitting of the form by the user.
No comments:
Post a Comment