Java是否可以向函数发送httpServletRequest?
我得到了一个如下所示的函数:Java是否可以向函数发送httpServletRequest?,java,function,timer,Java,Function,Timer,我得到了一个如下所示的函数: @GET @Path("/execute/{scriptId}") public String execute(@Context HttpServletRequest req, @PathParam("scriptId") Long scriptId) { /* ... */ engine.eval(getSrc(req.getServletContext().getRealPath("js/boot.js"))); if (s
@GET
@Path("/execute/{scriptId}")
public String execute(@Context HttpServletRequest req, @PathParam("scriptId") Long scriptId) {
/* ... */
engine.eval(getSrc(req.getServletContext().getRealPath("js/boot.js")));
if (scriptId == 1L)
engine.eval(getSrc(req.getServletContext().getRealPath("js/test.js")));
else
engine.eval(getSrc(req.getServletContext().getRealPath("js/test2.js")));
/* that above, its the only place i need the req */
}
我从html页面调用它
<a href="rest/dss/execute/1">execute 1</a>
所以,基本上,我需要用定时器调用这个函数
有什么想法吗?当然,这只是一个可以实现的接口 当然,实现它来做一些有用的事情可能并不简单,这取决于您在另一个方法中对请求所做的操作
从实现JEE标准的某个第三方库中准备好HttpServletRequest的实现可能会有所帮助,但可能会有些过分。如果您的函数不需要
HttpServletRequest
(即它不需要调用HttpServletRequest
上的方法)然后,您可以将现有代码提取到不依赖于HttpServletRequest
的实现方法中,并在execute
方法中调用该实现:
@GET
@Path("/execute/{scriptId}")
public String execute(@Context HttpServletRequest req, @PathParam("scriptId") Long scriptId) {
return executeImpl(scriptId);
}
public String executeImpl(Long scriptId) {
...// your current implementation
}
然后计时器也可以调用该方法:
@Timeout
public void execute(Timer timer) {
Long scriptId = Long.parseLong(timer.getInfo().toString());
executeImpl(scriptId);
System.out.println("Timer Service : " + scriptId);
System.out.println("Current Time : " + new Date());
System.out.println("Next Timeout : " + timer.getNextTimeout());
System.out.println("Time Remaining : " + timer.getTimeRemaining());
System.out.println("____________________________________________");
}
怎么做?只需下载库并实现?然后呢?我仍然不知道如何调用函数,所以最后,即使我实现了一些其他接口,我仍然不知道如何做对于像这样简单的东西,实现
HttpServletRequest
不是一个好主意。我更喜欢@Thomas的建议。@bheshgurhung如果不需要的话,是的。但从最初的问题来看,无法确定这一点。
@Timeout
public void execute(Timer timer) {
Long scriptId = Long.parseLong(timer.getInfo().toString());
executeImpl(scriptId);
System.out.println("Timer Service : " + scriptId);
System.out.println("Current Time : " + new Date());
System.out.println("Next Timeout : " + timer.getNextTimeout());
System.out.println("Time Remaining : " + timer.getTimeRemaining());
System.out.println("____________________________________________");
}