TL;DR:
我怎么才能在ServletRequestListener.requestDestroyed期间拿到ServletResponse美元?
Short Version
在Java EE中,我想知道什么时候:
- 当请求启动时
- 当请求结束时
Long Version
在ASP.net世界中,如果您想知道请求何时开始和结束,您可以编写一个100:
public class ExampleModuleForThisQuestion : IHttpModule
{
}
然后在Web XML配置文件中注册您的"module":
web.config:
<system.webServer>
<modules>
<add name="DoesntMatter" type="ExampleModuleForThisQuestion "/>
</modules>
</system.webServer>
在您的模块中,您可以注册以下回调处理程序:
- BeginRequest个活动
- EndRequest个活动
然后,Web服务器基础设施调用您的100方法.这是您注册希望在请求启动和请求结束时接收通知的机会:
public class ExampleModuleForThisQuestion : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(beginRequest); //register the "BeginRequet" event
application.EndRequest += new EventHandler(endRequest); //register the "EndRequest" event
}
}
现在我们有了回调,当request starts分:
private void beginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
//Record the time the request started
application.Context.Items["RequestStartTime"] = DateTime.Now;
//We can even access the Request and Response objects
application.ContenxtLog(application.Context.Request.Headers["User-Agent"]);
}
当request ends分的时候,我们有回拨:
private void endRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
//We can even access the Request and Response objects
//Get the response status code (e.g. 418 I'm a teapot)
int statusCode = application.Context.Response.StatusCode;
//Get the request method (e.g. GET, POST, BREW)
String method = application.context.Request.RequestType;
//Get the path from the request (e.g. /ViewCustomer)
String path = application.context.Request.AppRelativeCurrentExecutionFilePath'
//Get when the request started - that we recorded during "Begin Request"
DateTime requestStartTime = (DateTime)application.Context.Items["RequestStartTime"];
//And we can modify the response
if ((DateTime.Now - requestStartTime).TotalSeconds = 17)
application.Context.Response.StatusCode = 451;
}
Java Almost-Equivalent is ServletRequestListener
在Java apparently中,相应的技术是创建实现101接口的对象:
@WebListener
public class ExampleListenerForThisQuestion
implements javax.servlet.ServletRequestListener {
}
并将我们的listener注册到应用服务器,方法是将其包含在我们的Web XML配置文件中:
web.xml个
<listener>
<listener-class>ExampleListenerForThisQuestion</listener-class>
</listener>
现在,我们可以实现在请求开始和结束时获取的requestInitialized和requestDestroyed方法:
public class ExampleListenerForThisQuestion
implements javax.servlet.ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
ServletRequest sr = sre.getServletRequest();
sr.setAttribute("requestStartTicks", getCurrentTickCount());
HttpServletRequest request = (HttpServletRequest) sr;
// e.g. "PUT /Customers/1234"
System.out.printf("%s %s\r\n", request.getMethod());
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
ServletRequest sr = sre.getServletRequest();
long requestStartTicks = (long)sr.getAttribute("requestStartTicks");
HttpServletResponse response = (HttpServletRequest)...nothing, because i don't know how...
// e.g. "226 IM Used"
System.out.printf("%d %s\r\n", response.getStatus(), response.getStatusDescription());
}
}
But how to get the response?
现在我收到了响应结束时的通知,我需要该请求的结果:
- 我需要http状态代码(例如
424
) - 我需要http状态描述(例如
Failed Dependency
) - 我需要判断响应头
- 我需要修改响应头
您注意到我上面代码中的这一行:
HttpServletResponse response = (HttpServletRequest)...nothing, because i don't know how...
我不知道如何获得response;这就是为什么我问Stackoverflow.