客户端向服务端发送数据的几种方式
通用的servlet
1、分析
方式1:一个路径对应一个servlet
方式2:多个路径对应一个servlet,method请求参数与if语句进行分流
创建动态项目工程
从客户端向服务端发起请求,调用功能的几种方式:
1、通过表单向服务端发起请求
2、通过链接向服务端发起请求
3、通过AJAX向服务端发起请求
在WebContent下新建一个index.html
新建一个js文件夹,导入JQuery的
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入js 架包 -->
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
</head>
<body>
<form action="/BaseServlet/ServletDemo01?method=addStu" method="post">
用户:<input type="text" name="username"/><br/>
<button>提交</button>
</form>
<br/>
<!-- 通过a标签的herf向客户端发送请求,herf携带对应的数据?method=delStu(?携带数据) -->
<a href="/BaseServlet/ServletDemo01?method=delStu">删除学生</a><br/>
<button onclick="fn()">按钮</button>
</body>
<script type="text/javascript">
function fn() {
/* $.post("往什么位置发",{传什么数据键值对的形式},function(从servlet响应回来的数据){}); */
$.post("/BaseServlet/ServletDemo01",{"method":"checkStu","user":"tom"},function(data){
alert(data);
});
}
</script>
</html>
创建一个包和Servlet
原理:从客户端向服务端发起请求,每次都要传递额外的键值对的数据method="",服务端获取到method对应的内容之后,通过判断不同的内容来调用不同的功能。
package cn.itzheng.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo01
*/
public class ServletDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取客户端提交服务端的method对应的值
String md = request.getParameter("method");
System.out.println(md);
//定义,功能执行完毕之后要转发的路径
String path = null;
// 通过判断md当中不同的内容来决定本次的功能
if ("addStu".equals(md)) {
path = addStu(request, response);
} else if ("delStu".equals(md)) {
path = delStu(request, response);
} else if ("checkStu".equals(md)) {
path = checkStu(request, response);
} else if ("".equals(md)) {
}
if (null != path) {
//服务端的一个转发
request.getRequestDispatcher(path).forward(request, response);
}
}
protected String addStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端添加学生");
// 转发
return "/test.html";
}
protected String delStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端删除学生");
// 转发
return "/test.html";
}
protected String checkStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端检查学生");
// 转发
response.getWriter().println("DDDDDDD");//将DDDD发送到客户端
return null;
}
}
演示Servlet第二种获取到客户端的数据并对其进处理
再次新建一个index2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入js 架包 -->
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
</head>
<body>
<form action="/BaseServlet/ServletDemo02?method=addStu" method="post">
用户:<input type="text" name="username"/><br/>
<button>提交</button>
</form>
<br/>
<!-- 通过a标签的herf向客户端发送请求,herf携带对应的数据?method=delStu(?携带数据) -->
<a href="/BaseServlet/ServletDemo02?method=delStu">删除学生</a><br/>
<button onclick="fn()">按钮</button>
</body>
<script type="text/javascript">
function fn() {
/* $.post("往什么位置发",{传什么数据键值对的形式},function(从servlet响应回来的数据){}); */
$.post("/BaseServlet/ServletDemo02",{"method":"checkStu","user":"tom"},function(data){
alert(data);
});
}
</script>
</html>
再次新建一个ServletDemo02
采用反射的方式获取到不同的方法简化Servlet的复杂度
package cn.itzheng.servlet;
import java.io.IOException;
//引入method反射下面的包
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo02
*/
public class ServletDemo02 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取客户端提交服务端的method对应的值
String md = request.getParameter("method");
System.out.println(md);
// 定义,功能执行完毕之后要转发的路径
String path = null;
// 通过判断md当中不同的内容来决定本次的功能
// 获取到当前字节码对象(ServletDemo02.class在内存当中形成对象)
Class clazz = this.getClass();
try {
// 获取clazz上名称为md的方法(由传入的md来决定获取到那个方法)
Method method = clazz.getMethod(md, HttpServletRequest.class, HttpServletResponse.class);
if (null != method) {
//调用找到的方法
path = (String) method.invoke(this, request, response);// 执行当前(this)对象的方法
}
if (null != path) {
// 服务端的一个转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String addStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端添加学生");
// 转发
return "/test.html";
}
public String delStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端删除学生");
// 转发
return "/test.html";
}
public String checkStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端检查学生");
// 转发
response.getWriter().println("DDDDDDD");// 将DDDD发送到客户端
return null;
}
}
如果需要增加功能还需要该动代码所以需要抽取一个公共类
需要新建一个Servlet的公共类
BaseServlet
package cn.itzheng.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseServlet extends HttpServlet {
// 重写HttpServletservice方法
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取客户端提交服务端的method对应的值
String md = request.getParameter("method");
System.out.println(md);
// 定义,功能执行完毕之后要转发的路径
String path = null;
// 通过判断md当中不同的内容来决定本次的功能
// 获取到当前字节码对象(ServletDemo02.class在内存当中形成对象)
Class clazz = this.getClass();
try {
// 获取clazz上名称为md的方法(由传入的md来决定获取到那个方法)
Method method = clazz.getMethod(md, HttpServletRequest.class, HttpServletResponse.class);
if (null != method) {
// 调用找到的方法
path = (String) method.invoke(this, request, response);// 执行当前(this)对象的方法
}
if (null != path) {
// 服务端的一个转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
新建一个ServletDemo03用来继承BaseDemo
Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁
一旦请求发往ServletDemo03之后ServletDemo03会被初始化然后调用init方法-----》然后运行service
ServletDemo03会自动向上的父类找对应的init方法和service方法
package cn.itzheng.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo03
*/
public class ServletDemo03 extends BaseServlet {
//当请求进入到这里时会调用其无参构造函数
/*
* Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁
*/
public ServletDemo03() {
System.out.println("无参构造函数");
//会调用父类的init方法,来初始化子类的
//然后会调用父类的service方法
}
public String addStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端添加学生");
// 转发
return "/test.html";
}
public String delStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端删除学生");
// 转发
return "/test.html";
}
public String checkStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("向服务端检查学生");
// 转发
response.getWriter().println("DDDDDDD");// 将DDDD发送到客户端
return null;
}
}
新建一个index.html
向ServletDemo03发送请求,Servlet的执行过程ServletDemo03的构造方法—>init方法初始化该类,然后执行service方法(如果子类没有则会一层一层的向上找对应的方法)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入js 架包 -->
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
</head>
<body>
<form action="/BaseServlet/ServletDemo03?method=addStu" method="post">
用户:<input type="text" name="username"/><br/>
<button>提交</button>
</form>
<br/>
<!-- 通过a标签的herf向客户端发送请求,herf携带对应的数据?method=delStu(?携带数据) -->
<a href="/BaseServlet/ServletDemo03?method=delStu">删除学生</a><br/>
<button onclick="fn()">按钮</button>
</body>
<script type="text/javascript">
function fn() {
/* $.post("往什么位置发",{传什么数据键值对的形式},function(从servlet响应回来的数据){}); */
$.post("/BaseServlet/ServletDemo03",{"method":"checkStu","user":"tom"},function(data){
alert(data);
});
}
</script>
</html>