第十一章JSP开发模型
11.1 JSP开发模型
11.1.1 JSP Model1
JSP Model1前JSP开发的早期模型和 JSPModel1 的工作原理:
2. JSP Model2
JSP Model2架构模型采用 JSP + Servlet + JavaBean 的技术,此技术将原本 JSP 页面中的流程控制代码提取出来,封装到 Servlet 中,从而实现了整个程序页面显示、流程控制和业务逻辑的分离。
11.2 MVC设计模式
它是施乐帕克研究中心在 20 世纪 80 年代为编程语言 Smalltalk–80 发明的一种软件设计模式,提供了一种按功能对软件进行模块划分的方法。MVC 设计模式将软件程序分为 3 个核心模块:模型( Model ) 视图( View ) 和 控制器( Controller )
- 模型
模型(Model) 负责管理应用程序的业务数据、定义访问控制以及修改这些数据的业务规则。当模型的状态发生改变时,它会通知视图发生改变,并为视图提供查询模型状态的方法。 - 视图
视图(View) 负责与用户进行交互,它从模型中获取数据向用户展示,同时也能将用户请求传递给控制器进行处理。当模型的状态发生改变时,视图会对用户界面进行同步更新,从而保持与模型数据的一致性。 - 控制器
控制器( Controller ) 是负责应用程序中处理用户交互的部分,它负责从视图中读取数据,控制用户输入,并向模型发送数据。
三者的关系:
任务:按照Model2思想实现用户注册功能
- 创建项目,定义UserBean类,用来封装用户信息。
package cn.itcast.chapter11.model2.domain;
/**
* 用来封装用户的基本参数
* @author apple
*
*/
public class UserBean {
private String name;
private String password;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package cn.itcast.chapter11.model2.domain;
import java.util.HashMap;
import java.util.Map;
public class RegisterFormBean {
private String name;
private String password;
private String password2;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
private Map<String,String> errors=new HashMap<String,String>();
public boolean validate() {
boolean flag=true;
if(name==null || name.trim().equals("")) {
errors.put("name", "请输入用户名");
flag=false;
return flag;
}
if(password==null || password.trim().equals("")) {
errors.put("password", "请输入密码");
flag=false;
return flag;
}else if(password.length()>12 || password.length()<6) {
errors.put("password", "请输入6-12个字符");
flag=false;
return flag;
}
if(password !=null && !password.equals(password2)) {
errors.put("password", "两次输入密码不一致");
flag=false;
return flag;
}
String regex="[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+";
if(email==null || email.trim().equals("")) {
errors.put("email", "请输入邮箱");
flag=false;
return flag;
}else if(!email.matches(regex)) {
errors.put("email", "邮箱格式错误");
flag=false;
return flag;
}
return flag;
}
public Map<String, String> getErrors(){
return errors;
}
public void setErrorMsg(String err,String errMsg) {
if((err!=null)&&(errMsg!=null)) {
errors.put(err, errMsg);
}
}
}
- 定义DBUtil工具类
package cn.itcast.chapter11.model2.util;
import java.util.HashMap;
import cn.itcast.chapter11.model2.domain.UserBean;
/*
* 模拟数据库
*/
/*
* 模拟数据库
*/
public class DBUtil {
private static DBUtil instance = new DBUtil();
public static DBUtil getInstance(){
return instance;
}
//定义集合,来保存用户注册的数据
private HashMap<String, UserBean> users = new HashMap<String, UserBean>();
//插入2条数据
public DBUtil(){
UserBean user1 = new UserBean();
user1.setName("Jack");
user1.setPassword("123456");
user1.setEmail("jack@it315.org");
users.put("Jack",user1);
UserBean user2 = new UserBean();
user2.setName("Rose");
user2.setPassword("abcdefr");
user2.setEmail("rose@it315.org");
users.put("Rose",user2);
}
//获取值的方法
public UserBean getUser(String username){
UserBean user = users.get(username);
return user;
}
//提供插入数据库的方法
public boolean insertUser(UserBean user){
//用户为null,不能插入
if(user == null){
return false;
}
//判断用户名是否重复
String username = user.getName();//用户提交过来的注册的参数
//String usernameDB = users.get(username).getName();//从数据库查询出来的用户名
UserBean userDB = users.get(username);
if(userDB !=null){//根据注册参数查询数据库有用户对象,表明用户名存在
return false;
}
users.put(username,user);
return true;
}
}
- 定义ConrtrollerServlet类
package cn.itcast.chapter11.model.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.chapter11.model2.domain.RegisterFormBean;
import cn.itcast.chapter11.model2.domain.UserBean;
import cn.itcast.chapter11.model2.util.DBUtil;
/**
* Servlet implementation class ControllerServlet
*/
@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
String password2=request.getParameter("password2");
String email=request.getParameter("email");
RegisterFormBean fromBean=new RegisterFormBean();
fromBean.setName(name);
fromBean.setPassword(password);
fromBean.setPassword2(password2);
fromBean.setEmail(email);
if(!fromBean.validate()) {
request.setAttribute("fromBean", fromBean);
request.getRequestDispatcher("/register.jsp").forward(request, response);
return ;
}
UserBean userBean=new UserBean();
userBean.setName(name);
userBean.setPassword(password);
userBean.setEmail(email);
boolean b=DBUtil.getInstance().insertUser(userBean);
if(!b) {
request.setAttribute("DBMes", "用户名已存在");
request.setAttribute("formBean", fromBean);
request.getRequestDispatcher("/register.jsp").forward(request, response);
return ;
}
response.getWriter().print("恭喜你注册成功,3秒后自动跳转");
request.getSession().setAttribute("userBean",userBean);
response.setHeader("refresh", "3,url=loginSuccess.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
- 创建jsp文件
创建register.jsp文件,该文件是用户注册的表单页面,用于接收用户的注册信息。
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户注册</title>
<style type="text/css">
h3 {
margin-left: 100px;
}
#outer {
width: 750px;
}
span {
color: #ff0000
}
div {
height:20px;
margin-bottom: 10px;
}
.ch {
width: 80px;
text-align: right;
float: left;
}
.ip {
width: 500px;
float: left
}
.ip>input {
margin-right: 20px
}
#bt {
margin-left: 50px;
}
#bt>input {
margin-right: 30px;
}
</style>
</head>
<body>
<form action="ControllerServlet" method="post">
<h3>用户注册</h3>
<div id="outer">
<div>
<div class="ch">姓名:</div>
<div class="ip">
<input type="text" name="name" value="${formBean.name }" />
<span>${formBean.errors.name}${DBMes}</span>
</div>
</div>
<div>
<div class="ch">密码:</div>
<div class="ip">
<input type="password" name="password" />
<span>${formBean.errors.password}</span>
</div>
</div>
<div>
<div class="ch">确认密码:</div>
<div class="ip">
<input type="password" name="password2" />
<span>${formBean.errors.password2}</span>
</div>
</div>
<div>
<div class="ch">邮箱:</div>
<div class="ip">
<input type="text" name="email" value="${formBean.email }" />
<span>${formBean.errors.email}</span>
</div>
</div>
<div id="bt">
<input type="reset" value="重置 " />
<input type="submit" value="注册" />
</div>
</div>
</form>
</body>
</html>
创建loginSuccess.jsp文件,该文件是用户登入成功的页面。
<%@ page language="java" pageEncoding="GBK" import="cn.itcast.chapter11.model2.domain.UserBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>login successfully</title>
<style type="text/css">
#main {
width: 500px;
height: auto;
}
#main div {
width: 200px;
height: auto;
}
ul {
padding-top: 1px;
padding-left: 1px;
list-style: none;
}
</style>
</head>
<body>
<div id="main">
<div id="welcome">恭喜你,登录成功</div>
<hr />
<div>您的信息</div>
<div>
<ul>
<li>您的姓名:${userBean.name }</li>
<li>您的邮箱:${userBean.email }</li>
</ul>
</div>
</div>
</body>
</html>
运行结果
2020080605012