mvc案例

<%@ 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 'registter.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>
<form action="servlet/ControllerServlet" method="post">
<p>
用户名:
<label for="username"></label>
<input type="text" name="username" id="username" />
</p>
<p>
密码:
<label for="password"></label>
<input type="password" name="password" id="password" />
</p>
<p>
确认密码:
<label for="password"></label>
<input type="password" name="password1" id="password" />
</p>
<p>
邮箱 :
<input name="yx" type="text" id="yx"> 
</p>
<p>
<input type="submit" name="button" id="button" value="注册" />
</p>
</form>
</body>
</html>










package m.v.c;


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;


public class ControllerServlet extends HttpServlet {


/**
* 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("username");
String password= request.getParameter("password");
String password1= request.getParameter("password1");

String yx= request.getParameter("yx");

RegisterFormBean reisterForm = new RegisterFormBean();
reisterForm.setName(name);
reisterForm.setPassword1(password);
reisterForm.setPassword2(password1);
reisterForm.setEmail(yx);
request.setAttribute("reisterForm", reisterForm);

if(!reisterForm.validate()){
rd=request.getRequestDispatcher("jsp/register.jsp");
rd.forward(request, response);
return;
}
UserBean user=new UserBean();
user.setUsername(name);
user.setPassword(password1);
user.setYx(yx);
/*try{
Dbutil db=Dbutil.getInstance();
}catch(DbUtilException ex){
reisterForm.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/logonSucesss.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 {


this.doGet(request, response);
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}










package m.v.c;


import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;


public class Dbutil {
private static Dbutil insertance = new Dbutil();
private Hashtable users = new Hashtable();


private Dbutil() {
UserBean user1 = new UserBean();
user1.setUsername("zxx");
user1.setPassword("12345678");
user1.setYx("zxx@it315.org");
users.put("zxx", user1);


UserBean user2 = new UserBean();
user2.setUsername("fix");
user2.setPassword("abcdefg");
user2.setYx("fix@it315.org");
users.put("zxx", user2);
}


public void insertUser(UserBean user) throws DbUtilException {


if (user == null) {
return;
}
String userName = user.getUsername();
if (users.get(userName) != null) {
DbUtilException ex = new DbUtilException();
ex.setErrorMsg("name", "username hava used");
throw ex;
}
users.put(userName, user);
}


public static Dbutil getInstance() {
// TODO Auto-generated method stub
return null;
}


public UserBean getUser(String userName) {
UserBean user = (UserBean) users.get(userName);
return user;
}


}












package m.v.c;


import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;


public class DbUtilException extends Exception {
private Hashtable errors=new Hashtable();
public void setErrorMsg(String err,String errMsg){
if((err!=null)&&(errMsg!=null)){
errors.put(err, errMsg);
}
}


public String getErrorMsg(String err){
// TODO Auto-generated method stub
String err_msg=(String)errors.get(err);

return (err_msg==null)?"":err_msg;
}


}


















package m.v.c;


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 name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
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;
}
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 hava 6-10 characters.");
allOk=false;
}
if(!password2.equals(password1)){
errors.put("password1","password do not match.");
allOk=false;
}
if(!email.matches("[a-zA-z0-9_-]+@[a-zA-Z0-9_-]+(\\.a[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;
}
}












package m.v.c;


public class UserBean {
private String username="";
private String password="";
private String password1="";
private String yx="";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getYx() {
return yx;
}
public void setYx(String yx) {
this.yx = yx;
}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值