web開發模式
Model1
顯示層,控制層,資料層,統一交給jsp或者javabean處理.
處理流程
客戶端傳送request 到 jsp jsp 執行javabean javabean讀取databases
返回 databases 返回給javabean 在返回給jsp 在response 給客戶端
即 jsp + dao 方式
model - view - controller
客戶端傳送request 到servlet 然後servlet 執行javabean javabean用於讀取databases 控制器,獲取到javabean讀取的資料以後,再次返回給jsp,jso生成html檔案,response 給客戶端
分為 顯示層 控制層 模型層
EJB 屬於SUN提供的分散式
程式碼如下
建立資料庫
no 列名稱 描述
1 userid 儲存使用者的登入id
2 name 使用者真實姓名
3 password 使用者密碼
目錄結構如下
jdbc層
package com.ming.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://47.94.95.84:32786/test";
private static final String DBUSER = "test";
private static final String DBPASSWORD = "ABCcba20170607";
private Connection connection = null;
// 連線資料庫
public DatabaseConnection() throws Exception{
try{
Class.forName(DBDRIVER);
connection = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}catch (Exception e){
throw e;
}
// 獲得資料庫連線
public Connection getConnection(){
return this.connection;
// 資料庫關閉
public void close() throws Exception{
if(this.connection != null){
this.connection.close();
dao層
定義介面
package com.ming.dao;
import com.ming.vo.User;
public interface IUserDAO {
/**
* 使用者登入驗證
* @param user 傳入VO物件
* @return 驗證操作結果
* @throws Exception 丟擲錯誤
*/
public boolean findLogin(User user) throws Exception;
實現類
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDAOImpl implements IUserDAO {
// 資料庫連線物件
// 操作物件
private PreparedStatement preparedStatement = null;
// 資料庫連線
public UserDAOImpl(Connection _connection){
this.connection = _connection;
*
@Override
public boolean findLogin(User user) throws Exception {
boolean flag = false;
String sql = "SELECT name FROM user WHERE userid = ? AND password = ?";
// 獲得例項化物件
this.preparedStatement = this.connection.prepareStatement(sql);
// 設定id
this.preparedStatement.setString(1, user.getUserid());
this.preparedStatement.setString(2, user.getPassword());
ResultSet resultset = this.preparedStatement.executeQuery();
if(resultset.next()){
user.setName(resultset.getString(1));
flag = true;
}finally {
if(this.preparedStatement != null){
this.preparedStatement.close();
return flag;
代理類
import com.ming.dbc.DatabaseConnection;
public class UserDAOProxy implements IUserDAO {
private DatabaseConnection databaseConnection = null;
private IUserDAO dao = null;
public UserDAOProxy(){
this.databaseConnection = new DatabaseConnection();
e.printStackTrace();
this.dao = new UserDAOImpl(this.databaseConnection.getConnection());
flag = this.dao.findLogin(user);
this.databaseConnection.close();
定義代理工廠
package com.ming.factory;
import com.ming.dao.IUserDAO;
import com.ming.dao.UserDAOProxy;
public class DAOFactory {
public static IUserDAO getIuserDAOInstance(){
return new UserDAOProxy();
實體關係對映
package com.ming.vo;
// 對user表進行對映
public class User {
private String userid;
private String name;
private String password;
public String getUserid() {
return userid;
public String getName() {
return name;
public String getPassword() {
return password;
public void setUserid(String userid) {
this.userid = userid;
public void setName(String name) {
this.name = name;
public void setPassword(String password) {
this.password = password;
檢視層
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %><%--
Created by IntelliJ IDEA.
User: ming
Date: 19-3-16
Time: 下午11:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>使用者登入程式</h2>
<%
List<String> info = (List<String>)request.getAttribute("info");
if(info != null){
Iterator<String> iterator = info.iterator();
while(iterator.hasNext()){
%>
<h4><%=iterator.next()%></h4>
<form action="loginServlet" method="post">
使用者id <input type="text" name="userid"/>
密碼 <input type="password" name="userpass"/>
<input type="submit" value="登入"/>
<input type="reset" value="重置"/>
</form>
<script>
let submit = document.getElementById("submit");
submit.onclick = (event) => {
let uname = document.getElementById("uname").value;
let password = document.getElementById("password").value;
if(!(/^\w{5,15}/.test(uname))){
alert("使用者id為5-15位");
return false;
if(!(/^\w{5,15}/.test(password))){
alert("密碼必須為5-15位");
return true;
</script>
</body>
</html>
配置檔案
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.ming.servlrt.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
mvc執行流程
表單提交到servlet,servlet呼叫dao進行表單驗證,然後dao連線資料庫進行驗證,驗證結果返回給業務層,即servlet,在業務層servlet中獲取info等日誌資訊,然後伺服器端跳轉到執行結果頁面即view層.
web開發模式
Model1
顯示層,控制層,資料層,統一交給jsp或者javabean處理.
處理流程
客戶端傳送request 到 jsp jsp 執行javabean javabean讀取databases
返回 databases 返回給javabean 在返回給jsp 在response 給客戶端
即 jsp + dao 方式
model - view - controller
客戶端傳送request 到servlet 然後servlet 執行javabean javabean用於讀取databases 控制器,獲取到javabean讀取的資料以後,再次返回給jsp,jso生成html檔案,response 給客戶端
分為 顯示層 控制層 模型層
EJB 屬於SUN提供的分散式
程式碼如下
建立資料庫
no 列名稱 描述
1 userid 儲存使用者的登入id
2 name 使用者真實姓名
3 password 使用者密碼
目錄結構如下
jdbc層
package com.ming.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://47.94.95.84:32786/test";
private static final String DBUSER = "test";
private static final String DBPASSWORD = "ABCcba20170607";
private Connection connection = null;
// 連線資料庫
public DatabaseConnection() throws Exception{
try{
Class.forName(DBDRIVER);
connection = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}catch (Exception e){
throw e;
}
}
// 獲得資料庫連線
public Connection getConnection(){
return this.connection;
}
// 資料庫關閉
public void close() throws Exception{
if(this.connection != null){
try{
this.connection.close();
}catch (Exception e){
throw e;
}
}
}
}
dao層
定義介面
package com.ming.dao;
import com.ming.vo.User;
public interface IUserDAO {
/**
* 使用者登入驗證
* @param user 傳入VO物件
* @return 驗證操作結果
* @throws Exception 丟擲錯誤
*/
public boolean findLogin(User user) throws Exception;
}
實現類
package com.ming.dao;
import com.ming.vo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDAOImpl implements IUserDAO {
// 資料庫連線物件
private Connection connection = null;
// 操作物件
private PreparedStatement preparedStatement = null;
// 資料庫連線
public UserDAOImpl(Connection _connection){
this.connection = _connection;
}
/**
* 使用者登入驗證
*
* @param user 傳入VO物件
* @return 驗證操作結果
* @throws Exception 丟擲錯誤
*/
@Override
public boolean findLogin(User user) throws Exception {
boolean flag = false;
try{
String sql = "SELECT name FROM user WHERE userid = ? AND password = ?";
// 獲得例項化物件
this.preparedStatement = this.connection.prepareStatement(sql);
// 設定id
this.preparedStatement.setString(1, user.getUserid());
this.preparedStatement.setString(2, user.getPassword());
ResultSet resultset = this.preparedStatement.executeQuery();
if(resultset.next()){
user.setName(resultset.getString(1));
flag = true;
}
}catch (Exception e){
throw e;
}finally {
if(this.preparedStatement != null){
try{
this.preparedStatement.close();
}catch (Exception e){
throw e;
}
}
}
return flag;
}
}
代理類
package com.ming.dao;
import com.ming.dbc.DatabaseConnection;
import com.ming.vo.User;
public class UserDAOProxy implements IUserDAO {
private DatabaseConnection databaseConnection = null;
private IUserDAO dao = null;
public UserDAOProxy(){
try{
this.databaseConnection = new DatabaseConnection();
}catch (Exception e){
e.printStackTrace();
}
this.dao = new UserDAOImpl(this.databaseConnection.getConnection());
}
/**
* 使用者登入驗證
*
* @param user 傳入VO物件
* @return 驗證操作結果
* @throws Exception 丟擲錯誤
*/
@Override
public boolean findLogin(User user) throws Exception {
boolean flag = false;
try{
flag = this.dao.findLogin(user);
}catch (Exception e){
throw e;
}finally {
this.databaseConnection.close();
}
return flag;
}
}
定義代理工廠
package com.ming.factory;
import com.ming.dao.IUserDAO;
import com.ming.dao.UserDAOProxy;
public class DAOFactory {
public static IUserDAO getIuserDAOInstance(){
return new UserDAOProxy();
}
}
實體關係對映
package com.ming.vo;
// 對user表進行對映
public class User {
private String userid;
private String name;
private String password;
public String getUserid() {
return userid;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setUserid(String userid) {
this.userid = userid;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
檢視層
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %><%--
Created by IntelliJ IDEA.
User: ming
Date: 19-3-16
Time: 下午11:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>使用者登入程式</h2>
<%
List<String> info = (List<String>)request.getAttribute("info");
if(info != null){
Iterator<String> iterator = info.iterator();
while(iterator.hasNext()){
%>
<h4><%=iterator.next()%></h4>
<%
}
}
%>
<form action="loginServlet" method="post">
使用者id <input type="text" name="userid"/>
密碼 <input type="password" name="userpass"/>
<input type="submit" value="登入"/>
<input type="reset" value="重置"/>
</form>
<script>
let submit = document.getElementById("submit");
submit.onclick = (event) => {
let uname = document.getElementById("uname").value;
let password = document.getElementById("password").value;
if(!(/^\w{5,15}/.test(uname))){
alert("使用者id為5-15位");
return false;
}
if(!(/^\w{5,15}/.test(password))){
alert("密碼必須為5-15位");
return false;
}
return true;
}
</script>
</body>
</html>
配置檔案
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.ming.servlrt.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
mvc執行流程
表單提交到servlet,servlet呼叫dao進行表單驗證,然後dao連線資料庫進行驗證,驗證結果返回給業務層,即servlet,在業務層servlet中獲取info等日誌資訊,然後伺服器端跳轉到執行結果頁面即view層.