一、搭建Struts2环境
1.所需Jar包:(需要添加MySql数据库驱动)
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
mysql-connector-java-5.1.6-bin.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
2.配置web.xml
<?xml version="1.0" encoding="gbk"?>
<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>login.jsp</welcome-file>
</welcome-file-list>
<!--编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.login.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Struts2拦截器配置-->
<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>
</web-app>
<?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>
<include file="struts-default.xml" />
<constant name="struts.locale" value="zh_CN"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" namespace="/" extends="struts-default">
<action name="login" class="com.loginAction.LoginAction">
<result name="success">/success.jsp</result>
<result name="login">/error.jsp</result>
</action>
</package>
</struts>
package com.login.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;
public class EncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("GBK");
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.loginAction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
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(){
boolean flag=Login(username,password);
if(flag)
return SUCCESS;
else
return LOGIN;
}
/*验证登录是否成功*/
public boolean Login(String username,String password){
boolean flag=false;
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String DBDriver = "com.mysql.jdbc.Driver";
try{
Class.forName(DBDriver);
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
stmt=conn.createStatement();
System.out.println("测试连接!");
System.out.println(username);
rs = stmt.executeQuery("select * from user where username ='" + username +"' and password = '"+ password+"'");
if(rs.next()){
System.out.println("登录成功!");
flag=true;
}
}catch(Exception e){
System.out.print("连接错误!");
e.printStackTrace();
}
return flag;
}
}
三、编写页面代码
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<body>
<center><h2>
<form action="login" method="post">
用户名:<input name="username"/><br/>
密  码:<input name="password"/><br/>
<input value="登录" type="submit"><input type="reset" value="取消"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<body>
<center><h1>登录成功!</h1></center> <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<body>
<center><h1>登录失败!</h1> <br>
<a href="login.jsp">返回登录页面<a/>
</body>
</html>
四、运行结果
转载于:https://blog.51cto.com/6640606/1167803