web開發者模式?
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" id="uname"/>
密碼 <input type="password" name="userpass" id="password"/>
<input type="submit" value="登錄" id="submit"/>
<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層.