`

架构的演变

阅读更多

架构的演变

一、两层架构(jsp+DB)

 register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>用户注册</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 method="post" action="registerDeal.jsp">
    	用户名:<input type="text" name="username"><br>
    	密码:<input type="password" name="password"><br>
    	确认密码:<input type="password" name="password2"><br>
    	<input type="submit" value="提交"/>
    </form><br>
  </body>
</html>

 registerDeal.jsp

<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring", "root", "bjsxt");

String sqlQuery = "select count(*) from user where username = ?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1, username);
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
if(count > 0) {
	response.sendRedirect("registerFail.jsp");
	psQuery.close();
	conn.close();
	return;
}

String sql = "insert into user values (null, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
ps.close();
conn.close();

response.sendRedirect("registerSuccess.jsp");
%>

 

 

 

二、三层架构(jsp+Entity/service+DB)

register.jsp

 

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>用户注册</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 method="post" action="registerDeal.jsp">
    	用户名:<input type="text" name="username"><br>
    	密码:<input type="password" name="password"><br>
    	确认密码:<input type="password" name="password2"><br>
    	<input type="submit" value="提交"/>
    </form><br>
  </body>
</html>

 registerDeal.jsp

<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.registration.service.*" %>
<%@ page import="com.bjsxt.registration.model.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");

User u = new User();
u.setUsername(username);
u.setPassword(password);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring", "root", "bjsxt");

UserManager um = new UserManager();
boolean exist = um.exists(u);
if(exist) {
	response.sendRedirect("registerFail.jsp");
	return;
}

um.add(u);
response.sendRedirect("registerSuccess.jsp");
%>

 

user.java

package com.bjsxt.registration.model;

import com.bjsxt.registration.service.UserManager;

//贫血模型 充血模型
public class User {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}

}

 

userManager.java

package com.bjsxt.registration.service;

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

import com.bjsxt.registration.model.User;

public class UserManager {
	public boolean exists(User u) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring", "root", "bjsxt");

		String sqlQuery = "select count(*) from user where username = ?";
		PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
		psQuery.setString(1, u.getUsername());
		ResultSet rs = psQuery.executeQuery();
		rs.next();
		int count = rs.getInt(1);
		psQuery.close();
		conn.close();
		
		if(count > 0) {
			return true;
		}
		return false;
	}
	
	public void add(User u) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring", "root", "bjsxt");

		String sql = "insert into user values (null, ?, ?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, u.getUsername());
		ps.setString(2, u.getPassword());
		ps.executeUpdate();
		ps.close();
		conn.close();

		
	
	}
}

 

三、三层架构(jsp+service+Hibernate)

registerDeal.jsp

<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.registration.service.*" %>
<%@ page import="com.bjsxt.registration.model.*" %>
<%@ page import="com.bjsxt.registration.service.impl.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");

UserManager um = new UserManagerImpl();
boolean exist = um.exists(u);
if(exist) {
	response.sendRedirect("registerFail.jsp");
	return;
}

um.add(u);
response.sendRedirect("registerSuccess.jsp");
%>

 Service层:UserManager.java

package com.bjsxt.registration.service;

import com.bjsxt.registration.model.User;

public interface UserManager {

	public abstract boolean exists(User u) throws Exception;

	public abstract void add(User u) throws Exception;

}

 UserManagerImpl.java

package com.bjsxt.registration.service.impl;

import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.dao.impl.UserDaoImpl;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;

public class UserManagerImpl implements UserManager {
	
	private UserDao userDao = new UserDaoImpl();
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/* (non-Javadoc)
	 * @see com.bjsxt.registration.service.impl.UserManager#exists(com.bjsxt.registration.model.User)
	 */
	public boolean exists(User u) throws Exception {
		return userDao.checkUserExistsWithName(u.getUsername());
		
	}
	
	/* (non-Javadoc)
	 * @see com.bjsxt.registration.service.impl.UserManager#add(com.bjsxt.registration.model.User)
	 */
	public void add(User u) throws Exception {
		userDao.save(u);
	}
}

 

Dao层(Hibernate):UserDao.java

package com.bjsxt.registration.dao;

import com.bjsxt.registration.model.User;

public interface UserDao {
	public void save(User u);
	public boolean checkUserExistsWithName(String username);  
}

 UserDaoImpl.java

package com.bjsxt.registration.dao.impl;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.util.HibernateUtil;

public class UserDaoImpl implements UserDao {

	public void save(User u) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(u);
		s.getTransaction().commit();
	}

	public boolean checkUserExistsWithName(String username) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		long count = (Long)s.createQuery("select count(*) from User u where u.username = :username")
			.setString("username", username)
			.uniqueResult();
		s.getTransaction().commit();
		if(count > 0) return true;
		return false;
	}

}

 user.java(@Entity  Hibernate注解)

package com.bjsxt.registration.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import com.bjsxt.registration.service.impl.UserManagerImpl;

//贫血模型 充血模型
@Entity
public class User {
	private int id;
	private String username;
	private String password;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}

}

 

 

四、jsp+struts+Service+Hibernate

 

          

UserManager um = new UserManagerImpl();
boolean exist = um.exists(u);
if(exist) {
	response.sendRedirect("registerFail.jsp");
	return;
}

上面的代码被struts2给取代了

在struts2处理类Action里面:
1.spring通过
@Resource
private UserManager userManager ;
注入封装了UserDao的service处理类,在action里面直接调用处理类操作

2.struts.xml配置文件中的Result取代了上面的跳转

 

register.jap

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>用户注册</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 method="post" action="user.action">
    	用户名:<input type="text" name="username"><br>
    	密码:<input type="password" name="password"><br>
    	确认密码:<input type="password" name="password2"><br>
    	<input type="submit" value="提交"/>
    </form><br>
  </body>
</html>

 

 UserAction.java

package com.bjsxt.registration.action;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;
import com.opensymphony.xwork2.ActionSupport;

@Component("user")
@Scope("prototype")
public class UserAction extends ActionSupport {
	
	private String username;
	private String password;
	private String password2;
	
	private UserManager um;
	
	
	
	public UserManager getUm() {
		return um;
	}
	
	@Resource(name="userManager")
	public void setUm(UserManager um) {
		this.um = um;
	}

	@Override
	public String execute() throws Exception {
		User u = new User();
		u.setUsername(username);
		u.setPassword(password);
		if(um.exists(u)) {
			return "fail";
		}
		um.add(u);
		return "success";
	}

	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 getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	
}

 

五、jsp+struts+Hibernate+spring

 

spring把Dao注入到Daoimpl

            Service注入到Serviceimpl

            Dao注入到Service

            Service注入到Action处理类

   jsp页面*。action可以直接在action里面操作,跳转!

 

分享到:
评论

相关推荐

    SAP技术架构演变 SAP技术架构演变

    SAP 技术架构演变 SAP 技术架构的演变是 SAP 公司发展趋势的重要组成部分,本文将从技术角度对 SAP 技术架构的演变进行阐述。 1. R/2 时代(1972-1989) 在 R/2 时代,SAP 的技术架构主要基于 IBM 大型机,采用...

    大型网站架构演变和知识体系 讲解

    ### 大型网站架构演变与知识体系详解 #### 架构演变的第一步:物理分离Web服务器与数据库 在网站初创阶段,通常是通过一台主机来承载所有的功能,包括Web服务和数据库服务。随着用户访问量的增长,单一主机的性能...

    大型网站架构演变和知识体系.pdf

    ### 大型网站架构演变与知识体系解析 #### 架构演变的第一步:物理分离Web服务器与数据库 在网站发展的初期阶段,往往由于资源限制,Web应用与数据库部署在同一台服务器上。随着用户访问量的增长,单一服务器上的...

    2018大型互联网架构演变历程

    ### 2018大型互联网架构演变历程:以淘宝为例 #### 一、背景与重要性 随着互联网行业的快速发展,大型互联网企业的系统架构也在不断进化和完善。这些变化不仅仅是技术层面的进步,更是对整个互联网产业发展方向的...

    大型网站技术架构演变过程

    技术架构演变过程目录 1.架构演变第一步:物理分离webserver和数据库。 2.架构演变第二步:增加页面缓存 3.架构演变第三步:增加页面片段缓存 4.架构演变第四步:数据缓存 5.架构演变第五步: 增加webserver,应用...

    大型网站架构演变和知识体系

    【大型网站架构演变和知识体系】是一篇深入探讨互联网企业如何随着业务发展和技术进步逐步优化其架构的文章。本文将从大型网站的初期架构、垂直拆分、水平扩展、分布式服务、微服务化等方面进行详细阐述,并结合实际...

    大型网站架构演变和知识体系.

    总的来说,大型网站架构演变的知识体系包括:单体架构、垂直拆分、水平扩展、微服务架构、容器化、数据一致性理论、缓存技术、CDN、NoSQL、消息队列、监控和日志分析等多个方面。理解并掌握这些知识,有助于构建高效...

    中大型网站架构演变之路.pdf

    中大型网站架构的演变是伴随着互联网...综上所述,中大型网站架构演变是一个动态的、持续优化的过程,需要综合考虑多种技术要素,以及业务发展的不同阶段,通过不断的技术迭代和架构调整,来支撑业务的持续发展和扩展。

    整车电子电器架构演变推 动车联网信息安全的发展.pdf

    "整车电子电器架构演变推动车联网信息安全的发展" 从标题和描述中,我们可以总结出以下知识点: 1. 整车电子电器架构发展趋势:汽车工业从机械化到电气化再到机电一体化,目前正在进入第四次工业革命,即工业4.0...

    01_大型互联网架构演变历程.zip

    大型互联网架构演变历程 1. 课程目标 1.1. 了解互联网架构演变历程 1.2. 了解当前互联网架构中常用的一些 1.3. 站在巨人的肩膀上,我们的视野会更高一些 2. 淘宝技术这10年 2.1. 淘宝现状

    详解:大型网站架构演变和知识体系

    大型网站架构演变和知识体系 之前也有一些介绍大型网站架构演变的文章,例如LiveJournal的、ebay的,都是非常值得参考的,不过感觉他们讲的更多的是每次演变的结果,而没有很详细的讲为什么需要做这样的演变,再...

    大型的网站架构演变和知识体系

    大型网站架构演变和知识体系 之前也有一些介绍大型网站架构演变的文章,例如LiveJournal的、ebay的,都是非常值得参考的,不过感觉他们讲的更多的是每次演变的结果,而没有很详细的讲为什么需要做这样的演变,再加上...

    BS系统架构演变策略

    【BS系统架构演变策略】是关于大型网站在发展过程中如何逐步优化其架构以应对日益增长的访问量和性能需求的文章。文章着重讲述了从一个普通网站演变成大型网站的典型过程,涉及的关键技术和知识体系。 首先,架构...

    ebay架构原则,架构演变历史及ebay的自动化

    标题与描述均提到了“ebay架构原则,架构演变历史及ebay的自动化”,这实际上指代了eBay在IT架构设计、发展以及自动化技术应用上的深入探索与实践。以下是对这一主题的深入分析,旨在解析eBay如何通过其独特的架构...

    大型网站架构演变文档

    ### 大型网站架构演变知识点解析 #### 架构演变第一步:物理分离Web服务器与数据库 **背景**:随着网站的逐步发展,最初的单一主机难以应对不断增长的访问量及由此产生的性能瓶颈。 **解决方案**:将Web服务器与...

Global site tag (gtag.js) - Google Analytics