JSP的Struts2框架实现数据库连接的登录验证界面

框架这东西刚开始看真的是一脸懵逼,用struts2做了一个简单的登录小功能,现在对于框架已经是十分明朗了

【全文借鉴于:Struts2+MySql,又更改为连接SqlServer数据库】

在Eclipse上开发的Dynamic Web Project

使用的tomcat-7.0.73和struts2.5.16

配置好tomcat这个自然不用说,教程网上全都是


首先配置好web.xml



  	
  
    index.jsp
  
  
  	struts2
  	
  		org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  	
  
  
  	struts2
  	*.action
  

******

拦截器的写法可以在包中查看

JSP的Struts2框架实现数据库连接的登录验证界面_第1张图片

******

然后是jsp文件,共有3个


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    
姓名:
密码:


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'success.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    登陆成功!
  


fail.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'fail.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    登陆失败,请重新登陆!
  


之后引入struts2框架的jar包并且配置struts2.xml

JSP的Struts2框架实现数据库连接的登录验证界面_第2张图片

struts2.xml配置【注意:struts.xml必在Java Resource下的src目录下】

struts2.xml的配置借鉴:struts2配置




	
		
			/success.jsp
			/fail.jsp
		
	


最后写好5个类,包和类的部署如下图

JSP的Struts2框架实现数据库连接的登录验证界面_第3张图片

UserAction.java

package action;

import service.UsersService;

import com.opensymphony.xwork2.ModelDriven;

import entity.Users;

public class UsersAction implements ModelDriven{
	private Users user=new Users();
	public Users getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	public String execute(){
		UsersService userce=new UsersService();
		if(userce.userlogin(user)){
			return "success";
		}
		return "fail";
	}

}


UserInfoDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import until.Dbutil;
import entity.Users;

public class UserInfoDao {
	public boolean Login(Users u){
		Connection con=null;
		PreparedStatement pst=null;
			boolean flag = false;
			ResultSet rst = null;
			con=Dbutil.getCon();
			try {
				pst = con.prepareStatement("select * from users where username=? and password=?");
				pst.setString(1, u.getUsername());
				pst.setString(2, u.getPassword());
				rst = pst.executeQuery();
				if (rst.next()) {
					int result = rst.getInt(2);
					flag = result >=1 ? true : false;
				} else {
					flag = false;
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				Dbutil.closeResultSet(rst);
				Dbutil.closeStatement(pst);

			}
			return flag;
		}
}


User.java

package entity;

public class Users {
	private String username;
	private String password;
	public Users(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public Users() {
		super();
	}
	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;
	}	
}


UserService.java

package service;

import dao.UserInfoDao;
import entity.Users;

public class UsersService {
	UserInfoDao user=new UserInfoDao();
	public boolean userlogin(Users us){
		return user.Login(us);
	}
}


Dbutil.java

package until;

import java.sql.*;
public class Dbutil {
	
	private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String url="jdbc:sqlserver://localhost:1433;integratedSecurity=true;DataBaseName=userinfo";
	private static Connection con = null;
	//建立与数据库的连接
	public static Connection getCon(){
		try
		{
			Class.forName(driver);   
			//con = DriverManager.getConnection(url,user,pwd);
			con = DriverManager.getConnection(url);
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return con;
	}
	//关闭Connection
	public static void closeCon(Connection con){
		try
		{
			if(con!=null)
				con.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	//关闭ResultSet
	public static void closeResultSet(ResultSet rst){
		try
		{
			if(rst!=null){
				rst.close();
				rst = null;
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	
	}
	
	//关闭Statement
	public static void closeStatement(Statement pst){
		try
		{
			if(pst!=null){
				pst.close();
				pst = null;
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

完成


PS.配置这个项目花了3天时间,头都快炸了,希望各位道友顺利!

你可能感兴趣的:(杂,无聊的小作业)