struts2简单登录注册程序(连接数据库)

我用的是:struts2.3,tomcat7.0,oracle,编译器intellij idea。

首先,打开idea,找到file->new->project

struts2简单登录注册程序(连接数据库)_第1张图片

根据图中所指进行选择,点击create找到struts下的lib路径,找到以下核心jar包

struts2简单登录注册程序(连接数据库)_第2张图片

接下来一路next即可

创建成功后,将主目录下的lib移动到web->web-inf下,如图

struts2简单登录注册程序(连接数据库)_第3张图片

至此,创建项目完成,以下为我的项目目录

struts2简单登录注册程序(连接数据库)_第4张图片

具体代码实现:

web.xml:



    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    

struts.xml:






    
        
            /error.jsp
            /welcome.jsp
        
        
            /error.jsp
            /welcome.jsp
        
    

Dao.java:

package dao;
import java.sql.*;
public class Dao {
    Connection con = null;
    Statement stat = null;
    ResultSet rs = null;
    public Dao() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
            stat = con.createStatement();
        } catch (Exception e) {
// TODO: handle exception
            con = null;
        }
    }
    public ResultSet executeQuery(String sql) {
        try {
            rs = stat.executeQuery(sql);
        } catch (Exception e) {
// TODO: handle exception
            rs = null;
        }
        return rs;
    }
    public int executeUpdate(String sql) {
        try {
            stat.executeUpdate(sql);
            return 0;
        } catch (Exception e) {
// TODO: handle exception
        }
        return -1;
    }
}

UserAction.java:

package com;
import java.sql.*;
import com.opensymphony.xwork2.ActionSupport;
import dao.Dao;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
    private Dao dao = new Dao();
    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 login() {
        String sql = "select * from Users where username='" + getUsername()+"' and password ='"+getPassword()+"'";
        ResultSet rS = dao.executeQuery(sql);
        try {
            if (rS.next()) {
                return "loginin";
            }
            return "loginout";
        } catch (SQLException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
            return "loginout";
        }
    }
    public String regist() {
        String sql = "insert into Users(username,password) values('"+getUsername()+"','"+getPassword()+"')";
         int i = dao.executeUpdate(sql);
        if (i > -1) {
            return "success";
        }
        return "error";
    }
}

 

index.jsp:

 

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/5/20 0020
  Time: 15:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    show
  
  
        登录
        注册
  

loign.jsp:





Login Form



















 

regist.jsp:

 




    
    Login Form

    

    


    











welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    Insert title here


您已经执行成功!

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>



    
    Insert title here


执行失败!!

注:

 

1.struts2.5及以上版本,需将web.xml中的

 

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter改为
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter,即去掉其中的ng

2.执行时需要连接网络,否则会报错Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl

3.我所使用数据库为oracle,所以需要在lib下添加oracle的jar包

4.表名Users,字段username,password

实现代码已上传:https://download.csdn.net/download/m0_37802843/10430450

你可能感兴趣的:(struts2简单登录注册程序(连接数据库))