创建一个BaseServlet来利用反射自动根据前台传来的method自动分发方法
作者:互联网
创建模板Serclet,让实例都继承这个模板,来实现自动解析"method",并且再利用反射来调用方法.从而实现自动分发方法.
@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");//解决tomcat的post请求中文乱码问题
resp.setContentType("text/html;charset=utf-8");//解决响应乱码问题
//获取方法名
String methodName = req.getParameter("method");
//使用"反射"解决重复判断的问题
try
{
Class clazz = this.getClass();
Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.setAccessible(true);
method.invoke(this, req,resp);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
子类继承BaseServlet.
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet
{
//用BeanFactory来反射创造service对象
UserServiceImpl userservice = BeanFactory.createBean("userservice", UserServiceImpl.class);
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//告诉Tomcat用utf-8来解码
req.setCharacterEncoding("utf-8");
//告诉Tomcat对于text/html 用utf-8来编码,并且告诉浏览器用utf-8来解码。
resp.setContentType("text/html; charset = utf-8");
//登录方法
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = userservice.login(username, password);
//将user放入session域中.做到只登陆一次的功能.
req.getSession().setAttribute("user",user);
//将user变为json串,返回到登录页面中
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
resp.getWriter().write(json);
}
}
子类继承BaseServlet
@WebServlet("/StudentServlet")
public class StudentServlet extends BaseServlet
{
//创建studentService实现类
StudentServiceImpl studentservice = BeanFactory.createBean("studentservice", StudentServiceImpl.class);
//分页查询
protected void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//告诉Tomcat用utf-8来解码
req.setCharacterEncoding("utf-8");
//告诉Tomcat对于text/html 用utf-8来编码,并且告诉浏览器用utf-8来解码。
resp.setContentType("text/html; charset = utf-8");
//获取当前页和总页数
String currPage = req.getParameter("currPage");
String pageSize = req.getParameter("pageSize");
//调用service层的方法
PageInfo<Student> studentList = studentservice.selectByPage(currPage, pageSize);
//将返回值包装成json串 并且返回
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(studentList);
resp.getWriter().write(json);
}
//添加学生
protected void addStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//获取学生信息
Map<String, String[]> map = req.getParameterMap();
Student addstudent = new Student();
//封装之前注册日期转换
ConvertUtils.register(new Converter()
{
public Object convert(Class type, Object value)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try
{
return simpleDateFormat.parse(value.toString());
}
catch (ParseException e)
{
e.printStackTrace();
}
return null;
}
}, Date.class);
try
{
BeanUtils.populate(addstudent, map);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(studentservice.addStudent(addstudent));
resp.getWriter().write(json);
}
//修改学生
protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//获取学生信息
Map<String, String[]> map = req.getParameterMap();
Student updatestudent = new Student();
//封装之前注册日期转换
ConvertUtils.register(new Converter()
{
public Object convert(Class type, Object value)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try
{
return simpleDateFormat.parse(value.toString());
}
catch (ParseException e)
{
e.printStackTrace();
}
return null;
}
}, Date.class);
try
{
BeanUtils.populate(updatestudent, map);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
ObjectMapper objectMapper = new ObjectMapper();
Integer res = studentservice.updateStudent(updatestudent);
String json = objectMapper.writeValueAsString(res);
resp.getWriter().write(json);
}
//删除学生的功能
protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//获取要删除学生的id
String number = req.getParameter("number");
Integer res = studentservice.deleteStudent(number);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(res);
resp.getWriter().write(json);
}
}
标签:BaseServlet,utf,String,resp,req,json,自动,new,method 来源: https://blog.csdn.net/qq_46190347/article/details/110940389