JSP Model模式

用JSP开发的Web应用模型可以分为Model1和Model2

对于小型的Web应用,通常可以使用模型1来完成。

模型1可以分为两种方式:

一种是完全使用JSP页面来开发Web应用;

另一种是使用JSP页面和JavaBean相结合的方式。由JSP页面来接收客户端请求,用JavaBean或其它服务来完成业务逻辑和生成返回页面。

实战:Model1模式录入商品信息

1、构建javabean对象(Goods.java)

package com.wuyudong;
public class Goods {
    private String name;
    private double price;
    private String description;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    
}

2、创建GoodsDao类来封装对数据库的操作

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
 
public class GoodsDao {
    public void saveGoods(Goods goods) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/db_database05";
            Connection conn = DriverManager.getConnection(url, "root",
                    "wuyudong");
            String sql = "insert into tb_goods(name,price,description)values(?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, goods.getName());
            ps.setDouble(2, goods.getPrice());
            ps.setString(3, goods.getDescription());
            ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

index.jsp文件

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <form action="service.jsp" method="post" onsubmit="return save(this);">
        <table border="1" align="center" width="300">
            <tr>
                <td align="center" colspan="2"><br>
                <h1>录入商品信息</h1></td>
            </tr>
            <tr>
                <td align="right">商品名称</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td align="right">价 格:</td>
                <td><input type="text" name="price"></td>
            </tr>
            <tr>
                <td align="right">商品描述:</td>
                <td><textarea rows="3" cols="30" name="description"></textarea></td>
            </tr>
            <tr>
                <td align="center" colspan="2"><input type="submit" value="提 交">
                    <input type="reset" value="重 置"></td>
            </tr>
        </table>
    </form>
</body>
</html>

service.jsp用于处理表单请求并向数据库中添加数据

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("GBK");
    %>
    <jsp:useBean id="goods" class="com.wuyudong.Goods"></jsp:useBean>
    <jsp:setProperty property="*" name="goods" />
    <jsp:useBean id="goodsDao" class="com.wuyudong.GoodsDao"></jsp:useBean>
    <%
        goodsDao.saveGoods(goods);
    %>
    <a href="index.jsp">返回添加页面</a>
</body>
</html>

在模型2中,使用了JSP、JavaBean和Servlet三种技术。

其中,Servlet扮演一个控制者的角色。

  1. 浏览器的请求首先发送给Servlet;
  2. Servlet执行流程控制,并根据需要创建JavaBeans对象,这些JavaBeans对象完成诸如访问数据库等服务,并返回处理结果;
  3. 最后Servlet再将处理结果交由JSP文件生成网页返回给客户端。

在模型2中,JSP文件不再处理业务逻辑。  它的主要功能是生成返回给客户端的网页。

在模型2中,各个开发者的角色划分非常明确。  因此,对于复杂的Web应用开发,使用模型2的优点不言而喻。

(但对于小型应用,还是使用模型1更简单)

MVC模型

MVC模型是一种将应用分解成三个独立部分的通用模型(并不单指JSP应用)。

这三个部分分别是:

模型(Model):描述系统的数据

视图(view):数据的显示,包括图形、文本和  文件输出等;

控制器(Controller):获取系统的输入,控制系  统的执行;

JSP模型2其实就是一种MVC体系结构,也称为MVC模型2。

其中,Servlet处理所有请求,并执行业务逻辑,相当于控制器(Controller)的作用;

而JavaBeans用于操作各种数据和对象,相当于模型(Model)。

JSP文件用于生成返回给客户端的页面,则相当于视图组件(View)。

实战:Model2录入商品信息

index.jsp

<body>
        <form action="GoodsServlet" method="post" onsubmit="return save(this);">
            <table border="1" align="center" width="300">
                <tr>
                    <td align="center" colspan="2">
                        <br><h1>录入商品信息</h1>
                    </td>
                </tr>
                <tr>
                    <td align="right">商品名称:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td align="right">价 格:</td>
                    <td><input type="text" name="price"></td>
                </tr>
                <tr>
                    <td align="right">商品描述:</td>
                    <td><textarea name="description" cols="30" rows="3"></textarea></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <input type="submit" value="提 交">
                        <input type="reset" value="重 置">
                    </td>
                </tr>
            </table>
        </form>
    </body>

GoodsServlet类

package com.wuyudong;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class GoodsServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 291800654190966979L;
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("GBK");
        req.setCharacterEncoding("GBK");
 
        PrintWriter out = resp.getWriter();
 
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String description = req.getParameter("description");
 
        Goods goods = new Goods();
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setDescription(description);
 
        GoodsDao goodsDao = new GoodsDao();
        goodsDao.saveGoods(goods);
        out.print("保存商品成功!");
        out.flush();
        out.close();
    }
}

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>GoodsServlet</servlet-name>
    <servlet-class>com.wuyudong.GoodsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GoodsServlet</servlet-name>
    <url-pattern>/GoodsServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
原文地址:https://www.cnblogs.com/wuyudong/p/jsp-model.html