JSP的设计模式

1、JSP模型1(JSP+JavaBean)

  JSP页面负责处理请求,并将响应发送给客户端。JSP页面文件包括:login.html、loginchk.jsp、welcome.jsp。JavaBean文件包括:UserBean.java、UserCheckBean.java。
1)登录页面login.html:
<html>
<head>
    <title>登陆页面</title>
</head>
<body>
<form method="post" action="loginchk.jsp">
    用户名:<input type="text" name="name" width="20"><br/>&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" width="25"/><br/>
    <input type="submit" value="登录"/>
    <input type="reset" value="重置"/>
</form>
</body>
</html>

2)验证页面loginchk.jsp:

<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ page import="com.yyq.bean.UserCheckBean"%>
<%request.setCharacterEncoding("GB2312");%>
<jsp:useBean id="user" scope="session" class="com.yyq.bean.UserBean"/>
<jsp:setProperty name="user" property="*"/>
<%
    UserCheckBean uc = new UserCheckBean(user);
    if (uc.validate()){
%>
    <jsp:forward page = "welcome.jsp"/>
<%
    }
    else {
        out.println("用户名或密码错误,请<a href=\"login.html\">重新登陆</a>");
    }
%>
<html>
<head>
    <title>验证</title>
</head>
<body>
</body>
</html>

3)欢迎页面welcome.jsp:

<%@ page contentType="text/html;charset=GB2312" language="java" %>
<jsp:useBean id="user" scope="session" class="com.yyq.bean.UserBean"/>
欢迎您,<jsp:getProperty name="user" property="name"/>

4)UserBean类:

package com.yyq.bean;
/**
 * Created by gao on 16-4-18.
 */
public class UserBean {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

5)UserCheckBean类:

package com.yyq.bean;
/**
 * Created by gao on 16-4-18.
 */
public class UserCheckBean {
    private UserBean user;
    public UserCheckBean() {
    }
    public UserCheckBean(UserBean user) {
        this.user = user;
    }
    public UserBean getUser() {
        return user;
    }
    public void setUser(UserBean user) {
        this.user = user;
    }
    public boolean validate() {
        String name = user.getName();
        String password = user.getPassword();
        //实际应用中,你应该查询数据库,验证用户名和密码。
        if ("admin".equals(name) && "1234".equals(password)) {
            return true;
        } else {
            return false;
        }
    }
}

6)启动Tomcat,输入http://localhost:8080/login.html

 

输入用户名admin和密码1234,点击登录进入欢迎页面:

如果用户名或者密码输入错误,则显示错误信息:

 

7)项目结构:

 

 

 

2、JSP模型2(MVC)

  在该模型中,JSP页面用于表示层,Servlet负责处理各类任务。Servlet作为一个控制器,负责处理请求并创建JSP页面所需的任何Bean。该控制器也负责确定将该请求传递到哪个JSP页面。JSP页面检索Servlet创建的对象,并其他动态内容插入在一个模板中。该模型促进了模型视图控制器(MVC)体系结构风格设计模式的使用。MVC其实是模型、视图、控制器的缩写。在使用JSP时,有相应的文件去实现相应的操作。通常JSP只负责视图,也就是只负责显示页面。业务逻辑等由Bean去实现。
  在MVC设计模型中,View用来呈现数据处理结果,可以是JSP、XML、HTML;Model用来存储数据的状态,可利用JavaBean来实现;Controller负责协调应用程序的运行流程,可用Servlet来实现。运用这种模式可以将数据呈现方式与数据处理方式分离,提高了代码的利用率。
1)登录页面login.html:
<html>
    <head><title>用户登录</title></head>
    <body>
        <form method="post" action="controller">
            <input type="hidden" name="action" value="login">
            用户名:<input type="text" name="name"><br>&nbsp;&nbsp;码:<input  type="password" name="password"><p>
            <input type="submit" value="登录">
            <input type="reset" value="重置">
        </form>
    </body>
</html>

2)信息错误显示页面loginerr.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
用户名或密码错误,请<a href="login.html">重新登录</a>

3)欢迎页面welcome.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<jsp:useBean id="user" scope="session" class="com.yyq.bean.UserBean"/>
欢迎你,<jsp:getProperty name="user" property="name"/>!

4)用户UserBean类:

package com.yyq.bean;
/**
 * Created by gao on 16-4-18.
 */
public class UserBean {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

5)用户检查UserCheckBean类:

package com.yyq.bean;
/**
 * Created by gao on 16-4-18.
 */
public class UserCheckBean {
    private UserBean user;
    public UserCheckBean() {
    }
    public UserCheckBean(UserBean user) {
        this.user = user;
    }
    public UserBean getUser() {
        return user;
    }
    public void setUser(UserBean user) {
        this.user = user;
    }
    public boolean validate() {
        String name = user.getName();
        String password = user.getPassword();
        //实际应用中,你应该查询数据库,验证用户名和密码。
        if ("admin".equals(name) && "1234".equals(password)) {
            return true;
        } else {
            return false;
        }
    }
}

6)控制类 ControllerServlet类:

package com.yyq.controller;
import com.yyq.bean.UserBean;
import com.yyq.bean.UserCheckBean;
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 java.io.IOException;
/**
 * Created by gao on 16-4-18.
 */
public class ControllerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("GBK");
        resp.setContentType("text/html;charset=GBK");
        String action = req.getParameter("action");
        if (!isValidated(req) && !("login".equals(action))) {
            gotoPage("login.html", req, resp);
            return;
        }
        if ("login".equals(action)) {
            UserBean user = new UserBean();
            user.setName(req.getParameter("name"));
            user.setPassword(req.getParameter("password"));
            UserCheckBean uc = new UserCheckBean(user);
            if (uc.validate()) {
                HttpSession session = req.getSession();
                //将user对象保存到Session对象中,在welcome.jsp中
                //通过<jsp:userBean>动作元素从Session中得到user对象
                session.setAttribute("user", user);
                //验证成功,将请求转向welcome.jsp
                gotoPage("welcome.jsp", req, resp);
            } else {
                //验证失败,将请求转向loginerr.jsp
                gotoPage("loginerr.jsp", req, resp);
            }
        }
        //对于其它的action请求,可在后面的else if...else语句中继续处理。
        /*else if
        {
        }
        else
        {
        }*/
    }
    /**
     * 判断用户是否已经登录了。
     */
    private boolean isValidated(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("user") != null)
            return true;
        else
            return false;
    }
    /**
     * 将请求导向指定的页面。
     */
    private void gotoPage(String targetURL, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        RequestDispatcher rd;
        rd = request.getRequestDispatcher(targetURL);
        rd.forward(request, response);
    }
}

7)项目结构:

 

posted @ 2016-04-25 20:45  我是一名老菜鸟  阅读(5580)  评论(0编辑  收藏  举报