整理一下当年的学习笔记之:多个请求使用同一个Servlet
2017-04-14 23:21
676 查看
参考此文章点击打开链接
package cn.itcast.web.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;
@SuppressWarnings("serial")
public abstract class BaseServlet extends HttpServlet{
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");//处理响应编码
/*
* 1、获取参数,用来识别用户想请求的方法
* 2、然后判断是哪一种方法,是哪一个我们就调用哪一个方法
*/
String methodName = request.getParameter("method");
if(methodName == null || methodName.trim().isEmpty()) {
throw new RuntimeException("您没有传递method参数!无法确定您想要调用的方法!");
}
/*
* 得到方法名称,是否可通过反射来调用方法?
* 1、得到方法名,通过方法名再得到Method类的对象
* * 需要得到Class,然后调用它的方法进行查询!得到Method
* * 我们要查询的是当前类的方法,所以我们要得到当前类的Class
*/
Class c = this.getClass();//得到当前类的Class
Method method = null;
try {
method = c.getMethod(methodName,
HttpServletRequest.class,HttpServletResponse.class);
} catch (Exception e) {
throw new RuntimeException("您要调用的方法" + methodName + ",它不存在!");
}
try {
/*
* 调用method表示的方法
*/
//正常调用this.addUser(request,response), 反射调用method(this,request,response)
String result = (String) method.invoke(this, request,response);//反射调用
/*
* 获取请求处理方法执行后返回的字符,它表示转发或重定向的路径!
* 帮它完成转发或重定向!
*/
/*
* 如果用户返回的是字符串null或“”,那么我们什么也不做!
*/
if(result == null || result.trim().isEmpty()) {
return;
}
/*
* 查看返回的字符串中是否包含冒号,如果没有,表示转发
* 如果有,使用冒号分割字符串,得到前缀和后缀!
* 其中前缀如果是f,表示转发,如果是r表示重定向,后缀就是转发或重定向的路径了!
*/
if(result.contains(":")) {
//使用冒号分割字符串,得到前缀和后缀
int index = result.indexOf(":");//获取冒号位置
String s = result.substring(0, index);//截取出前缀,表示操作
String path = result.substring(index+1);//截取出后缀,表示路径
if(s.equalsIgnoreCase("r")) {//如果前缀是r,那么重定向
response.sendRedirect(request.getContextPath() + path);
} else if(s.equalsIgnoreCase("f")) {
request.getRequestDispatcher(path).forward(request, response);
} else {
throw new RuntimeException("你指定的操作:" + s + "当前的版本还不支持");
}
} else {//没有冒号,默认为转发
request.getRequestDispatcher(result).forward(request, response);;
}
} catch (Exception e) {
System.out.println("您调用的方法:" + methodName + ",它的内部抛出了异常!");
throw new RuntimeException(e);
}
}
}
package cn.itcast.web.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;
@SuppressWarnings("serial")
public abstract class BaseServlet extends HttpServlet{
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");//处理响应编码
/*
* 1、获取参数,用来识别用户想请求的方法
* 2、然后判断是哪一种方法,是哪一个我们就调用哪一个方法
*/
String methodName = request.getParameter("method");
if(methodName == null || methodName.trim().isEmpty()) {
throw new RuntimeException("您没有传递method参数!无法确定您想要调用的方法!");
}
/*
* 得到方法名称,是否可通过反射来调用方法?
* 1、得到方法名,通过方法名再得到Method类的对象
* * 需要得到Class,然后调用它的方法进行查询!得到Method
* * 我们要查询的是当前类的方法,所以我们要得到当前类的Class
*/
Class c = this.getClass();//得到当前类的Class
Method method = null;
try {
method = c.getMethod(methodName,
HttpServletRequest.class,HttpServletResponse.class);
} catch (Exception e) {
throw new RuntimeException("您要调用的方法" + methodName + ",它不存在!");
}
try {
/*
* 调用method表示的方法
*/
//正常调用this.addUser(request,response), 反射调用method(this,request,response)
String result = (String) method.invoke(this, request,response);//反射调用
/*
* 获取请求处理方法执行后返回的字符,它表示转发或重定向的路径!
* 帮它完成转发或重定向!
*/
/*
* 如果用户返回的是字符串null或“”,那么我们什么也不做!
*/
if(result == null || result.trim().isEmpty()) {
return;
}
/*
* 查看返回的字符串中是否包含冒号,如果没有,表示转发
* 如果有,使用冒号分割字符串,得到前缀和后缀!
* 其中前缀如果是f,表示转发,如果是r表示重定向,后缀就是转发或重定向的路径了!
*/
if(result.contains(":")) {
//使用冒号分割字符串,得到前缀和后缀
int index = result.indexOf(":");//获取冒号位置
String s = result.substring(0, index);//截取出前缀,表示操作
String path = result.substring(index+1);//截取出后缀,表示路径
if(s.equalsIgnoreCase("r")) {//如果前缀是r,那么重定向
response.sendRedirect(request.getContextPath() + path);
} else if(s.equalsIgnoreCase("f")) {
request.getRequestDispatcher(path).forward(request, response);
} else {
throw new RuntimeException("你指定的操作:" + s + "当前的版本还不支持");
}
} else {//没有冒号,默认为转发
request.getRequestDispatcher(result).forward(request, response);;
}
} catch (Exception e) {
System.out.println("您调用的方法:" + methodName + ",它的内部抛出了异常!");
throw new RuntimeException(e);
}
}
}
public class PageServlet extends BaseServlet { private PageService pageService = new PageService(); /* * 指导教师为答辩阶段学生评分前的加载 */ public String essayAskGt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Teacher tea = (Teacher) req.getSession().getAttribute("session_tea"); try { req.setAttribute("stuList", pageService.findStuByTid(tea.getTid())); return "f:/jsps/teacher/gtessayask.jsp"; } catch (UserException e) { req.setAttribute("msg", e.getMessage()); return "f:/jsps/msg.jsp"; } } }
相关文章推荐
- 整理一下当年的学习笔记之:初步学习freemarker ,先做一个简单的HelloWord程序!
- 整理一下当年的学习笔记之:ajax请求json和普通项
- 整理一下当年的学习笔记之:struts2
- jmeter学习笔记(2)——上一个请求的结果传递给下一个请求使用
- 整理一下当年的学习笔记之:poi导入导出
- Servlet学习笔记_03_使用servlet编写一个简单的helloworld程序
- Jsp&Servelet 学习笔记-对一个Servlet创建多个Mapping
- 细雨学习笔记:Jmeter上一个请求的结果作为下一个请求的参数--使用正则提取器
- 多个请求使用同一个Servlet
- 整理一下当年的学习笔记之:JFreeChart初体验
- EmpServlet案例--使用重定向和路径匹配访问路径(一个Servlet处理多个请求)
- Silverlight学习笔记一(理解一下机制,使用一下布局,实现一个简单的用户登录)
- Jsp&Servelet 学习笔记-对一个Servlet创建多个Mapping
- 整理一下当年的学习笔记之:Jquery
- 整理一下当年的学习笔记之:SpringMVC的各种参数绑定方式
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- [原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析
- 学习笔记(JSJ-servlet获取请求头信息)
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- servlet学习笔记8——ServletContext的讲解和使用