思路 原来是写一个Servlet然后让这个Servlet继承HttpServlet,然后HttpServlet里面的Service方法自动的帮我们进行请求方式方法的分发,也就是分发doGet和doPost方法,然后我们也是这个思路。我们将原来写的Servlet写成一个个方法,也就是对应原来HttpServlet的doGet和doPost方法,但是此时需要一个Servlet来替代HttpServlet,来帮我们进行方法分发,那么这个替代HttpServlet的Servlet需要我们自己去编写,一般命名为BaseServlet,这个BaseServlet也要继承HttpServlet,重写Service方法帮我们进行方法的分发。那这个BaseServlet怎么去分发方法呢?首先,我们将我们的方法放在一个Servlet里面,我们可以很容易获取到这个Servlet的字节码文件,也就类似于获取了这个类,而通过反射机制可以执行一个类里面的方法,步骤是先通过getMethod(参数,HttpServletRequest.class,HttpServletResponse.class)(注:参数是获取目的方法的名称),然后通过invoke(this,requset,response)方法就可以执行获取到的方法。按照这个思路,我们已经拿到Servlet的字节码文件,现在需要方法名称,有了方法名称我们就可以调用相应的方法了。怎么获取方法名称呢?就是通过前端请求的URL地址来获取,然后截取最后的方法名,那么此时什么都有了,就可以执行相应的方法了。 注意:BaseServlet不需要注解@WebServlet,因为他继承了HttpServlet重写了Service方法 错误:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2…Could not star 解决:如果发现Servlet优化前Tomcat启动好好的,写了相关Servlet优化代码后发现TomCat启动错误,那么很有可能是Servlet路径错误,先把需要优化的Servlet的注解@WebServlet都注释掉就可以了。 BaseServlet
package web ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. lang. reflect. InvocationTargetException ;
import java. lang. reflect. Method ;
public class BaseServlet extends HttpServlet {
@Override
protected void service ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
String uri = request. getRequestURI ( ) ;
int index = uri. lastIndexOf ( "/" ) ;
String methodName = uri. substring ( index+ 1 ) ;
Class < ? extends BaseServlet > cls = this . getClass ( ) ;
try {
Method method = cls. getMethod ( methodName, HttpServletRequest . class , HttpServletResponse . class ) ;
method. invoke ( this , request, response) ;
} catch ( NoSuchMethodException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
}
}
}
BrandServlet
package web ;
import com. alibaba. fastjson. JSON ;
import pojo. Brand ;
import service. BrandService ;
import javax. servlet. * ;
import javax. servlet. http. * ;
import javax. servlet. annotation. * ;
import java. io. BufferedReader ;
import java. io. IOException ;
import java. util. List ;
@WebServlet ( "/brand/*" )
public class BrandServlet extends BaseServlet {
private BrandService brandService = new BrandService ( ) ;
public void selectAll ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
List < Brand > brands = brandService. selectAll ( ) ;
String jsonString = JSON . toJSONString ( brands) ;
response. setContentType ( "text/json;charset=utf-8" ) ;
response. getWriter ( ) . write ( jsonString) ;
}
public void addBrand ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
BufferedReader reader = request. getReader ( ) ;
String json = reader. readLine ( ) ;
Brand brand = JSON . parseObject ( json, Brand . class ) ;
brandService. addBrand ( brand) ;
response. getWriter ( ) . write ( "true" ) ;
}
public void dlt ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
BufferedReader reader = request. getReader ( ) ;
String json = reader. readLine ( ) ;
brandService. deleteById ( Integer . parseInt ( json) ) ;
response. getWriter ( ) . write ( "true" ) ;
}
public void modify ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
BufferedReader reader = request. getReader ( ) ;
String json = reader. readLine ( ) ;
Brand brand = JSON . parseObject ( json, Brand . class ) ;
brandService. modify ( brand) ;
response. getWriter ( ) . write ( "true" ) ;
}
public void selectById ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
BufferedReader reader = request. getReader ( ) ;
String json = reader. readLine ( ) ;
Brand brand = brandService. selectById ( Integer . parseInt ( json) ) ;
String jsonString = JSON . toJSONString ( brand) ;
response. setContentType ( "text/json;charset=utf-8" ) ;
response. getWriter ( ) . write ( jsonString) ;
}
}