[原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
多个请求对应一个Servlet解析
如果我们每一个请求对应一个Servelt,这样的话,代码就显得比较臃肿,最主要的是,也不方便系代码的管理和优化
方式一:

1. 思路:对于每一个页面请求我们设置成一个对应的方法,并且为请求的url设置相应的method参数,servlet-mapping 为 @WebServlet("/customerServlet") ,而在Servlet 中通过获取 method 参数不同的取值,通过一个switch 语句选择不同的方法,同时调用不同的方法。
2. 代码:test1.jsp , CustomerServlet1.java
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body> <a href="customerServlet?method=add">Add1</a>
<br><br> <a href="customerServlet?method=query">Query1</a>
<br><br> <a href="customerServlet?method=delete">Delete1</a>
<br><br>
<br><br>
<br><br> </body>
</html>
CustomerServlet1.java
package com.jason.mvcapp.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class CustomerServlet
*/
@WebServlet("/customerServlet")
public class CustomerServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L; /**
*
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
} protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); switch (method) {
case "add":
add(request, response);
break;
case "query":
query(request, response);
break;
case "delete":
delete(request, response);
break;
} } private void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("delete"); } private void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("query"); } private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("add");
} }
方法二:

1. 思路:将所有的请求都设置成 方法名.do, 而将 servlet-mapping 为 @WebServlet("*.do"),即响应所有以 .do 结尾的请求.在Servlet中通过反射,获取运行时类,之后invoke() 调用方法
2.代码:test2.jsp , CustomerServlet2.java
test2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试2</title>
</head>
<body> <a href="addCustomer.do">Add2</a>
<br><br> <a href="query.do">Query2</a>
<br><br> <a href="deleteCustomer.do">Delete2</a>
<br><br> <a href="update.do">Update2</a>
<br><br> <a href="editeCustomer.do">Edite2</a>
<br><br> </body>
</html>
CustomerServlet2.java
package com.jason.mvcapp.servlet; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class CustomerServlet2
*/
@WebServlet("*.do")
public class CustomerServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
} protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//1.获取servletPath:/add.do 或者 query.do
28 String serveltPath = request.getServletPath();
29
30 System.out.println(serveltPath);
31 //2.去除/ 和 .do 得到对应的方法,如 add query
32 String methodName = serveltPath.substring(1);
33 methodName = methodName.substring(0, methodName.length() - 3);
34 // System.out.println(methodName);
35
36 try {
37 //3.利用反射获取methodName对应的方法
38 Method method = getClass().getDeclaredMethod(methodName,
39 HttpServletRequest.class, HttpServletResponse.class);
40
41 //4.利用反射调用方法
42 method.invoke(this, request, response);
43 } catch (Exception e) {
44
45 e.printStackTrace();
46 }
} private void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("update"); } private void editeCustomer(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("edit"); } private void deleteCustomer(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("delete"); } private void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("query"); } private void addCustomer(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("add");
} }
总结:
1)理解由方式一过度到方法二;
2)理解反射获取当前类,获取提交的方法,及解析,调用相应的方法;
[原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析的更多相关文章
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记20:MVC案例完整实践(part 1)---MVC架构分析
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
随机推荐
- kvo&kvc
Key Value Coding Key Value Coding是cocoa的一个标准组成部分,它能让我们可以通过name(key)的方式访问property, 不必调用明确的property ac ...
- HTML5中两种方法实现客户端存储数据
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...
- C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)
需要下载ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; u ...
- Extjs4 Combobox 联动始终出现loading错误的解决的方法
当反复选者combobox 联动时,下级的Combobox 会出现loading的错误表现形式,尽管Store数据已载入完也是一样. 废话少说贴代码就知道怎样处理了:(注意红色部分的关键语句) }, ...
- Maven环境下搭建SSH框架之Spring整合Struts2
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Struts2:2.5.10 Spring:4.3.8.RELEASE 注意:其他版本在某些特性的使用上可能稍微存在差别 2.准备工作 ...
- TC2安装方法
电驴下载TC2英文原版安装文件,3 Disk,安装方法记录如下: cmd.exe chcp 437 挂载安装文件夹1到A盘 subst a: d:\c\Disk1 另开一个cmd,转到A盘,输入ins ...
- bootstrap-ui-datetime-picker插件学习
GitHub:https://github.com/Gillardo/bootstrap-ui-datetime-picker 准备 安装:bower install --save bootstrap ...
- 安卓TabHost+ViewPager+RadioGroup多功能模板整理
如今安卓比較流行的布局就是类似新闻client和手机QQ那种的底端可选择,上面的个别页面能够滑动选择. 在測试过程中发现用安卓自带的TabHost去构建.非常难得到自己定义的效果. 因此採用TabHo ...
- linux下性能测试工具netperf使用
一.功能简介 netperf是一款针对网络性能的测试工具,主要基于TCP或UDP的传输.根据应用的不同,可以进行批量数据传输(bulk data transfer)模式和请求/应答(request/r ...
- 尽管是一个CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题。现在,她正在玩一个数值变换的游戏。她发现计算机中经常用不同的进制表示同一个数,如十进制数123表达为16进制时只包含两位数7、11(B),用八进制表示时为三位数1、7、3。按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11。
include "stdafx.h" #include<iostream> #include<vector> #include <algorithm& ...