BaseServlet encapsulated object is to simplify the servlet class code heavy.
private static final long serialVersionUID = 1L;
serialVersionUID role:
The equivalent of identity cards java class. Mainly used for version control.
serialVersionUID version of the role is to maintain serialization compatibility, the uniqueness that is deserialized object remains in the version upgrade.
When serialized version in order to maintain compatibility, that is, when version upgrade deserialization still remain unique object.
cn.mingliang.base.servlet Package; Import java.io.IOException; Import the java.lang.reflect.Method; Import javax.servlet.ServletException; Import the javax.servlet.http *;. public abstract class BaseServlet the extends the HttpServlet { Private static Long serialVersionUID = 1L Final; @Override public void-Service (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { Request.setCharacterEncoding ( "UTF-. 8"); the response.setContentType ( "text / htmm; charset = UTF-. 8") ; // get the user to pass parameters String method = request.getParameter ( "method" ); if the // user determines transmission parameters, if there is no transmission error parameter is IF (Method == null || method.trim () isEmpty ().) { System.out.println ( "hahah"); the throw a RuntimeException new new ( "Please pass parameters"); } // get reflected by the corresponding transmission parameters the method // if there is no method of transmission parameters corresponding to the user, the error method, methodd = null; the try { . methodd = this.getClass () getMethod (method, HttpServletRequest.class, HttpServletResponse.class); } the catch (a NoSuchMethodException E) { // TODO Auto-Generated Block the catch the throw new new RuntimeException ( "you do not create the method"); } // call user parameters corresponding to the transfer method, passing request, response internal method // if there is an error, an error // get the value returned by calling the method, if not no operation value, if the prefix is a default prefix f or no forward if (prefix.equalsIgnoreCase ( "r")) { // If it is then redirected r If the error for other // the try { String Result = (String) methodd.invoke (the this, Request, Response); (. Result.trim Result == null || () isEmpty ()) IF { return; } // indexOf is acquired brackets index symbol int index = result.indexOf ( ":"); // string taken string prefix result.substring = (0, index); string = result.substring path (index +. 1); iF (result.contains ( ":")) { // equalsIgnoreCase contains, ignore case Response.sendRedirect (request.getContextPath () + path); } the else iF (prefix.equalsIgnoreCase ( "F")) { request.getRequestDispatcher (path) .forward (Request, Response); } {the else the throw a RuntimeException new new ( "this version does not support this method"); } } the else { request.getRequestDispatcher (path) .forward (Request, Response); } } the catch (exception E) { e.printStackTrace (); System.out.println ( "inner throws an exception"); the throw a RuntimeException new new (E); } } }
Then create a servlet class that inherits BaseServlet, was written in the servlet class
Then requests path