今天我给大家带来的是通用servlet的写法
为什么要用通用servlet的写法哪,比如一个功能,增删该查各建一个类,但是假如入有N个哪,这样资源会被占用太多,所以这时候通用servlet的作用就出来了,大大的减少类的数量,解放资源。
@WebServlet("/common/*")
public class Common extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uri = request.getRequestURI();
String sb = uri.substring(uri.lastIndexOf("/") + 1);
try {
这里运用到一点反射知识
Method mthod = this.getClass().getDeclaredMethod(sb, HttpServletRequest.class, HttpServletResponse.class);
mthod.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
jsp部分要实现某个功能的代码如下,
这里我写了删除功能
href="common/delete?id=<%=book.getId()%>">删除</a>
这样的道理就可以把增删该查的功能都写上去了。
这里要特别说明,单词大小写等一定要更具自己写的准确写,不然会出错,博主我写的时候单词字母大小写错了我找了好久
(-_-),
谢谢欣赏。
2231

被折叠的 条评论
为什么被折叠?



