本博客为原创:综合 尚硅谷(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解析的更多相关文章

  1. [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  5. [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. [原创]java WEB学习笔记20:MVC案例完整实践(part 1)---MVC架构分析

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  7. [原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

随机推荐

  1. 第九节: 利用RemoteScheduler实现Sheduler的远程控制 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案 第六节: 六类Calander处理六种不同的时间场景 第五节: Quartz.Net五大构件之Trigger的四大触发类 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联

    第九节: 利用RemoteScheduler实现Sheduler的远程控制   一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上 ...

  2. SpringCloud系列三:将微服务注册到Eureka Server上

    1. 回顾 通过上篇博客的讲解,我们知道硬编码提供者地址的方式有不少问题.要想解决这些问题,服务消费者需要一个强大的服务发现机制,服务消费者使用这种机制获取服务提供者的网络信息.不仅如此,即使服务提供 ...

  3. 盘古分词demo,盘古分词怎么用

    1.下载PanGu.dll dll地址:http://download.csdn.net/detail/dhfekl/7493687 2.将PanGu.dll和词库引入到项目 最新词库地址:http: ...

  4. UVA 11354 - Bond (最小生成树 + 树链剖分)

    题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...

  5. Hadoop2.6.0子项目hadoop-mapreduce-examples的简介

    引文 学习Hadoop的同学们,一定知道假设执行Hadoop自带的各种样例,以大名鼎鼎的wordcount为例,你会输入下面命令: hadoop org.apache.hadoop.examples. ...

  6. [译]GLUT教程 - 键盘高级特性

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Advanced Keyboard 本节我们会介绍另外4个处理键盘事件的 ...

  7. 正负小数点后两位浮点数--jquery

    背景:项目中需要做个对两位小数点的正负浮点数的处理, 要求:非数字或者.字符自动清除,并对.12自动修补.前的0 原理:在输入框中加入两个事件,keyup与blur,keyup处理字符串中非要求的字符 ...

  8. PYTHON流向下载

    #-*- coding:utf-8 -*- import gzip import re import http.cookiejar import urllib.request import urlli ...

  9. log4j日志异步化大幅提升系统性能

    .log4j已成为大型系统必不可少的一部分,log4j可以很方便的帮助我们在程序的任何位置输出所要打印的信息,便于我们对系统在调试阶段和正式运行阶段对问题分析和定位.由于日志级别的不同,对系统的性能影 ...

  10. Idiomatic Phrases Game(图论最短路)

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...