index.jsp:
- <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
- <%@ page contentType="text/html; charset=UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma"content="no-cache">
- <meta http-equiv="cache-control"content="no-cache">
- <meta http-equiv="expires"content="0">
- <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
- <meta http-equiv="description"content="This is my page">
- </head>
- <body>
- <form action="test1"method="post">
- 用户名:<inputtype="text"name="username"/><br/>
- 密 码:<inputtype="password"name="password">
- <input type="submit"value="提交">
- </form>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="test1" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp:
- <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'success.jsp' starting page</title>
- <meta http-equiv="pragma"content="no-cache">
- <meta http-equiv="cache-control"content="no-cache">
- <meta http-equiv="expires"content="0">
- <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
- <meta http-equiv="description"content="This is my page">
- </head>
- <body>
- 登录成功!欢迎:${username}
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
登录成功!欢迎:${username}
</body>
</html>
error.jsp:
- <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'error.jsp' starting page</title>
- <meta http-equiv="pragma"content="no-cache">
- <meta http-equiv="cache-control"content="no-cache">
- <meta http-equiv="expires"content="0">
- <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
- <meta http-equiv="description"content="This is my page">
- </head>
- <body>
- 登录失败!
- <a href="index.jsp">返回重新登录</a>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
登录失败!
<a href="index.jsp">返回重新登录</a>
</body>
</html>
LoginAction.java:
- package com.cz.action;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- publicclass LoginActionextends ActionSupport {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- publicvoid setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- public String execute(){
- System.out.println("到达execute。。。。");
- returnnull;
- }
- public String login(){
- System.out.println("到达login。。。。用户名:"+username);
- if("啊".equals(username) &&"123".equals(password)){
- return"success";
- }
- return"login";
- }
- }
package com.cz.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
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 execute(){
System.out.println("到达execute。。。。");
return null;
}
public String login(){
System.out.println("到达login。。。。用户名:"+username);
if("啊".equals(username) && "123".equals(password)){
return "success";
}
return "login";
}
}
过滤器FilterEncoding.java
- 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;
- publicclass FilterEncodingimplements Filter {
- protected String encoding;// 接收字符编码
- protected FilterConfig filterConfig;// 初始化配置
- publicvoid init(FilterConfig filterConfig)throws ServletException {
- // 从web.xml文件中读取encoding的值
- encoding = filterConfig.getInitParameter("encoding");
- }
- // doFilter方法
- publicvoid doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- request.setCharacterEncoding(encoding);
- chain.doFilter(request, response);
- }
- publicvoid destroy() {
- }
- }
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;
public class FilterEncoding implements Filter {
protected String encoding; // 接收字符编码
protected FilterConfig filterConfig; // 初始化配置
public void init(FilterConfig filterConfig) throws ServletException {
// 从web.xml文件中读取encoding的值
encoding = filterConfig.getInitParameter("encoding");
}
// doFilter方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
web.xml:
- <?xml version="1.0"encoding="UTF-8"?>
- <web-app version="2.5"
- 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_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>FilterEncoding </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
struts.xml:
- <?xml version="1.0"encoding="UTF-8"?>
- <!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">
- <actionname="test1"class="com.cz.action.LoginAction"method="login">
- <resultname="success">/success.jsp</result>
- <resultname="login">/error.jsp</result>
- </action>
- </package>
- </struts>