您的位置:首页 > 其它

如何利用反射机制实现前台页面共用一个servlet文件

2016-04-29 19:15 766 查看

背景:当前台页面需要共用一个servlet的时候,这个时候可以在servlet的doGet方法中根据不同的页面调用servlet定义的不同方法

例如,前台页面中设置
<a href=“servlet?method=xaiojie”></a>

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodname=request.getParameter("method");
try {
Method method=getClass().getDeclaredMethod(methodname,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this, request,response);//调用各自的方法
} catch (Exception e) {
e.printStackTrace();
}
}
protected void xiaojie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<span style="white-space:pre">	</span>……
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: