//子servlet
@WebServlet("/user")
public class UserServlet extends CoreServlet {
public void test(HttpServletRequest req,HttpServletResponse resp)throws IOException{
req.setCharacterEncoding("utf-8");
System.out.println(req);
System.out.println(req.getParameter("test"));
}
}
//父servlet
public class CoreServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// req.setCharacterEncoding("utf-8");
try {
System.out.println(req);
String ac = req.getParameter("ac");
//调用其他方法(反射)
Method addMethod = this.getClass().getDeclaredMethod(ac, HttpServletRequest.class, HttpServletResponse.class);
//执行
addMethod.invoke(this, req, resp);//invoke(方法拥有类的对象,参数)
} catch (Exception e) {
e.printStackTrace();
}
}
}
请求是post,在子servlet设置req.setCharacterEncoding没有作用,在父servlet设置就能生效