Chinaunix首页 | 论坛 | 博客
  • 博客访问: 555351
  • 博文数量: 855
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 19:08
文章分类

全部博文(855)

文章存档

2011年(1)

2008年(854)

我的朋友

分类:

2008-10-16 19:11:10


  MVC的概念,大家都清楚吧,Model,View,Control
  首先我们看看这个目录结构
  --+login
  ----------+WEB-INF
  -----------------------+classes
  -beans
               -tags
  -----------+tlds
  
  login 是主目录放jsp文件,在例子login.jsp,loginFailed.jsp,login_form.jsp,newAccount.jsp,welcome.jsp,accountCreated.jsp
  
  Web-inf下面有web.xml配置文件,classes文件夹放类,tlds文件夹放自定义标签
  由于我没有用到数据库,所以没有用LIB文件夹,是来放置 *.jar 文件的。
  
  classes目录下,有beans,tags文件夹,分别放置User,LoginDB类,和自定义标签类GetRequestParameterTag,classes目录下还直接放了LoginServlet,NewAccountServlet控制器类
  
  我们先看beans下的两个业务对象类
  User.java
  package beans;
  
  public class User implements java.io.Serializable {
  private final String userName, password, hint;
          //final强调此属性初始化后,不能修改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 upwd) {
  return getUserName().equals(uname) && getPassword().equals(upwd);
  }
  }
  
  LoginDB.java
  package beans;
  import java.util.Iterator;
  import java.util.Vector;
  public class LoginDB implements java.io.Serializable {
     private Vector users = new Vector();
    //Vector类是同步的,所以addUser就不需要同步了
    public void addUser(String name, String pwd, String hint) {
      users.add(new User(name, pwd, hint));
    }
    //下面方法判断是否存在正确的user
    public User getUser(String name,String pwd) {
      Iterator it = users.iterator();
      User user;
       //迭代需要同步
    synchronized(users) {
      while(it.hasNext()){
       user = (User)it.next();
       if(user.equals(name,pwd))
          return user; //如果返回真,就返回当前user
      }
     }
     return null;
     }
     public String getHint(String name) {
       Iterator it = users.iterator();
       User user;
     synchronized(users) {
       while(it.hasNext()){
       user = (User)it.next();
       if(user.getUserName().equals(name))
         return user.getHint();
       }
     }
    return null;
     }
  }
  
  login.jsp
  
  
  
  Login Page
  
  
  <%@ include file="login_form.jsp" %>
  
  
  
  被包含的login_form.jsp
  <%@ taglib uri="utilities" prefix="util" %>
  
  

请登陆


  

  
">
   
    
     
     
    
    
     
     
    
    
     
    
   
用户名:
   
密码:

      
     

  

  
  LoginServlet.java
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.IOException;
  
  import beans.User;
  import beans.LoginDB;
  
  public class LoginServlet extends HttpServlet {
    private LoginDB loginDB;
  
    public void init(ServletConfig config) throws ServletException {
  super.init(config);
  loginDB = new LoginDB();
  config.getServletContext().setAttribute("loginDB",loginDB);
  }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
     String name = request.getParameter("userName"); //从login_form 表单得到值
     String pwd = request.getParameter("userPwd");
     User user = loginDB.getUser(name,pwd);
     if(user != null){ //说明存在用户
     request.getSession().setAttribute("user",user);  //放到session 里面
     request.getRequestDispatcher(response.encodeURL("/welcome.jsp")).forward(request,response);    //成功转发到welcome.jsp
     }else{
      request.getRequestDispatcher(response.encodeURL("/loginFailed.jsp")).forward(request, response);
     }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    doGet(request,response);
      }
  }
  
  web.xml添加
  
  Login  
  LoginServlet  
   

  new_account
  NewAccountServlet
   

   
  new_account
  /new_account
   

   
  Login  
  /login  
   

  
  utilities
  /WEB-INF/tlds/utilities.tld
            
  

  
  utilities.tld关键部分
  
  
  requestParameter
  tags.GetRequestParameterTag
  
  Simplest example: inserts one line of output
  Empty
  
  property
  true
  true
  

  

  

  
  自定义标签类GetRequestParameterTag.java
  package tags;
  
  import javax.servlet.ServletRequest;
  import javax.servlet.jsp.JspException;
  import javax.servlet.jsp.tagext.TagSupport;
  
  public class GetRequestParameterTag extends TagSupport {
      private String property;
  
     public void setProperty(String property){
        this.property = property;
     }
     public int doStartTag() throws JspException {
        ServletRequest reg = pageContext.getRequest();
        String value = reg.getParameter(property);
  
       try{
         pageContext.getOut().print(value == null ? "":va
【责编:admin】

--------------------next---------------------

阅读(311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~