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);
}
}
原创粉丝点击