`
零度弥合
  • 浏览: 20648 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

J2EE之路(四)JspModel1模式和JspModel2模式的简单实例

 
阅读更多

JSP MODEL1

所谓JSP MODEL1 模式就是纯JSP开发,不说废话,一个简单的例子

包结构:JSP要放在WEB-INF文件夹之外,结构如图所示

 

login.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 'login.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 bgcolor="pink">
  <center>
  	<h1>用户登陆</h1><br>
  	<hr>
  	<form action="loginC1.jsp" method="post">
  		用户名:<input type="text" name="username" size="20"><br> 
  		&nbsp;&nbsp;密码:<input type="password" name="password" size="20"><br>
  		<input type="submit" value="登陆">
  		<input type="reset" value="重置">
  	</form>
  </center>
  </body>
</html>

 

loginC1.jsp

<%@ page language="java" import="java.util.*,java.sql.*" 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 'loginC1.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>
    <% 
    	//接收用户名和密码,完成对用户的验证
    	String u=request.getParameter("username");
    	String p=request.getParameter("password");
    	
    	//到数据库中去验证用户
    	//1.加载驱动
    	//Class.forName("com.mysql.jdbc.Driver");
    	Class.forName("com.mysql.jdbc.Driver").newInstance();
    	
    	//2.得到链接
    	Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
    	
    	//3.创建statement
    	Statement s = c.createStatement();
    	
    	//4.查询
    	ResultSet rs = s.executeQuery("select password from users where username='" + u + "'");
    	
    	//5.得到结果
    	if(rs.next()){
    		if(rs.getString(1).equals(p)){
    			//用户名密码正确
    			response.sendRedirect("wel.jsp?username="+u);
    		}else{
    			//密码错了
    			response.sendRedirect("login.jsp");
    		}
    	}else{
    		//用户名不存在
    		response.sendRedirect("login.jsp");
    	}
    %>
  </body>
</html> 

wel.jsp

<%@ page language="java" import="java.util.*,java.sql.*" 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 'wel.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>
  	登陆成功!!恭喜您!!<%=request.getParameter("username") %><br>
  	<a href="login.jsp">返回登陆页面</a>
  	<br>
  	<h3>用户信息列表</h3>
  	<%
  		int pageSize = 5;
  		int pageNow = 1;//默认显示第一页
  		int rowCount = 0;//该值从数据库查询
  		int pageCount = 0;//该值通过计算获得
  		
  		//接收pageNow
  		String strPageNow = request.getParameter("pageNow");
  		System.out.println(strPageNow);
  		if(strPageNow != null && !strPageNow.isEmpty()){
  			pageNow = Integer.parseInt(strPageNow);
  		}
  		
  		//查询得到rowCount
  		//1.加载驱动
    	//Class.forName("com.mysql.jdbc.Driver");
    	Class.forName("com.mysql.jdbc.Driver").newInstance();
    	
    	//2.得到链接
    	Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
    	
    	//3.创建statement
    	Statement s = c.createStatement();
    	
    	//4.查询
    	ResultSet rs = s.executeQuery("select count(*) from users");
    	
    	//5.得到结果
    	if(rs.next()){
    		rowCount = rs.getInt(1);
    	}
    	
    	if(rowCount%pageSize == 0){
    		pageCount = rowCount/pageSize;
    	}else{
    		pageCount = rowCount/pageSize + 1;
    	}

  		//查出需要显示的数据
  		rs = s.executeQuery("select * from users limit " + String.valueOf((pageNow - 1)*pageSize) + "," + pageSize);
  	%>
  		<table border="1">
  			<tr><td>用户ID</td><td>用户名字</td><td>用户密码</td><td>电子邮件</td><td>级别</td></tr>
  	<%
  		while(rs.next()){
  	%>
  		<tr>
  			<td><%=rs.getInt(1) %></td>
  			<td><%=rs.getString(2) %></td>
  			<td><%=rs.getString(3) %></td>
  			<td><%=rs.getString(4) %></td>
  			<td><%=rs.getInt(5) %></td>
  		</tr>
  	<%
  		}
  	%>
  		</table>
  	<%
  		//翻页控件
  		if(pageNow!=1){
  			out.println("<a href=wel.jsp?pageNow=" + String.valueOf(pageNow-1) + "><上一页</a>&nbsp;&nbsp;");
  		}
  		for(int i=1;i<=pageCount;i++){
  			out.println("<a href=wel.jsp?pageNow=" + String.valueOf(i) + ">" + i + "</a>&nbsp;&nbsp;");
  		}
  		if(pageNow!=pageCount){
  			out.println("&nbsp;&nbsp;<a href=wel.jsp?pageNow=" + String.valueOf(pageNow+1) + ">下一页></a>");
  		}
  	%>
  </body>
</html>

 

 JSP MODEL2模式

就是把页面和逻辑尽量分开,页面用JSP,逻辑用java类

包结构如下



UserBean.java

package bean;

public class UserBean {

	private int userId;
	private String username;
	private String password;
	private String eMail;
	private int grade;
	
	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the eMail
	 */
	public String geteMail() {
		return eMail;
	}
	/**
	 * @param eMail the eMail to set
	 */
	public void seteMail(String eMail) {
		this.eMail = eMail;
	}
	/**
	 * @return the grade
	 */
	public int getGrade() {
		return grade;
	}
	/**
	 * @param grade the grade to set
	 */
	public void setGrade(int grade) {
		this.grade = grade;
	}
	
}

 

UserBo.java

package bo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import util.JdbcUtils;
import bean.UserBean;

//封装对user表的增删改查.....
public class UserBo {
	
	private PreparedStatement ps = null;
	private Connection conn = null;
	private ResultSet rs = null;
	
	private int pageSize = 5;
  	private int rowCount = 0;//该值从数据库查询
  	private int pageCount = 0;//该值通过计算获得

	//验证用户是否存在
	public boolean checkUser(String username,String password){
		boolean userExist = false;
		
		try {
			conn = JdbcUtils.getConnection();
			String strSql = "select password from users where username=?";
			ps = conn.prepareStatement(strSql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if(rs.next()){
	    		if(rs.getString(1).equals(password)){
	    			//用户名密码正确
	    			userExist = true;
	    		}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.free(rs, ps, conn);
		}
		return userExist;
	}
	
	//得到分页的一览数据
	public List<UserBean> getUserList(String strPageNow){
		List<UserBean> userList = new ArrayList<UserBean>();

		int pageNow = 1;
  		if(strPageNow != null){
  			pageNow = Integer.parseInt(strPageNow);
  		}
  		
  		try {
			//查出需要显示的数据
  			conn = JdbcUtils.getConnection();
  			String startRow = String.valueOf((pageNow - 1)*pageSize);
  			String strSql = "select*from users limit ?,?";
  			ps = conn.prepareStatement(strSql);
  			ps.setInt(1, Integer.parseInt(startRow));
  			ps.setInt(2, pageSize);
  			
	  		rs = ps.executeQuery();
	  		
	  		while(rs.next()){
	  			UserBean userBean = new UserBean();
		  		userBean.setUserId(rs.getInt(1));
		  		userBean.setUsername(rs.getString(2));
		  		userBean.setPassword(rs.getString(3));
		  		userBean.seteMail(rs.getString(4));
		  		userBean.setGrade(rs.getInt(5));
		  		
		  		userList.add(userBean);
	  		}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.free(rs, ps, conn);
		}
		
		return userList;
	}
	
	//返回分页的总件数
	public int getPageCount(){
		try {
			conn = JdbcUtils.getConnection();
			String strSql = "select count(*) from users";
			ps = conn.prepareStatement(strSql);
			rs = ps.executeQuery();
			if(rs.next()){
	    		rowCount = rs.getInt(1);
	    	}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.free(rs, ps, conn);
		}
		
		if(rowCount%pageSize == 0){
    		pageCount = rowCount/pageSize;
    	}else{
    		pageCount = rowCount/pageSize + 1;
    	}
		return pageCount;
	}
}

 

JdbcUtils.java

package util;

import java.sql.*;

public final class JdbcUtils {
	private static String url = "jdbc:mysql://localhost:3306/test";
	private static String user = "root";
	private static String pw = "";
	
	//不让别人new
	private JdbcUtils(){
	}
	
	static{
		try{
			//1:注册驱动(只做一次)
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e){
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static Connection getConnection() throws SQLException{
		return DriverManager.getConnection(url,user,pw);
	}
	
	public static void free(ResultSet rs,PreparedStatement ps,Connection conn){
		try{
			if(rs != null)
				rs.close();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
				try{
					if(ps != null)
						ps.close();
				}catch(SQLException e){
					e.printStackTrace();
				}finally{
					try{
						if(conn != null)
							conn.close();
					}catch(SQLException e){
						e.printStackTrace();
					}	
				}
			}
	}
	
	public static void free(ResultSet rs,Statement ps,Connection conn){
		try{
			if(rs != null)
				rs.close();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
				try{
					if(ps != null)
						ps.close();
				}catch(SQLException e){
					e.printStackTrace();
				}finally{
					try{
						if(conn != null)
							conn.close();
					}catch(SQLException e){
						e.printStackTrace();
					}	
				}
			}
	}
}

 

login.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 'login.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 bgcolor="pink">
  <center>
  	<h1>用户登陆</h1><br>
  	<hr>
  	<form action="loginC1.jsp" method="post">
  		用户名:<input type="text" name="username" size="20"><br> 
  		&nbsp;&nbsp;密码:<input type="password" name="password" size="20"><br>
  		<input type="submit" value="登陆">
  		<input type="reset" value="重置">
  	</form>
  </center>
  </body>
</html>

 

loginC1.jsp

<%@ page language="java" import="java.util.*,java.sql.*,bo.UserBo" 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 'loginC1.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>
    <% 
    	//接收用户名和密码,完成对用户的验证
    	String u=request.getParameter("username");
    	String p=request.getParameter("password");
    	
    	//调用userBo完成对用户的验证
    	UserBo userBo = new UserBo();
    	if(userBo.checkUser(u,p)){
    		response.sendRedirect("wel.jsp?username="+u);
    	}else{
    		response.sendRedirect("login.jsp");
    	}
    %>
  </body>
</html>

 

wel.jsp  

<%@ page language="java" import="java.util.*,java.sql.*,java.util.List,bo.UserBo,bean.UserBean" 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 'wel.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>
  	登陆成功!!恭喜您!!<%=request.getParameter("username") %><br>
  	<a href="login.jsp">返回登陆页面</a>
  	<br>
  	<h3>用户信息列表</h3>
  	<%
  		//接收pageNow
  		String strPageNow = request.getParameter("pageNow");
  		int pageNow = 1;
  		if(strPageNow != null){
  			pageNow = Integer.parseInt(strPageNow);
  		}
  		UserBo userBo = new UserBo();
  		List<UserBean> userBeanList = userBo.getUserList(strPageNow);
  	%>
  		<table border="1">
  			<tr><td>用户ID</td><td>用户名字</td><td>用户密码</td><td>电子邮件</td><td>级别</td></tr>
  	<%
  		for(UserBean userBean:userBeanList){
  	%>
  		<tr>
  			<td><%=userBean.getUserId() %></td>
  			<td><%=userBean.getUsername() %></td>
  			<td><%=userBean.getPassword() %></td>
  			<td><%=userBean.getPassword() %></td>
  			<td><%=userBean.getGrade() %></td>
  		</tr>
  	<%
  		}
  		int pageCount = userBo.getPageCount();
  	%>
  		</table>
  	<%
  		//翻页控件
  		if(pageNow!=1){
  			out.println("<a href=wel.jsp?pageNow=" + String.valueOf(pageNow-1) + "><上一页</a>&nbsp;&nbsp;");
  		}
  		for(int i=1;i<=pageCount;i++){
  			out.println("<a href=wel.jsp?pageNow=" + String.valueOf(i) + ">" + i + "</a>&nbsp;&nbsp;");
  		}
  		if(pageNow!=pageCount){
  			out.println("&nbsp;&nbsp;<a href=wel.jsp?pageNow=" + String.valueOf(pageNow+1) + ">下一页></a>");
  		}
  	%>
  </body>
</html>

 

  • 大小: 7.3 KB
  • 大小: 26 KB
分享到:
评论

相关推荐

    5_javaWeb知识之:MVC设计模式和java三层架

    JSPModel2模式实现了MVC设计模式的基本结构,它将表示层、业务逻辑层和数据访问层进行了明确的划分。具体来说: - **JSP**:作为视图层,用于接收和显示数据。 - **Servlet**:作为控制器层,负责调度和协调模型与...

    jsp Model1 示例系统

    Model1模式是早期JSP应用中的简单架构,它将业务逻辑、视图和控制逻辑紧密地结合在一起。在这个系统中,我们将探讨如何使用JSP Model1模型来实现用户管理,包括添加、修改、查询和删除用户等功能。 **一、JSP Model...

    JSP model2(MVC)思想实现用户注册

    在Web开发领域,JSP Model2模式是一种广泛应用的架构,它是对MVC(Model-View-Controller)设计模式的一种实现。MVC模式是软件工程中用于分隔应用程序数据和显示逻辑的重要工具,使得代码结构清晰,易于维护。下面将...

    JSPModel.rar_page

    本资料"JSPModel.rar_page"主要关注JSP中的四个核心对象:`page`, `request`, `response`, `session`, `application`,它们在网页开发中扮演着至关重要的角色。 1. **page对象** `page` 对象代表当前正在执行的JSP...

    J2EE电子商务系统开发从入门到精通

    - **JSPModel1与JSPModel2**:JSPModel1是指直接在JSP页面中编写Java代码来处理业务逻辑,这种方式容易导致代码混乱;JSPModel2则是采用MVC模式,将业务逻辑处理交给后端的Java类处理,提高了解耦性。 - **Struts...

    Java程序开发教学课件:16JSPModel.pptx

    Java程序开发教学课件主要关注的是Java服务器页面(JSP)的编程模型,特别是JSP Model 1和Model 2,以及Web框架的概念。这里我们将深入探讨这些知识点。 1. JSP Scriptlet JSP Scriptlet是嵌入在JSP文件中的Java...

    spring mvc 入门

    - **JspModel2架构**:遵循MVC模式,将业务逻辑、视图和控制分离得更加彻底,提高了代码的可维护性和可测试性。 #### SpringMVC架构特色 SpringMVC架构相较于传统的JSP Model2架构,具有以下优势: - **清晰的...

    JSPModel1_Project

    会员系统和公告板系统 会员制 公告栏系统 ERD 实现功能 登录,登出功能 会员注册,会员修改,会员退出功能 通用布告栏,图像布告栏,文件上传布告栏功能 公告栏评论功能 公告板关键词搜索功能 主页幻灯片功能 使用...

    2020计算机毕业实习总结报告三篇.doc

    基于 Struts 构架的 web 应用程序基本上贴合 JSPModel2 的设计标准,能够说是 MVC 设计模式的一种变化类型。 五、Hibernate 对象关系映射框架 Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了...

    超杰软件系列(JSP进销存).

    - 使用MVC架构模式,将业务逻辑层、数据访问层和用户界面层分开,提高系统的可维护性和扩展性。 ##### 4.2 权限分配 - 为不同的用户角色分配不同的权限,确保数据安全。 - 例如管理员可以拥有全部权限,而普通用户...

    基于JSP的网上书城的设计与实现

    - **JSPModel2**:遵循MVC模式,将业务逻辑、视图逻辑和控制逻辑分离。使用Servlet作为控制器,JSP作为视图,JavaBean作为模型。这种方式更加灵活,适合构建大型Web应用。 ##### 1.4 Struts框架 - **Struts的工作...

    计算机毕业生实习报告写.doc

    Struts框架:Struts是一组相互协作的类(组件)、Serlvet以及jsptaglib组成,基于Struts构架的web应用程序根本上贴合JSPModel2的设计标准,能够说是MVC设计模式的一种变化类型。 Hibernate框架:Hibernate是一个...

Global site tag (gtag.js) - Google Analytics