一个Servelt多个请求

基础模拟

方法一:使用switch方法

 一、方法介绍

  • 方法:switch
  • 优点:方法简单,明了
  • 缺点:维护麻烦,保密性不好

二、代码实现

1、servlet类

package servlet;
/**
* 使用参数进行多个请求
*/ import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet("/CustomerServelet")
public class CustomerServelet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("method");
switch (method){
case "add":
add(request,response);
break;
case "update":
update(request,response);
break;
case "delet":
delet1(request,response);
break;
default:
break;
}
}

  //删除
private void delete(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delete");
}
  //更新
private void update(HttpServletRequest request, HttpServletResponse response) {
System.out.println("update");
}
  //添加
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("add");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}

2、jsp测试文件

<body>
  <a href="CustomerServelet?method=add">Add</a>
  <a href="CustomerServelet?method=update">Update</a>
  <a href="CustomerServelet?method=delete">Delete</a>
</body>

方法二:使用Java反射方法

 一、方法介绍

  • 方法:Java反射
  • 优点:保密性好,易于维护
  • 缺点:比较麻烦

二、代码实现

1、servlet类

package servlet;

import mvcdemo.CriteriaCustomer;
import mvcdemo.CustomerDAO;
import mvcentity.Customer;
import mvcimpl.CustomerDAOJdbcImpl; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List; @WebServlet("*.do")
//或者
//@WebServlet("*.do")//符合“*.do”的格式,则映射到里面。
public class CustomerServelet1 extends HttpServlet {
private static final long serialVersionUID=1L; private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到请求路径,格式为“/add.do”
String serveletPath=request.getServletPath();     //格式路径,获取方法名
String methodName=serveletPath.substring();//此时格式“*.do"
methodName=methodName.substring(,methodName.length()-);//此时格式"*"      //利用反射获取methodName对应的方法
try{
Method method=getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this,request,response);
}catch (Exception e){
e.printStackTrace();
} } //删除private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
System.out.println("delete");
}

  //更新
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
System.out.println("update");
} //增加private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
      System.out.println("add");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
}
}

2、jsp测试文件

<body>
<a href="add.do">Add</a>
<a href="update.do">Update</a>
<a href="delete.do">Delete</a>
</body>

三、说明

当运行jsp文件,并且点击其超链接,可以在控制台中显示出,相应的数据说明成功

JavaWeb:一个Servelt多个请求的更多相关文章

  1. 一个完整的Http请求

    一个完整的http请求,通常有以下7点: 1.建立tcp连接 2.web浏览器web服务器发送请求命令 3.web浏览器发送请求头信息 4.web服务器应答 5.web服务器发送应答信息 6.web服 ...

  2. 无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。

    新建一个MVC4的项目,引用DAL后,将DAL的连接字符串考入: <connectionStrings>     <add name="brnmallEntities&qu ...

  3. “无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性 “之解决

    今天在学习插件系统设计的时候遇到一个问题:“System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个请求的类型. 于是百度一下,很多内容都差不多 ...

  4. 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)

    在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...

  5. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  6. 一个标准的AJAX请求

    这是一个标准的ajax请求: $.ajax({ type:"post", url:basePath+"/resourcePush/operationLog", ...

  7. 一个简易的Http请求转发器

    这两天一直再看微信开发,临时在我的电脑搭了个IIS服务器做微信开发,外网也能访问了,关键是,调试太麻烦了!! 我写完代码,要将代码发布到IIS才能接收微信消息,可是在这个过程中,我不知道微信发过来的是 ...

  8. wpf prism4 出现问题:无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。

    WPF Prism 框架 程序 出现 问题: 无法加载一个或多个请求的类型.有关更多信息,请检索 LoaderExceptions 属性. 1.开始以为是配置的问题,找了半天,最后原来是有个依赖类库没 ...

  9. 一个完整的http请求响应过程

    一. HTTP请求和响应步骤   图片来自:理解Http请求与响应 以上完整表示了HTTP请求和响应的7个步骤,下面从TCP/IP协议模型的角度来理解HTTP请求和响应如何传递的. 二.TCP/IP协 ...

随机推荐

  1. python生成随机整数

    python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...

  2. PHP单例模式实例,连接数据库对类的引用

    <?php//单例模式连接数据库class pzhang{ static private $instance; private static $config; private $dbase = ...

  3. 2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test

    题意:找三条同起点同终点的不相交的路径 题解:用tarjan的思想,记录两个low表示最小和次小的dfs序,以及最小和次小的位置,如果次小的dfs序比dfn小,那么说明有两条返祖边,那么就是满足条件的 ...

  4. 有一个问题关于stl函数中的算法问题

    是不是stl中的算法函数中参数只要是和函数相关的就是函数对象和谓词?

  5. JQ 实现监测input中值的变化并绑定到另个input

                    $('#input').bind('input propertychange', function () {                     $('#myDiv ...

  6. poj1002 大数的 n的m次

    import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(S ...

  7. oracle配置访问白名单教程

    出于提高数据安全性等目地,我们可能想要对oracle的访问进行限制,允许一些IP连接数据库或拒绝一些IP访问数据库. 当然使用iptables也能达到限制的目地,但是从监听端口变更限制仍可生效.只针对 ...

  8. 快捷键设置 keyiing.json

    // 快捷键设置 keyiing.json // 将键绑定放入此文件中以覆盖默认值 [     /* // 转换大写     {         "key" : "ctr ...

  9. toolTip(用svg制作出富有动态的对话框)

    昨晚看了用svg如何制作富有动态的tooltip,于是今天就心血来潮学着做一下,于是也成功做出来,也明白其中的原理,收获颇多阿!接下来要多去学习svg,这是个好东西. 这其中也注意了一些平时纠结的细节 ...

  10. 1-find

    查找算法 #include <stdio.h> #include <assert.h> #define FALSE 0 #if 1 //array method int fin ...