JavaWeb:一个Servelt多个请求
一个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多个请求的更多相关文章
- 一个完整的Http请求
一个完整的http请求,通常有以下7点: 1.建立tcp连接 2.web浏览器web服务器发送请求命令 3.web浏览器发送请求头信息 4.web服务器应答 5.web服务器发送应答信息 6.web服 ...
- 无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。
新建一个MVC4的项目,引用DAL后,将DAL的连接字符串考入: <connectionStrings> <add name="brnmallEntities&qu ...
- “无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性 “之解决
今天在学习插件系统设计的时候遇到一个问题:“System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个请求的类型. 于是百度一下,很多内容都差不多 ...
- 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...
- 实现一个简单的http请求工具类
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
- 一个标准的AJAX请求
这是一个标准的ajax请求: $.ajax({ type:"post", url:basePath+"/resourcePush/operationLog", ...
- 一个简易的Http请求转发器
这两天一直再看微信开发,临时在我的电脑搭了个IIS服务器做微信开发,外网也能访问了,关键是,调试太麻烦了!! 我写完代码,要将代码发布到IIS才能接收微信消息,可是在这个过程中,我不知道微信发过来的是 ...
- wpf prism4 出现问题:无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。
WPF Prism 框架 程序 出现 问题: 无法加载一个或多个请求的类型.有关更多信息,请检索 LoaderExceptions 属性. 1.开始以为是配置的问题,找了半天,最后原来是有个依赖类库没 ...
- 一个完整的http请求响应过程
一. HTTP请求和响应步骤 图片来自:理解Http请求与响应 以上完整表示了HTTP请求和响应的7个步骤,下面从TCP/IP协议模型的角度来理解HTTP请求和响应如何传递的. 二.TCP/IP协 ...
随机推荐
- vue,vux判断字符串是否是undefined
if (typeof thisObj.city === 'undefined') { return}
- 完整的Django入门指南学习笔记2
part2: 前沿 在第一节中,我们安装了项目所需要的一切:Python3.6以及在虚拟环境中运行的Django2.0,这部分教程继续在项目上编写代码. 开始写代码前,先讨论下项目的相关背景知识,然后 ...
- python-day81--Ajax
一.准备知识:json 1.什么是json? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.任何的语言之间都可以用json进行数据的交 ...
- UI基础一:值节点赋值
METHOD EH_ONSEARCH. *CALL METHOD SUPER->EH_ONSEARCH ** EXPORTING ** HTMLB_EVENT = ** HTMLB_EVENT_ ...
- Linux下Tomcat项目启动报错
Linux下Tomcat项目启动报错 org.springframework.beans.factory.CannotLoadBeanClassException: Error loading cla ...
- ActiveMQ 集群和主从
举例说明:假设有 3 个 broker 节点,分别是61616,61618, 61620,其中 61616 和 61618 组成主.从节点,而 61616(或61618)和 61620 构成集群.61 ...
- zhuan 常用图像数据集:标注、检索
目录(?)[+] 1.搜狗实验室数据集: http://www.sogou.com/labs/dl/p.html 互联网图片库来自sogou图片搜索所索引的部分数据.其中收集了包括人物.动物. ...
- linux用户管理 用户和用户组管理
用户组的基本命令 groupadd [选项] [参数] -g 指定新建工作的id -r 创建系统工作组,系统工作组的ID小于500,非系统工作组大于500 -K 覆盖配置文件"/etc/lo ...
- xml的方式配置AOP:Aspect Oriented Programming
在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentServ ...
- javascript es6系列教程 - 不定参数与展开运算符(...)
三个点(...)在es6中,有两个含义: 用在形参中, 表示传递给他的参数集合, 类似于arguments, 叫不定参数. 语法格式: 在形参面前加三个点( ... ) 用在数组前面,可以把数组的值 ...