一个servlet处理多个请求,
原理:利用反射机制获取在selvlet类中的其他方法
1、前端页面

<%@ 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>测试一个Servlet处理多个请求</title>
</head>
<body> <h1>一个Servlet处理多个请求</h1>
<hr>
<a href="queryEmp.do">查询员工信息</a>
<br/>
<br/>
<a href="addEmp.do">新增员工信息</a>
<br/>
<br/>
<a href="deleteEmp.do">删除员工信息</a>
<br/>
<br/>
<a href="queryEmpList.do">查询所有员工信息</a> </body>
</html> 2、web.xml配置
    <!-- 处理多个请求 -->
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.servlet.userservlet.CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

3、servlet类
package com.servlet.userservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method; /**
* 使用一个servlet处理多个请求
* @author 登风
* @date 2018年6月14日
*/
public class CustomerServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求的URI地址信息
String url = request.getRequestURI();
// 截取其中的方法名
String methodName = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf("."));
Method method = null;
try {
// 使用反射机制获取在本类中声明了的方法
method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 执行方法
method.invoke(this, request, response);
} catch (Exception e) {
throw new RuntimeException("调用方法出错!");
}
} private void queryEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行查询员工的方法...");
response.setContentType("text/html;charset=utf8");
PrintWriter pw = response.getWriter();
pw.println("<h1>查询员工的方法</h1>");
pw.flush();
pw.close();
} private void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行新增员工的方法!!!");
response.setContentType("text/html;charset=utf8");
PrintWriter pw = response.getWriter();
pw.println("<h1>执行了新增员工的方法。。。</h1>");
pw.flush();
pw.close();
} private void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行删除员工的方法...");
response.setContentType("text/html;charset=utf8");
PrintWriter pw = response.getWriter();
pw.println("<h1>删除员工的方法</h1>");
pw.flush();
pw.close();
} private void queryEmpList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行查询所有员工的方法...");
response.setContentType("text/html;charset=utf8");
PrintWriter pw = response.getWriter();
pw.println("<h1>查询所有员工的方法</h1>");
pw.flush();
pw.close();
}
}

网页反参结果


 

如何用一个servlet处理多个请求的更多相关文章

  1. Servlet(五):一个Servlet处理多个请求

    一.为什么要使用一个Servlet来处理多个请求? 当浏览器发送了一次请求到服务器时,servlet容器会根据请求的url-pattern找到对应的Servlet类,执行对应的doPost或doGet ...

  2. 一个servlet处理多个请求(使用Method的反射机制)

    方法一 可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法.达到一个servlet处理多个请求的目的 test.jsp: <%@ page language= ...

  3. 【WEB小工具】BaseServlet—一个Servlet处理多个请求

    package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...

  4. BaseServlet,让一个servlet处理多个请求

    BaseServlet 第一次学习servlet的时候是跟着传智播客免费的教学视频,其中崔希凡讲的是我学过自认讲的最好的一位,BaseServlet也是跟着他写过一次,当时很多东西不能理解,后来慢慢发 ...

  5. 怎么在一个servlet中实现多个功能 ?如何使一个Servlet处理多个请求?

    自学javaweb一直不知道一个servelt可以有多个功能!看了别人代码才知道这个可以有! 平时你建立servelt时候你会吧doget和dopost这两个勾上,要想实现多个功能,你不必要勾选dog ...

  6. 一个servlet处理多个请求或者叫方法

    http://blog.csdn.net/qq_25201665/article/details/52037607

  7. 一个servlet如何处理多个请求

    页面1:表单的action=login?method=login 页面2:表单的action=login?method=insert ..... 然后通过method的值采用不同方法进行处理. 如下 ...

  8. Tomcat是一个Servlet容器?

    "Tomcat是一个Servlet容器",这句话对于2019年的程序员应该是耳熟能详的. 单纯的思考一下这句话,我们可以抽象出来这么一段代码: class Tomcat { Lis ...

  9. (二)第一个Servlet

    一.预备知识 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个 ...

随机推荐

  1. Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)

    BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using na ...

  2. [转]使用HttpOnly提升Cookie安全性

    原文:https://www.cnblogs.com/zlhff/p/5477943.html 在介绍HttpOnly之前,我想跟大家聊聊Cookie及XSS. 随着B/S的普及,我们平时上网都是依赖 ...

  3. Python字符串排序

    看了西红柿的博客,此文纯搬运,方便自己查询用. 原文:https://www.cnblogs.com/tomato0906/articles/4890701.html 1.python中的字符串类型是 ...

  4. 排序算法之归并排序的python实现

    采用分治法: 分割:递归地把当前序列平均分割成两半. 集成:在保持元素顺序的同时将上一步得到的子序列集成到一起(归并). 归并操作(归并算法),指的是将两个已经排序的序列合并成一个序列的操作.归并排序 ...

  5. 喵星之旅-狂奔的兔子-myeclipse搭建ssm

    . 可以使用试用期限内的myeclipse,也可以找到有授权的机器进行操作.搭建好的项目框架可以直接移植到免费软件eclipse使用.或者直接购买myeclipse授权. 一.创建一个java web ...

  6. springboot @Configuration @bean注解作用

    @Configuration注解可以达到在Spring中使用xml配置文件的作用 @Bean就等同于xml配置文件中的<bean> 在spring项目中我们集成第三方的框架如shiro会在 ...

  7. 分别用shell编程和c编程实现文件和目录的复制

    c编程参考:https://blog.csdn.net/maizi_hsx/article/details/78645698 makefile文件: copy:cp.o gcc cp.o -o cop ...

  8. AspectRatio图片的宽高比、Card 卡片组件

    一.AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度 ...

  9. java语法基础--动手动脑问题及课后实验问题

    ---恢复内容开始--- 动手动脑: 1:仔细阅读示例:EnumTest.java,运行它,分析运行结果 结果 :枚举类型是引用类型!枚举类型不属于原始数据类型,它的每个具体指都引用一个特定的对象.相 ...

  10. Linux修改本机/etc/hosts的hostName后经常不生效

    1.Linux修改本机别名/etc/hosts的hostName后经常不生效解决 Linux修改本机别名/etc/hosts的hostName后经常不生效, 比如我们/etc/hosts的内容如下: ...