免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2114 | 回复: 0
打印 上一主题 下一主题

MVC渐行渐进(四) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-08-03 17:16 |只看该作者 |倒序浏览
     测试页面testmvc.jsp示例:
/testmvc.jsp
<%@ page contentType="text/html;charset=gb2312" %>;
<html>;
<head>;
<title>;无标题文档</title>;
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">;
</head>;
<body bgcolor="#FFFFFF" text="#000000">;
   Pleasa Login
  <hr  width="98%">;
<!--form name="form1" method="post" action="<%=response.encodeURL("login"%>;"-->;
<form name="form1" method="post" action="<%=response.encodeURL("login-action.do"%>;">;
  <table width="80%" border="0" cellspacing="0" cellpadding="0">;
    <tr>;
      <td>; 用户名:
        <input type="text" name="userName">;
      </td>;
  </tr>;
  <tr>;
      <td>; 密 码:
        <input type="password" name="assWord" size="12">;
      </td>;
  </tr>;
  <tr>;
      <td height="37">;
        <input type="button" name="Submit" onclick="test()" value="登 陆">;
      </td>;
  </tr>;
</table>;
</form>;


</body>;
</html>;
<script language="javascript">;
function test()
{
        alert("ok";
        document.form1.submit();
}
</script>;

测试页面:welcome.jsp
示例: /welcome.jsp
you are welcome

说明:这里我偷了个懒,该页面只写这句话。

BEAN:USER类
示例: /WEB-INF/Classes/beans/User.java
package bean;
public class User implements java.io.Serializable
{
        private final String userName,password,hint;
        public User(String userName,String         password,String hint)
        {
                this.userName=userName;
                this.password=password;
                this.hint=hint;
        }
        public String getUserName()
        {
                return userName;       
        }
       
        public String getPassWord()
        {
                return password;       
        }
       
        public String getHint()
        {
                return hint;
        }
        public boolean equals(String uname,String pwd)
        {
                return getUserName().equals(uname)&& getPassWord().equals(pwd);       
        }
}
   该类表示了一个用户,并提供了一个equals的方法,当用户名和口令匹配的时候,返回true值。

BEAN:LoginDB类
示例: /WEB-INF/Classes/beans/User.java
package bean;
import java.util.Iterator;
import java.util.Vector;
import java.io.*;

public class LoginDB implements Serializable
{
        private Vector users=new Vector();
        public void addUser(String uname,String pwd,String hint)//添加用户的方法
        {
                users.add(new User(uname,pwd,hint));
        }
        public User getUser(String uname,String pwd)//检索用户的方法
        {
                Iterator it=users.iterator();
                User bean=null;
                synchronized (users){
                        while(it.hasNext())
                        {
                                bean=(User)it.next();
                                if (bean.equals(uname,pwd))
                                  return bean;
                        }
                }
                return null;
        }
        public String getHint(String uname)//对指定的用户提供返回口令提示的方法
        {
                Iterator it=users.iterator();
                User bean=null;
                synchronized (users)
                {
                        while(it.hasNext())
                        {
                                if (bean.getUserName().equals(uname))
                                        return bean.getHint();
                        }                       
                }
                return null;
        }       
}
   
LoginServlet类:
示例:  /WEB-INF/Classes/LoginServlet.java
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.*;
import bean.*;
public class LoginServlet extends HttpServlet
{
         private LoginDB loginDB;
         public void init(ServletConfig config) throws ServletException
         {
                 loginDB=new LoginDB();
         }
         public void service(HttpServletRequest req, HttpServletResponse res)
        throws java.io.IOException, ServletException
     {
             loginDB.addUser("long","long","long";
             User user=loginDB.getUser(req.getParameter("userName",req.getParameter("assWord");
             System.out.println("The name of loginDB is"+loginDB.getClass().getName());
             //String user=req.getParameter("userName";
             //System.out.println("get user name:"+user);
             /*getServletContext().getRequestDispatcher(res.encodeURL("/index.jsp").forward(req,res);*/
             /*要注意getServletContext()和req两个对象的区别,经过实验应用getServletContext()进行重定向*/
             /*总是不行,而应用req则可以*/
             if (user!=null)
             {
                     req.getRequestDispatcher(res.encodeURL("/welcome.jsp").forward(req,res);
             }
             else
             {
                     req.getRequestDispatcher(res.encodeURL("/adduser.jsp").forward(req,res);       
             }
     }
     
}
   当testmvc.jsp的表单提交时,请求被发送到登录的Servlet,这段代码我没什么好说的,在使用mvc构架之前,我们喜欢用隐藏帧来处理表单提交的内容,实际LoginServlet.java就是替代了隐藏帧而已。

到此为止,请读者将所有的示例,按示例所示的路径存储好所有的类和jsp页面。下一步我们将讨论这个东东的玩法和原理。
对了,忘了告诉大家,我的测试环境是:
win2000server  tomcat 4.1  jdk1.4  没有数据库
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP