1.此处我写的是一个总的Servlet=>BaseServlet,紧接着其余Servlet可以继承BaseServlet。
BaseServlet.java
package cn.railway.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 该类用到了反射机制
*
* JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,
* 对于任意一个对象,都能够调用他的任意一个方法;这种动态获取的信息以及动态调用对象
* 的方法的功能成为java语言的反射机制。
*/
@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取jsp页面中method对应的servlet
String methodName = request.getParameter("method");
Class<? extends BaseServlet> clazz = this.getClass();
try {
//通过methodName和参数的.class(方法中的参数类型)得到要执行的method
Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
/*
* Method Class.getMethod(String name, Class<?>... parameterTypes)的作用是
* 获得对象所声明的公开方法
该方法的第一个参数name是要获得方法的名字,
第二个参数parameterTypes是按声明顺序标识该方法形参类型。
* */
/**
* 执行该Method.invoke方法的参数是执行这个方法的对象this,和参数类型request,response。
* 可以这么理解,this对象中带有参数request,response的method方法。
*/
method.invoke(this, request,response);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
创建一个Servlet类
PassengerServlet.java
package cn.railway.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import cn.railway.entity.Passenger;
import cn.railway.entity.User;
import cn.railway.service.PassengerService;
import cn.railway.service.impl.PassengerServiceImpl;
import cn.railway.utils.PageUtils;
@WebServlet("/user/PassengerServlet")
public class PassengerServlet extends BaseServlet {
PassengerService ps = null;
public PassengerServlet() {
ps = new PassengerServiceImpl();
}
public void ajaxRetrun(HttpServletResponse response, int i, String msg) {
PrintWriter out = null;
try {
out = response.getWriter();
if (i > 0) {
out.write(msg);
} else {
out.write("操作失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 旅客用户页面初始化和展示页面 其中 包括用户自己
*
* @param request
* @param response
*/
public void showAllPassengersByPage(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
int uid = 1;
// 获取当前页
String indexStr = request.getParameter("index");
String sizeStr = request.getParameter("size");
int index = 1;
int size = 4;
if (indexStr != null) {
index = Integer.parseInt(indexStr);
}
if (sizeStr != null) {
size = Integer.parseInt(sizeStr);
}
User user = ps.getUserByUid(uid);
ps.getPage(uid, index, size);
List<Passenger> pList = ps.getAllPassengers(uid, index, size);
request.setAttribute("user", user);
request.setAttribute("list", pList);
request.setAttribute("index", PageUtils.index);
request.setAttribute("totalPage", PageUtils.totalPage);
try {
request.getRequestDispatcher("/pages/user/passengers.jsp").forward(request, response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 删除全部乘客信息的请求 和删除部分代码重复这里弃用
*
* @param request
* @param response
*/
public void delAllPassengers(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
int uid = 1;
int i = ps.delAllPassengers(uid);
String msg = null;
if (i > 0) {
msg = "delsucc";
} else {
msg = "delfail";
}
PrintWriter out = null;
try {
out = response.getWriter();
out.write(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除多个乘客信息的请求 这里和删除全部右重合
*
* @param request
* @param response
*/
public void delPassengers(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
// int uid=2;
String pid = request.getParameter("pid");
String[] pidStr = pid.substring(0, pid.length() - 1).split(",");
// 将字符串数组变为int数组
Integer[] pids = new Integer[pidStr.length];
for (int i = 0; i < pidStr.length; i++) {
pids[i] = Integer.valueOf(pidStr[i]);
}
int i = ps.delPassengers(pids);
// System.out.println("删除了"+i+"条");
String msg = "删除成功";
this.ajaxRetrun(response, i, msg);
}
/**
* 删除特定的联系人
*
* @param request
* @param response
*/
public void delOnePassenger(HttpServletRequest request, HttpServletResponse response) {
String pidStr = request.getParameter("pid");
Integer pid = Integer.valueOf(pidStr);
int i = ps.delOnePassenger(pid);
String msg = "删除成功";
this.ajaxRetrun(response, i, msg);
}
public void changeToUpdate(HttpServletRequest request, HttpServletResponse response) {
String pidStr = request.getParameter("pid");
Integer pid = Integer.valueOf(pidStr);
// System.out.println(pid);
Passenger passenger = ps.getPassengerByPid(pid);
request.setAttribute("passenger", passenger);
try {
request.getRequestDispatcher("/pages/user/passengerUpdate.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 跟新旅客信息的请求
*
* @param request
* @param response
*/
public void updatePassenger(HttpServletRequest request, HttpServletResponse response) {
Integer pid = Integer.valueOf(request.getParameter("pid"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("tel", request.getParameter("tel"));
map.put("visitorType", request.getParameter("visitorType"));
int i = ps.updatePassenger(pid, map);
System.out.println(i);
String msg = "修改成功";
this.ajaxRetrun(response, i, msg);
}
/**
* 添加一非新的联系人请求
*
* @param request
* @param response
*/
public void createPassenger(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
int uid = 1;
String pname = request.getParameter("pname");
String cardType = request.getParameter("cardType");
String cardNum = request.getParameter("pname");
String tel = request.getParameter("tel");
String visitorType = request.getParameter("visitorType");
Passenger passenger = new Passenger(null, uid, pname, cardType, cardNum, tel, visitorType, null);
int i = ps.createPassenger(passenger);
String msg = "添加成功";
this.ajaxRetrun(response, i, msg);
}
/**
* 动态的查询联系人信息请求
*
* @param request
* @param response
*/
public void searchPassengers(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
int uid = 1;
// 获取当前页
String indexStr = request.getParameter("index");
String sizeStr = request.getParameter("size");
int index = 1;
int size = 4;
if (indexStr != null) {
index = Integer.parseInt(indexStr);
}
if (sizeStr != null) {
size = Integer.parseInt(sizeStr);
}
String name = request.getParameter("pname");
List<Passenger> list = ps.getPassengersByName(uid, name, index, size);
String js = JSON.toJSONString(list);
PrintWriter out = null;
try {
out = response.getWriter();
out.write(js);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取所有乘客的姓名
*
* @param request
* @param response
*/
public void showAllPassNames(HttpServletRequest request, HttpServletResponse response) {
// 根据session获取uid
int uid = 1;
List<String> list = ps.getAllPassNames(uid);
String showNames = JSON.toJSONString(list);
System.out.println(showNames);
}
}
此处我只想访问该Servlet下的showAllPassNames时,填写的网址是:
localhost:8080/RialwayTicketSystem/user/PassengerServlet?method=showAllPassNames
此时,在后台控制台便可以看到打印出来的情况