1. 程式人生 > >JSP的Struts2框架實現資料庫連線的登入驗證介面

JSP的Struts2框架實現資料庫連線的登入驗證介面

框架這東西剛開始看真的是一臉懵逼,用struts2做了一個簡單的登入小功能,現在對於框架已經是十分明朗了

【全文借鑑於:Struts2+MySql,又更改為連線SqlServer資料庫】

在Eclipse上開發的Dynamic Web Project

使用的tomcat-7.0.73和struts2.5.16

配置好tomcat這個自然不用說,教程網上全都是

首先配置好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">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping></web-app>

******

攔截器的寫法可以在包中檢視


******

然後是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+"/";
%>

<!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">
	<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
  </head>
  
  <body>
    <form action="login.action" method="post">
    	姓名:<input type="text" name="username"/><br/>
    	密碼:<input type="text" name="password"/><br/>
    	<input type="submit" value="登入"/>
    	<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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    登陸成功!
  </body>
</html>

fail.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 'fail.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    登陸失敗,請重新登陸!
  </body>
</html>

之後引入struts2框架的jar包並且配置struts2.xml

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

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" extends="struts-default" namespace="/">
		<action name="login" class="action.UsersAction">
			<result name="success">/success.jsp</result>
			<result name="fail">/fail.jsp</result>
		</action>
	</package>
</struts>

最後寫好5個類,包和類的部署如下圖

UserAction.java

package action;

import service.UsersService;

import com.opensymphony.xwork2.ModelDriven;

import entity.Users;

public class UsersAction implements ModelDriven<Users>{
	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天時間,頭都快炸了,希望各位道友順利!

相關推薦

JSP的Struts2框架實現資料庫連線登入驗證介面

框架這東西剛開始看真的是一臉懵逼,用struts2做了一個簡單的登入小功能,現在對於框架已經是十分明朗了。【全文借鑑於:Struts2+MySql,又更改為連線SqlServer資料庫】在Eclipse上開發的Dynamic Web Project使用的tomcat-7.0.

C# 實現 MySql資料庫連線 登入並跳轉介面

前言:連線上篇文章,在上文C#登入並跳轉介面基礎上,修改登入視窗使用者資訊的驗證方式,從簡單字串匹配判斷,改進到連線MySql資料庫進行驗證。正文:1.Mysql資料庫的建立2.Visual Studio 2017 的設定(2) 安裝好以後,找到它的安裝路徑,一般都是C:\P

Jeeplus框架SSM+shiro許可權控制中實現跳過登入驗證訪問介面資料

新專案中用的Jeeplus框架,裡面用的shiro許可權控制,特別好用,但是如果單獨寫介面的話,總是會有登入驗證,下面上思路實現一行程式碼讓任意介面跳過登入限制。 正常程式碼生成機生成的程式碼不管它,這是每個模組都會自動生成的專案目錄下的模組目錄。 下面這個假設是我們自

Spring+SpringMVC+MyBatis實現資料庫連線登入功能

在使用SSM框架實現連線資料的登入功能時, 第一步首先匯入相應的jar包,然後配置web.xml檔案 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/

搭建開發框架Express,實現Web網站登入驗證

  JS是指令碼語言,指令碼語言都需要一個解析器才能執行。對於寫在HTML頁面裡的JS,瀏覽器充當瞭解析器的角色。而對於需要獨立執行的JS,NodeJS就是一個解析器。每一種解析器都是一個執行環境,不但允許JS定義各種資料結構,進行各種計算,還允許JS使用執行環境提供的內

利用functools模組的wraps裝飾器實現Flask的登入驗證

首先看一段官方對functools.wraps的功能描述: This is a convenience function for invoking update_wrapper() as a function decorator when defining a wrapper

Laravel框架實現同時連線多個數據庫

laravel4.2版本, 1.修改配置檔案:config/database.php 'pgsql' => array( 'driver' => 'pgsql', 'host' => '10.0.2.203', 'database'

自定義實現資料庫連線

資料庫連線池: >資料庫的連線物件建立工作,比較消耗效能 >一開始先在記憶體中開闢一塊空間(集合) , 先往池子裡面放置 多個連線物件。  後面需要連線的話,直接從池子裡面去。不要去自己建立連線了。  使用完畢, 要記得歸還連線。確保連線物件能迴圈利用。即建立

vue router+vuex實現首頁登入驗證判斷邏輯

首頁登入邏輯要求在頁面上判斷是否獲取到登入token ,沒有獲取到則跳轉到登入頁。登入成功後,跳轉到前一個頁面。 1.vue router 路由判斷首先我們想到的是router.beforeEach 前置導航守衛 ,這個方法接受三個引數 to from next 。 to引數為即將跳

實現企業郵箱登入驗證功能

需求說明 實現企業郵箱登入驗證功能 使用者通過JSP頁面輸入使用者名稱和密碼 如果使用者名稱為lucky,密碼為123456, 在歡迎頁面顯示“你好:lucky!” 如果驗證登入失敗,則返回登入頁面重新登入   <%@ page language="

Flask學習筆記之——藍圖、基於DBUtils實現資料庫連線池、上下文管理等

面向物件知識回顧 子類繼承父類的三種方式 class Dog(Animal): #子類 派生類 def __init__(self,name,breed, life_value,aggr): # Animal.__init__(self

本地新建Mysql資料庫連線登入錯誤

一、場景描述: mac新安裝的資料庫,/usr/local/mysql/bin下用root使用者居然不讓連線登入,不瞭解Mysql的基礎許可權前真是一臉懵的 二、操作詳情 看到這裡我們知道root使用者應該是缺少localhost連線許可權 SELECT DIST

Flask學習【第3篇】:藍圖、基於DBUtils實現資料庫連線池、上下文管理等 基於DBUtils實現資料庫連線

基於DBUtils實現資料庫連線池 小知識: 1、子類繼承父類的三種方式 class Dog(Animal): #子類 派生類 def

Java 使用動態代理和觀察者模式+threadlocal實現資料庫連線

當我們使用資料庫連線池時,如果多執行緒連線資料庫時,當超過5個執行緒連線資料庫時,那麼在第6個執行緒包括之後就連線不到資料庫了,該怎麼處理。 這裡用了java動態代理來這裡用了java的動態代理來代理資料庫的連線,攔截連線的close方法。並且給代理的連線加上一

基於DBUtils實現資料庫連線

小知識: 1、子類繼承父類的三種方式 class Dog(Animal): #子類 派生類 def __init__(self,name,breed, life_value,aggr): # Animal.__init__(self,nam

JDBC/InvocationHandler動態代理實現資料庫連線池、資料來源

Java的JDBC程式設計中,資料庫的連線和關閉是十分耗費資源的。當對資料庫的訪問不是很頻繁時,可以在每次訪問資料庫時建立一個連線,用完之後關閉。但是,對於一個複雜的資料庫應用,頻繁的建立、關閉連線,會極大的減低系統性能,造成瓶頸。所以可以使用資料庫連線池來達到

Java資料庫連線:JDBC介面

JDBC:Java Database Connection是Java資料庫連線介面,是Java與資料庫連線的橋樑。通過Java的Driver介面連線資料庫,在此之前,需匯入相關jar包(即資料庫驅動器,不同型別的資料庫有不同jar包,上網下載並匯入即可)。下一步

SSH2框架實現註冊發簡訊驗證碼例項

【原文連線】https://blog.csdn.net/u010539352/article/details/46858189  這兩天開始敲程式碼了,讓用SSH2框架,以前沒有接觸過Java專案更沒有接觸過SSH2框架,所以用註冊開始了我Java之旅。後來發現,後臺程式碼挺

使用SSH框架實現簡單的登入功能

一、準備工作 1.資料庫 2.開發工具 開發工具與環境:MyEclipse2017,jdk1.8,tomcat8.0 二、環境搭建 1.開啟MyEclipse,建立web工程ssh_example 點選finish即可

php實現註冊和登入驗證

html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head>