在Servlet开发的工程实践中,为了减少过多的业务Servlet编写,会采用构建公共Servlet的方式,通过反射来搭建轻量级的MVC框架,从而加快应用开发。

关于Servlet开发的基础知识,请看:JavaWeb开发之详解Servlet及Servlet容器

前后端交互的基本形式

一般来说,前端提交数据请求有三种基本方式,分别是表单、链接和Ajax

1. 按钮

 <form action="/BaseServlet/ServletDemo02?method=addStu" method="post">
用户<input type="text" name="username"/><br/>
<button>提交</button>
</form>

2. 链接

<a href="/BaseServlet/ServletDemo02?method=delStu">删除学生</a><br/>

3. Ajax

 <button onclick="fn()">按钮</button>
<script>
function fn(){
$.post("/BaseServlet/ServletDemo02",{"method":"checkStu","user":"tom"},function(data){
alert(data);
});
}

在Servlet开发的语境中,它们的共同点都是:指定处理的Servlet类路径(在web.xml中指定)以及附带在请求中的“method”参数

通过调用request参数匹配业务处理逻辑

前端按照以上方法发起请求,Servlet容器就会把请求交给对应的Servlet处理,并且附带上形如 method=delStu 的参数,利用这个原理,就可以构建一个基础Servlet类,用来优化开发:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端提交到服务端的method对应的值
String md=request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path=null; //通过判断md中不同的内容来决定本次功能
if("addStu".equals(md)){
path=addStu(request, response);
}else if("delStu".equals(md)){
path=delStu(request, response);
}else if("checkStu".equals(md)){
path=checkStu(request, response);
}else if("".equals(md)){ }
if(null!=path){
//服务端的请求转发
request.getRequestDispatcher(path).forward(request, response);
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} protected String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("添加学生");
return "/test.html"; }
protected String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("删除学生");
return "/test.html"; }
protected String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("检查学生");
response.getWriter().println("DDDDDD");
return null;
}

以上的Servlet对请求进行了处理,通过获取index.html的请求,最后请求转发至目标页面,其核心思想是:

1. 提取request的method参数的值;

2. 定义变量,存储请求转发的路径;

3. 通过判断method参数中的值的内容,来决定调用哪个业务功能。

4. 完成业务功能后,使用请求转发处理

通过反射匹配业务处理逻辑

当业务量多的时候,以上实践仍是不够,此时比较好的方式就是采用反射。

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端提交到服务端的method对应的值
String md=request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path=null;
//获取到当前字节码对象(ServletDemo02.class在内存中对象)
Class<? extends ServletDemo02> clazz = this.getClass();
try {
//获取clazz上名称为md, 参数为HttpServletRequest和HttpServletResponse的方法
Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
if(null!=method){
//调用找到的方法
path=(String)method.invoke(this, request,response);
}
if(null!=path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
} } public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("添加学生");
return "/test.html"; }
public String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("删除学生");
return "/test.html"; }
public String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("检查学生");
response.getWriter().println("DDDDDD");
return null;
}

1. 提取request的method参数的值;

2. 定义变量,存储请求转发的路径;

3. 获取到当前Servlet对象在内存中的Class对象

4. 获取Class对象上名称为md, 参数为HttpServletRequest和HttpServletResponse的方法

5. 根据是否返回方法对象,调用业务功能并使用请求转发处理

Java HttpServlet的父类GenericServlet,就是通过这个方法来调取HttpServlet的doGet或doPost方法的。

最佳实践

在实践开发中,一般会搭建一个BaseServlet,继承了HttpServlet并重写其Service方法,通过反射来找到业务功能子类对应的业务方法。

 public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 12197442526341123L; @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("service.....");
//获取客户端提交到服务端的method对应的值
String md=request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path=null;
//获取到当前字节码对象(ServletDemo02.class在内存中对象)
Class<? extends BaseServlet> clazz = this.getClass();
try {
//获取clazz上名称为md方法
Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
if(null!=method){
//调用找到的方法
path=(String)method.invoke(this, request,response);
}
if(null!=path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
} }

而业务功能子类,则继承BaseServlet,专注于业务逻辑的开发

 public class ServletDemo03 extends BaseServlet {

     private static final long serialVersionUID = 11248215356242123L;

     public ServletDemo03() {
System.out.println("没有参数的构造函数");
} public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("添加学生");
return "/test.html"; }
public String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("删除学生");
return "/test.html"; }
public String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("检查学生");
response.getWriter().println("DDDDDD");
return null;
} }

Java Servlet开发的轻量级MVC框架最佳实践的更多相关文章

  1. 开发自己PHP MVC框架(一)

    本教程翻译自John Squibb 的Build a PHP MVC Framework in an Hour,但有所改动,原文地址:http://johnsquibb.com/tutorials 这 ...

  2. 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。

    本文转自http://www.cnblogs.com/davidwang456/p/4090058.html 感谢作者 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并 ...

  3. .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比

    在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy的优势在哪里?和微软的MVC比 ...

  4. 《深入理解Java虚拟机:JVM高级特性与最佳实践》【PDF】下载

    <深入理解Java虚拟机:JVM高级特性与最佳实践>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062566 内容简介 作为一位 ...

  5. 读书笔记-《深入理解Java虚拟机:JVM高级特性与最佳实践》

    目录 概述 第一章: 走进Java 第二章: Java内存区域与内存溢出异常 第三章: 垃圾收集器与内存分配策略 第四章: 虚拟机性能监控与故障处理 第五章: 调优案例分析与实战 第六章: 类文件结构 ...

  6. openresty 前端开发轻量级MVC框架封装一(控制器篇)

    通过前面几章,我们已经掌握了一些基本的开发知识,但是代码结构比较简单,缺乏统一的标准,模块化,也缺乏统一的异常处理,这一章我们主要来学习如何封装一个轻量级的MVC框架,规范以及简化开发,并且提供类似p ...

  7. [Java] 使用 Spring 2 Portlet MVC 框架构建 Portlet 应用

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-spring2-portal/ Spring 除了支持传统的基于 Servlet 的 Web 开发之 ...

  8. .NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy

    当我们要接到一个新的项目的时候,我们第一时间想到的是用微软的MVC框架,但是你是否想过微软的MVC是不是有点笨重?我们这个项目用MVC是不是有点大材小用?有没有可以替代MVC的东西呢?看到这里也许你会 ...

  9. web开发中的MVC框架与django框架的MTV模式

    1.MVC 有一种程序设计模式叫MVC,核心思想:分层,解耦,分离了 数据处理 和 界面显示 的代码,使得一方代码修改了不会影响到另外一方,提高了程序的可扩展性和可维护性. MVC的全拼为Model- ...

随机推荐

  1. samba服务器配置及window网络磁盘映射

    1. Samba服务器工作原理 客户端向Samba服务器发起请求,请求访问共享目录,Samba服务器接收请求,查询smb.conf文件,查看共享目录是否存在,以及来访者的访问权限,如果来访者具有相应的 ...

  2. jquery訪问ashx文件演示样例

    .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,能够通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程.事实上就是带HTM ...

  3. 【服务器】Https服务配置

    1)利用openssl生成证书 2)再次修改nginx配置文件nginx.conf中的server配置 ① 是默认监听http请求的8080端口的 server    (再次修改,第一次是在 用ngi ...

  4. c# speech 文本转语言

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  5. Exp02

    使用netcat后门工具 原理示意图 使用netcat获取主机操作Shell,cron启动 Win获取Linux Shell Linux获取Win Shell cron启动 用man -k指令查看有关 ...

  6. [Deep-Learning-with-Python]基于Keras的房价预测

    预测房价:回归问题 回归问题预测结果为连续值,而不是离散的类别. 波士顿房价数据集 通过20世纪70年代波士顿郊区房价数据集,预测平均房价:数据集的特征包括犯罪率.税率等信息.数据集只有506条记录, ...

  7. ECMAScript6——Set数据结构

    /** * 数据结构 Set */ // ----------------------------------------------------- /** * 集合的基本概念:集合是由一组无序且唯一 ...

  8. [BZOJ4082][Wf2014]Surveillance[倍增]

    题意 给你一个长度为 \(len\) 的环,以及 \(n\) 个区间,要你选择尽量少的区间,使得它们完全覆盖整个环.问最少要多少个区间. \(len,n\leq 10^6\) . 分析 考虑普通的区间 ...

  9. Macaca 基础原理浅析

    导语 前面几篇文章介绍了在Macaca实践中的一些实用技巧与解决方案,今天简单分析一下Macaca的基础原理.这篇文章将以前面所分享的UI自动化Macaca-Java版实践心得中的demo为基础,进行 ...

  10. 一个Python开源项目-腾讯哈勃沙箱源码剖析(上)

    前言 2019年来了,2020年还会远吗? 请把下一年的年终奖发一下,谢谢... 回顾逝去的2018年,最大的改变是从一名学生变成了一位工作者,不敢说自己多么的职业化,但是正在努力往那个方向走. 以前 ...