2012-08-19 10:33:36| 分类: 4. Struts2|举报|字号 订阅
一、搭建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"?>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
3.Struts.xml配置
xml version="1.0" encoding="UTF-8" ?> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
4.创建数据库login,然后在数据库login中创建一个user表,该表有两个字段username和password,字段类型为varchar类型。然后再输入两条记录
二、编写Java代码
1.建立一个com.login.filter包,在包下新建一个Java类
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
}
}
2.创建一个com.loginAction包,在该包下新建一个Java类
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; } }
三、编写页面代码
需要3个页面login.jsp、success.jsp和error.jsp
1.login.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
2.success.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
3.error.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
href="login.jsp">返回登录页面
运行项目,实现登录功能。