javaweb调用后台总是需要配置web.xml,看着配置文件越来越长怎么办?看这里!

在一些老的系统中,需要新增后台servlet,但又不想动配置,害怕配置越来越多的时候,可以考虑使用以下技巧。

1:创建一个action接口类

public interface AbstractAction {
public abstract void execute() throws IOException, ServletException;
}

2:创建一个servlet类

public class ServiceAction extends HttpServlet {
private static final String strContentType = "text/html; charset=UTF-8";
protected final Log logger = LogFactory.getLog(getClass());
public ServiceAction() {}
public void destroy() {}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType(strContentType);
// response.setCharacterEncoding("UTF-8");
String strRequestURI = request.getRequestURI();// 取得完整的请求URL


// *******取出请求URL中所包含的相关 Action 类的信息,并存入 strRequestType
// 变量中(start)*******//
int index = strRequestURI.lastIndexOf("/");
int index2 = strRequestURI.indexOf("?");
String strRequestType;
if (index2 == -1){
strRequestType = strRequestURI.substring(index + 1);
}else{
strRequestType = strRequestURI.substring(index2 - 1);
                }
AbstractAction objAbstractAction = null;// 所有 Action 类需实现的接口的声明
if (strRequestType == null || strRequestType.equalsIgnoreCase("")) return;
// *******取出请求URL中所包含的相关 Action 类的信息,并存入 strRequestType
// 变量中(end)*******//
try {
// *******根据 strRequestType 变量中包含的信息创建相应的 Action 类(start)*******//
Class clazz = Class.forName(strRequestType);
Constructor ctor = clazz.getConstructor(new Class[] {
HttpServletRequest.class, HttpServletResponse.class });
objAbstractAction = (AbstractAction) ctor.newInstance(new Object[] {
request, response });
// *******根据 strRequestType 变量中包含的信息创建相应的 Action 类(end)*******//
} catch (Exception ex) {
logger.error(
new StringBuffer("Could not Instance ")
.append(strRequestType).append(" Class").toString(),
ex);
}
if (objAbstractAction != null)
objAbstractAction.execute();// 执行相应的 Action 类中的方法
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {}
}

3.配置web.xml 

        <servlet>
<servlet-name>ServiceAction</servlet-name>
<servlet-class>xxx.xxxx.xxxxx.ServiceAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServiceAction</servlet-name>
<url-pattern>/ServiceAction/*</url-pattern>

</servlet-mapping>

注:xxx.xxxx.xxxxx.ServiceAction 请改为实际ServiceAction所在路径

4.完成啦!后续新增servlet的话,直接写一个action类实现AbstractAction就可以啦

示例:TestAction

public class TestAction implements AbstractAction{
private HttpServletRequest request;
private HttpServletResponse response;
public TestAction(HttpServletRequest request,HttpServletResponse response){
this.request = request;
this.response = response;
}
@Override
public void execute() throws IOException, ServletException {
response.getWriter().write("测试action");
response.getWriter().close();
}

}

访问后台TestAction 的路径:/ServiceAction/xxx.xxxx.xxxxx.TestAction?action=xxx

这种方法仅适合针对于一些老的系统,不方便使用spring注解等手段的,也算是减少配置增加的一种方式了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心怀寰宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值