MVC模式 用户登录的代码
来源:互联网 发布:图片合成编辑软件 编辑:程序博客网 时间:2024/06/18 07:23
register.jsp
<%@ page contentType="text/html; charset=GB2312"%>
<jsp:useBean id="registerForm"
class="com.hbsi.csdn.net.bean.RegisterFormBean" scope="request" />
<form action="../servlet/controllerservlet" method="post">
姓名:
<input type="text" name="name"
value='<jsp:getProperty name="registerForm" property="name"/>'>
<font color="red"><%=registerForm.getErrorMsg("name")%></font>
<br />
密码:
<input type="password" name="password1"
value='<jsp:getProperty name="registerForm" property="password1"/>' />
<font color="red"><%=registerForm.getErrorMsg("password1")%></font>
<br>
确认密码:
<input type="password" name="password2"
value='<jsp:getProperty name="registerForm" property="password2"/>' />
<font color="red"><%=registerForm.getErrorMsg("password2")%></font>
<br>
email:
<input type="text" name="email"
value='<jsp:getProperty name="registerForm" property="email"/>' />
<font color="red"><%=registerForm.getErrorMsg("email")%></font>
<br>
<input type="submit" name="submit" value="注册" />
</form>
ControllerServlet.java
package com.hbsi.csdn.net.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hbsi.csdn.net.bean.RegisterFormBean;
import com.hbsi.csdn.net.bean.UserBean;
import com.hbsi.csdn.net.util.DbUtil;
import com.hbsi.csdn.net.util.DbUtilException;
public class ControllerServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ControllerServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = null;
String name = request.getParameter("name");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
String email = request.getParameter("email");
RegisterFormBean registerFrom = new RegisterFormBean();
registerForm.setName(name);
registerForm.setPassword1(Password1);
registerForm.setPassword2(Password2);
registerForm.setemail(email);
request.setAttribute("registerForm", registerForm);
if (!registerForm.validate()) {
rd = request.getRequestDispatcher("/jsp/register.jsp");
rd.forward(request, response);
return;
}
UserBean user = new UserBean();
user.setName(name);
user.setPassword(password1);
user.setEmail(email);
try {
DbUtil db = DbUtil.getInstance();
db.insertUser(user);
} catch (DbUtilException ex) {
registerForm.setErrorMsg("name", ex.getErrorMsg("name"));
rd = request.getRequestDispatcher("/jsp/register.jsp");
rd.forward(request, response);
return;
}
HttpSession session = request.getSession();
session.setAttribute("logonUser", user);
rd = request.getRequestDispatcher("/jsp/logonSuccess.jsp");
rd.forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
LogonSuccess.jsp
<%@ page contentType="text/html; charset=GB2312"%>
<%
if ("logout".equals(request.getParameter("action"))) {
session.invalidate();
%>
<jsp:forward page="logon.jsp" />
<%
}
if (session.getAttribute("logonUser") == null) {
%>
<jsp:forward page="logon.jsp" />
<%
}
%>
<jsp:useBean id="logonUser" class="com.hbsi.csdn.net.bean.UserBean"
scope="session" />
恭喜你,登陆成功!
<br />
<jsp:getProperty property="name" name="logonUser" /><br />
<jsp:getProperty property="password" name="logonUser" /><br />
<jsp:getProperty property="email" name="logonUser" /><br />
<a href="logonSuccess.jsp?action=logout">注销</a>
<br />
<a href="logonSuccess.jsp">重新登录</a>
RegisterFormBean.java
package com.hbsi.csdn.net.bean;
import java.util.Hashtable;
public class RegisterFormBean {
private String name = "";
private String password1 = "";
private String password2 = "";
private String email = "";
private Hashtable errors = new Hashtable();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword1() {
return this.password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getPassword2() {
return this.password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean validate() {
boolean allok = true;
if (name.trim().equals("")) {
errors.put("name", "Please input your name.");
allok = false;
}
if (password1.length() > 10 || password1.length() < 6) {
errors.put("password1", "Password must have 6-10 characters.");
allok = false;
}
if (!password2.equals(password1)) {
errors.put("password2", "Passwords do not match.");
allok = false;
}
if (!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+")) {
errors.put("email", "Illegal email.");
allok = false;
}
return allok;
}
public void setErrorMsg(String err, String errMsg) {
if ((err != null) && (errMsg != null)) {
errors.put(err, errMsg);
}
}
public String getErrorMsg(String err) {
String err_msg = (String) errors.get(err);
return (err_msg == null) ? "" : err_msg;
}
}
UserBean.java
package com.hbsi.csdn.net.bean;
import java.io.Serializable;
public class UserBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name="";
private String password="";
private String email="";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean vaildatePassword(String password){
if(this.password.equals(password)){
return true;
}else{
return false;
}
}
}
DbUtil.java
package com.hbsi.csdn.net.util;
import java.util.Hashtable;
import com.hbsi.csdn.net.bean.UserBean;
public class DbUtil {
/**
* @param args
*/
private static DbUtil instance = new DbUtil();
private Hashtable users = new Hashtable();
@SuppressWarnings("unchecked")
private DbUtil() {
UserBean user1 = new UserBean();
user1.setName("zxx");
user1.setPassword("12345678");
user1.setEmail("zxx@126.com");
users.put("zxx", user1);
UserBean user2 = new UserBean();
user2.setName("flx");
user2.setPassword("abcdefg");
user2.setEmail("flx@126.com");
users.put("flx", user2);
}
public static DbUtil getInstance() {
return instance;
}
public UserBean getUser(String userName) {
UserBean user = (UserBean) users.get(userName);
return user;
}
public void InsertUser(UserBean user) throws DbUtilException {
if (user == null) {
return;
}
String userName = user.getName();
if (users.get(userName) != null) {
DbUtilException ex = new DbUtilException();
ex.setErrorMsg("name", "UserName have used.");
throw ex;
}
users.put(userName, user);
}
}
<%@ page contentType="text/html; charset=GB2312"%>
<jsp:useBean id="registerForm"
class="com.hbsi.csdn.net.bean.RegisterFormBean" scope="request" />
<form action="../servlet/controllerservlet" method="post">
姓名:
<input type="text" name="name"
value='<jsp:getProperty name="registerForm" property="name"/>'>
<font color="red"><%=registerForm.getErrorMsg("name")%></font>
<br />
密码:
<input type="password" name="password1"
value='<jsp:getProperty name="registerForm" property="password1"/>' />
<font color="red"><%=registerForm.getErrorMsg("password1")%></font>
<br>
确认密码:
<input type="password" name="password2"
value='<jsp:getProperty name="registerForm" property="password2"/>' />
<font color="red"><%=registerForm.getErrorMsg("password2")%></font>
<br>
email:
<input type="text" name="email"
value='<jsp:getProperty name="registerForm" property="email"/>' />
<font color="red"><%=registerForm.getErrorMsg("email")%></font>
<br>
<input type="submit" name="submit" value="注册" />
</form>
ControllerServlet.java
package com.hbsi.csdn.net.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hbsi.csdn.net.bean.RegisterFormBean;
import com.hbsi.csdn.net.bean.UserBean;
import com.hbsi.csdn.net.util.DbUtil;
import com.hbsi.csdn.net.util.DbUtilException;
public class ControllerServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ControllerServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = null;
String name = request.getParameter("name");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
String email = request.getParameter("email");
RegisterFormBean registerFrom = new RegisterFormBean();
registerForm.setName(name);
registerForm.setPassword1(Password1);
registerForm.setPassword2(Password2);
registerForm.setemail(email);
request.setAttribute("registerForm", registerForm);
if (!registerForm.validate()) {
rd = request.getRequestDispatcher("/jsp/register.jsp");
rd.forward(request, response);
return;
}
UserBean user = new UserBean();
user.setName(name);
user.setPassword(password1);
user.setEmail(email);
try {
DbUtil db = DbUtil.getInstance();
db.insertUser(user);
} catch (DbUtilException ex) {
registerForm.setErrorMsg("name", ex.getErrorMsg("name"));
rd = request.getRequestDispatcher("/jsp/register.jsp");
rd.forward(request, response);
return;
}
HttpSession session = request.getSession();
session.setAttribute("logonUser", user);
rd = request.getRequestDispatcher("/jsp/logonSuccess.jsp");
rd.forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
LogonSuccess.jsp
<%@ page contentType="text/html; charset=GB2312"%>
<%
if ("logout".equals(request.getParameter("action"))) {
session.invalidate();
%>
<jsp:forward page="logon.jsp" />
<%
}
if (session.getAttribute("logonUser") == null) {
%>
<jsp:forward page="logon.jsp" />
<%
}
%>
<jsp:useBean id="logonUser" class="com.hbsi.csdn.net.bean.UserBean"
scope="session" />
恭喜你,登陆成功!
<br />
<jsp:getProperty property="name" name="logonUser" /><br />
<jsp:getProperty property="password" name="logonUser" /><br />
<jsp:getProperty property="email" name="logonUser" /><br />
<a href="logonSuccess.jsp?action=logout">注销</a>
<br />
<a href="logonSuccess.jsp">重新登录</a>
RegisterFormBean.java
package com.hbsi.csdn.net.bean;
import java.util.Hashtable;
public class RegisterFormBean {
private String name = "";
private String password1 = "";
private String password2 = "";
private String email = "";
private Hashtable errors = new Hashtable();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword1() {
return this.password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getPassword2() {
return this.password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean validate() {
boolean allok = true;
if (name.trim().equals("")) {
errors.put("name", "Please input your name.");
allok = false;
}
if (password1.length() > 10 || password1.length() < 6) {
errors.put("password1", "Password must have 6-10 characters.");
allok = false;
}
if (!password2.equals(password1)) {
errors.put("password2", "Passwords do not match.");
allok = false;
}
if (!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+")) {
errors.put("email", "Illegal email.");
allok = false;
}
return allok;
}
public void setErrorMsg(String err, String errMsg) {
if ((err != null) && (errMsg != null)) {
errors.put(err, errMsg);
}
}
public String getErrorMsg(String err) {
String err_msg = (String) errors.get(err);
return (err_msg == null) ? "" : err_msg;
}
}
UserBean.java
package com.hbsi.csdn.net.bean;
import java.io.Serializable;
public class UserBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name="";
private String password="";
private String email="";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean vaildatePassword(String password){
if(this.password.equals(password)){
return true;
}else{
return false;
}
}
}
DbUtil.java
package com.hbsi.csdn.net.util;
import java.util.Hashtable;
import com.hbsi.csdn.net.bean.UserBean;
public class DbUtil {
/**
* @param args
*/
private static DbUtil instance = new DbUtil();
private Hashtable users = new Hashtable();
@SuppressWarnings("unchecked")
private DbUtil() {
UserBean user1 = new UserBean();
user1.setName("zxx");
user1.setPassword("12345678");
user1.setEmail("zxx@126.com");
users.put("zxx", user1);
UserBean user2 = new UserBean();
user2.setName("flx");
user2.setPassword("abcdefg");
user2.setEmail("flx@126.com");
users.put("flx", user2);
}
public static DbUtil getInstance() {
return instance;
}
public UserBean getUser(String userName) {
UserBean user = (UserBean) users.get(userName);
return user;
}
public void InsertUser(UserBean user) throws DbUtilException {
if (user == null) {
return;
}
String userName = user.getName();
if (users.get(userName) != null) {
DbUtilException ex = new DbUtilException();
ex.setErrorMsg("name", "UserName have used.");
throw ex;
}
users.put(userName, user);
}
}
- MVC模式 用户登录的代码
- 使用mvc模式实现用户登录001
- 使用mvc模式实现用户登录002
- 使用mvc模式实现用户登录003
- MVC模式实现用户登录练习中遇到的问题和需要注意的细节
- 基于MVC的用户登录程序
- Java Web笔记:MVC设计模式实现用户登录
- 用户登录及验证用户是否登录的代码
- WEB MVC模式下的注册登录
- 对用户自动登录实现的代码
- JS记录用户登录次数的代码
- 限制用户登录主库的代码
- 简单用户登录模式的实现
- MVC 用户登录状态检测
- 用Spring MVC实现用户登录的完整实例
- 基于MVC构架的用户登录简单实践
- Mvc校验用户没有登录就跳转的实现
- 用Spring MVC实现用户登录的完整实例
- innerHTML,outerHTML,innerText,outerText的区别和用法
- oracle表分区详解
- 天源迪科笔试20121115
- (一)要开发设即时通讯 首先定义功能
- open vswitch研究: ovsdb
- MVC模式 用户登录的代码
- 南阳理工学院 OJ 16 矩形嵌套
- 硬盘不能立着放
- 数字图像噪声
- Spring 读书笔记-----使用Spring容器(一)
- Eclipse IDE for C/C++ Developers 与 MinGW 配置说明
- 【转】eclipse 部署 openfire 3.7.1
- 权限系统分析
- heap和stack有什么区别