Home > Back-end >  The use of the forward error BaseServlet
The use of the forward error BaseServlet

Time:10-30

I use a BaseServle, BaseServle UserServlet inheritance, in UserServlet approach, using the forward requests, jump page without success, called the NoSuchMethodException, there is no way that abnormal
At the same time of obtaining the method name I print the method name, I found that I forwarded the HTML page login. HTML is printed out, that is to say he is to obtain the login. HTML page this method, this method must not ah, this kind of problem will happen using redirects, this is what reason is caused?

CodePudding user response:

The following is a BaseServlet code,

CodePudding user response:

Public class BaseServlet extends the HttpServlet {

@ Override
Protected void service (it request, HttpServletResponse response) throws ServletException, IOException {
/* the Service method analysis
* as a first step, to obtain the request URI interception at last the method called
* method according to the method name for objects,
* */execution method object

//get request URI
String URI=request. GetRequestURI ();//such as/user/method name

//intercept method to obtain the final/index position, plus one after the first is the method name
String methodName=URI. The substring (URI) lastIndexOf ('/') + 1);
System. The out. Println (methodName);

//methods according to the method of acquiring object note: this represents the subclass object here, because the request is UserService, he is a subclass object
//note that subclass object method name shall become public, otherwise unable to get object, violence can only be reflected

Try {
Method Method=this. GetClass (). GetMethod (methodName, it. Class, HttpServletResponse class);
//execution method object
method.invoke(this,request,response);
} the catch (NoSuchMethodException e) {
e.printStackTrace();
} the catch (IllegalAccessException e) {
e.printStackTrace();
} the catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

CodePudding user response:

This is method of userServlet
Public void exit (it request, HttpServletResponse response) throws ServletException, IOException {
Request. GetRequestDispatcher (" login. HTML "). The forward (request, response);
}
  • Related