javaweb

一.学习的内容
1.JSP开发模型
(1)JSP Model1
JSP Model1 简单轻便,合适小型web项目的快捷开发
(2)JSP Model2
JSP Model2 更适用于多人合作开发的大型web项目
2. MVC 模式
(1)模型
(2)视图
(3)控制器
3.按照JSP Model2 思想实现用户注册功能
(1)创建项目,编写JavaBean
(2)编写RegisterFormBean类
(3)创建工具类DBUtil类
(4)创建Servlet
(5) 创建JSP页面
编写两个jsp文件loginSuccess
register
4.代码及测试结果如下:

package cn.itcast.chapter11.model2.domain;

import java.util.HashMap;
import java.util.Map;

public class RegisterFormBean {

	//1.用来封装注册参数的
	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;
	}
	//------------进行服务端的数据-----------------------
	//2.定义变量来接收用户校验的信息,比如说用户名、邮箱是否符合规范,
	//第一个参数 属性名称  第二个参数:属性值是否符合规范信息,比如(“email”,”邮箱格式有误“);
	private Map<String,String> errors = new HashMap<String,String>();
	public boolean validate(){
		//2.定义变量,来判断数据是否满足规范,如果满足 就返回true,不满足就返回false
		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("password2","两次输入密码不一致");
			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;
	}
	//提供向map集合中添加错误信息
	public void setErrorMsg(String err,String errMsg){
		if((err != null) && (errMsg!=null)){
			errors.put(err, errMsg);
		}
	}
}

(2)

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;
	}
	
	
}  

(3)

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("[email protected]");
		users.put("Jack",user1);
		UserBean user2 = new UserBean();
		user2.setName("Rose");
		user2.setPassword("abcdefr");
		user2.setEmail("[email protected]");
		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;
		
	}	
}

(4)

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;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset-utf-8");
		response.setCharacterEncoding("utf-8");
		
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		String password2=request.getParameter("password2");
		String email=request.getParameter("email");
		
		RegisterFormBean formBean =new RegisterFormBean ();
		
		formBean.setName(name);
		formBean.setPassword(password);
		formBean.setPassword2(password2);
		formBean.setEmail(email);
		
		if(!formBean.validate()) {
			request.setAttribute("formBean", formBean);
			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", formBean);
			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);
	}

}

(5)

<%@ 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>您的姓名:</li>
				    <li>您的邮箱:</li>
			    </ul>
		    </div>
	    </div>
</body>
</html>

(6)

<%@ 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>

javaweb_第1张图片

javaweb_第2张图片

javaweb_第3张图片
javaweb_第4张图片

你可能感兴趣的:(javaweb,mvc,java,前端)