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协 ...
随机推荐
- 报错 hint: Updates were rejected because the remote contains work that you do 解决方法
1. git pull origin master --allow-unrelated-histories 2.git pull origin master 3.git init 4.git remo ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
- Date类型与String类型的相关问题
今天完成boss交代的任务时,遇到Date类型与String类型的相关问题,参考了网上的一些例子,并且自己写了demo,现在记录下了总结一下: (一)判断一个字符串是不是合法的日期格式 public ...
- 『Python』skimage图像处理_旋转图像
一段简短的实现图像旋转的代码,使用了skimage库,据说和PIL相比,skimage对numpy等科学计算库的支持更好,这里是为了完成师兄给的帮他修改程序的任务,如果以后有需求的话可能会对pytho ...
- Codeforces Round #503 (by SIS, Div. 1)E. Raining season
题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...
- 安装sql 2008步骤以及所遇到的问题
下载网址:http://www.xiazaiba.com/html/4610.html 安装步骤: 1. 在Windows7操作系统系,启动Microsoft SQL 2008安装程序后,系统兼容性 ...
- zend framwork黑箱测试
1.我采用的测试时phpunit 1).按照:https://phpunit.de/ 把下载的文件放到C:/window 目录,让后修改一下文件的后缀,使在敲击命令的时候可以不用敲文件的全称 如:p ...
- ubuntu 安装国际qq
下载地址::http://pan.baidu.com/s/1nt1Nu6P 解压后,输入终端:suao dpkg -i 解压的文件名称.deb 如果你前面安装过qq,你可以输入sudo dpkg -P ...
- UI基础四:简单的assign block
经常会有需求让在标准的order加个assign block,那就来简单说一下: 1.创建assign block组件ZXXXXXX 2.添加BTORDER节点和GUID属性 3.创建表视图(可配置, ...
- WDA基础三:简单的INPUT选择,简单的TABLE显示
先从基本的开始,简单的单选和TABLE显示 1.创建选择条件节点,CONTEXT页签,右键CONTEXT创建NODE,对应1:1 1:1 lead selection 2.创建结果节点,对应0:n ...