我编写了一个存在业务逻辑的servlet。我将从调用我的RESTful Web服务方法的调用程序中获取一个令牌。有了这个令牌,我需要使用serlvet中编写的业务逻辑进行验证。我还映射了一个Filter,它在命中servlet之前设置了一些头值。
现在我想从RESTful Web服务方法中获取过滤器和servlet。能够以下面的方式获取ServletContext。
@Context private ServletContext servletContext;
但是如何获取HttpServletRequest,HttpServletResponse对象。因为,我需要在HttpServletRequest中设置令牌。
请提出解决方案。
提前致谢。
修改
我需要将令牌设置为Authorization Header。下面是代码。 的的web.xml
<filter>
<filter-name>HeaderFilter</filter-name>
<filter-class>com.controller.HeaderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HeaderFilter</filter-name>
<url-pattern>/dummy</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
休息网络服务类
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
@Path("/service")
public class Service {
@Path("/val/{token}")
@GET
@Produces("application/xml")
public String setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("token") String token) throws ServletException, IOException {
String value=token;
if(request==null){
System.out.println("Request null");
}
System.out.println("Token: " + value);
if(request!=null){
request.setAttribute("param", value);
request.getRequestDispatcher("/dummy").include(request, response);
}
return "<token>"+ "<value>"+value+" token value"+"</value>" + "</token>";
}
}
过滤类
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest=(HttpServletRequest)request;
System.out.println("********Filter**********");
HttpServletResponse httpResponse=(HttpServletResponse)response;
if(request.getAttribute("param")!=null){
String token=request.getAttribute("param").toString();
System.out.println("Filter httpRequest: " + token);
Wrapper requestWrapper = new Wrapper(httpRequest);
requestWrapper.addHeader("Authorization", token);
chain.doFilter(requestWrapper, response);
}
}
Servlet类
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext ctx=getServletContext();
System.out.println("Inside Dummy Servlet");
System.out.println(request.getAttribute("param").toString());
System.out.println("Request Header: "+response.getHeader("Authorization").toString());
}
现在可以通过在web.xml中添加以下代码来过滤。
<filter-mapping>
<filter-name>HeaderFilter</filter-name>
<url-pattern>/dummy</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
但是在Author for Author中设置的值在servlet中调用时为null。我需要在HeaderFilter
中的授权标题中设置令牌。我坚持这样做。
答案 0 :(得分:2)
您使用的是jax-rs,需要使用@Context注释创建一个控制器:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
@Path("helloword")
public class SampleControler {
@GET
public String doGetAsHtml(@Context HttpServletRequest req, @Context HttpServletResponse response) {
req.setAttribute("my attr", "my object");
.....
return ...;
}
}
这应该有效。