1.Today will show you the single example about strust2.
//Step One
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Start Strust2</title>
</head>
<body>
<!-- This is a Start page of strust2 -->
<a href="input.action"> Strust</a>
</body>
</html>
//Step Two
package iparhan.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class FirstFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request2 = (HttpServletRequest) request;
/**
* 1.from index.jsp to input.jsp
*/
//1.getservletPath
String realPath = request2.getServletPath();
//the path after jump
String path = null;
//2.judge it
if("/input.action".equals(realPath)){
path = "input.jsp";
}
/**
* 2.from input.jsp t details.jsp
*/
if("/save.action" .equals(realPath)){
//1.get parameter
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
String phono = request.getParameter("phono");
//2. create aobject
DemoFilter demoFilter = new DemoFilter(id, username, password, phono);
//3.show it
System.out.println(demoFilter);
//4.save it
path = "details.jsp";
//judge is it empty
}
if(path != null){
request.getRequestDispatcher(path).forward(request, response);
return;
}
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
//Step Thee
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<form action="save.action">
Username :<input type="text" name="username"><br/>
Password :<input type="text" name ="password"><br/>
Phono :<input type="text" name ="phono" ><rbr/>
<input type="submit" value="Strust">
</form>
</body>
</html>
//Step Four
package iparhan.filter;
public class DemoFilter {
private String id;
private String username;
private String password;
private String phono;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getPhono() {
return phono;
}
public void setPhono(String phono) {
this.phono = phono;
}
public DemoFilter(String id, String username, String password, String phono) {
super();
this.id = id;
this.username = username;
this.password = password;
this.phono = phono;
}
public DemoFilter() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "DemoFilter [id=" + id + ", username=" + username + ", password=" + password + ", phono=" + phono + "]";
}
}
//Step Five
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>after save message</title>
</head>
<body>
Id : ${requestScope.demoFilter.id }
<br/>
Username :${requestScope.demoFilter.username }
<br/>
Password: ${requestScope.demoFilter.password }
<br/>
Phono :${requestScope.demoFilter.phono }
</body>
</html>
//Step Six
<?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" 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>FirstFilter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<display-name>FirstFilter</display-name>
<filter-name>FirstFilter</filter-name>
<filter-class>iparhan.filter.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/FirstFilter</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
It,s Over , if you finish it step by step and you will success. Good luck!
Iparhan_Java 2016-7-4