8、什么是JavaWeb三层架构思想以及如何使用Servlet

JAVAWeb开发的三层架构

JAVAWeb开发三层架构分为:

  • Controller层(起到获取用户请求合响应用户的作用)
  • Service层(主要起到处理业务逻辑的作用)
  • Dao层(主要和数据库打交道)

xRHdED.png

分层目的为了解耦,解耦就是为了降低代码的耦合度。方便项目后期的维护和升级。

在开发中一般给每个层应用起一些比较统一的包名

  • web层:…web/servlet/controller
  • service层:
    • …service:service接口包
    • …service.impl:service接口实现类
  • dao层:
    • …dao:dao:接口包
    • …dao.impl:dao接口的实现类
  • 实体bean对象
    • …pojo/entity/domain/bean:Javabean
  • 测试包:…test/junit
  • 工具类: …/Utils

如何使用Servlet

Servlet是运行在Web服务器上的程序,是JavaWeb开发的重要组件之一。它能够让我们处理客户端发送的请求

想要使用Servlet首先我们需要创建一个web项目

  • 1689064536159.png

配置tomcat服务器

  • 1689065410596.png

部署工件

  • 1689065440906.png

使用servlet处理请求

//该注解用来注册该Servelt控制器,并且配置Servlet得映射路径
@WebServlet("/deptController")
public class DeptController extends javax.servlet.http.HttpServlet {
    private IDeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //....
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //....
    }
}

如果我们想要我们得控制层能够处理客户端发送得请求,控制层得类需要继承javax.servlet.http.HttpServlet类,实现它得doGet或者doPost方法。并且使用@WebServlet("/deptController")来映射Servlet得路径。

  • doGet:该方法主要用来处理get请求,如果客户端是通过get发送得请求将会调用该方法
  • doPost:该方法主要用来处理Post请求。

参数

  • HttpServletRequest:该类用来获取客户端发送请求所带来得数据,请求头,请求体,它也可以获取Session等内容。
  • HttpServletResponse:该类主要用来给客户端返回一些数据

案例

该案例利用JavaWeb得三层架构配合Servlet实现基本得客户端发送请求,服务端响应数据功能

  • Dao层
public class DeptDaoImpl implements IDeptDao {
    @Override
    public List<Dept> list() {
        String sql = "select * from t_dept";
        List<Dept> depts = null;
        try {
            depts = JDBCUtil.queryForList(Dept.class, sql, JDBCUtil.getConnection());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return depts;
    }
}
  • Service层
@Override
    public List<Dept> list() {
        return deptDao.list();
    }
  • Controller层
    • 使用Servlet实现请求处理和响应
@WebServlet("/deptController")
public class DeptController extends javax.servlet.http.HttpServlet {
    private IDeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Dept> list = deptService.list();
        System.out.println(list);
        JSONUtil.transJson(list,resp);
    }
}

这里我们将返回得数据转换成JSON数据响应给客户端

// jsonutil
public class JSONUtil {
    public static void transJson(Object obj, HttpServletResponse response) throws IOException {
        String jsonString = JSON.toJSONString(obj);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonString);
    }
}

主要使用过得是fastjson第三方库来快速实现对象到json的转换

  • 客户端发送请求

    <script>
      new Vue({
        el:"#app",
        data: {
          depts:{},
        },
        mounted:function () {
          axios.get("http://localhost:8081/deptController").then(
                  (response)=>{
                    this.depts = response.data;
                  }
          );
        }
      })
    </script>
    
  • 数据渲染

    <table border="1">
          <tr>
            <th>部门号</th>
            <th>部门名称</th>
            <th>部门地址</th>
            <th>部门领导</th>
            <th>操作</th>
          </tr>
          <tr v-for="dept in depts">
            <td>
              {{dept.did}}
            </td>
            <td>
              {{dept.dName}}
            </td>
            <td>
              {{dept.dLocation}}
            </td>
            <td>
              {{dept.leader}}
            </td>
            <td>
              <button>编辑</button> |
              <button>删除</button>
            </td>
          </tr>
        </table>
    
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值