2011-02-06 47 views
1

我已经部署在某个服务器上的某些网页说:包含一些页面,返回401

http://myhost/some_secured_file.html

,当我访问返回401问我授权自己在浏览器中该文件。

问题是,我试图使用c:import标签将此页面包含在某个JSP页面内。

应用服务器返回:

javax.servlet.jsp.JspException: Problem accessing the absolute URL "http://myhost/some_secured_file.html". java 
    .io.IOException: Server returned HTTP response code: 401 for URL: http://myhost/some_secured_file.html 

我如何可以完成包括!?

+0

您是否拥有所需的凭据? – 2011-02-06 11:54:01

回答

3

考虑通过另一个jsp页面或servlet代理请求。然后,让代理执行验证请求,例如使用Apache HTTPClient,并将该响应的内容写入页面。然后你可以简单地在你的jsp页面上导入你的代理的URL。

好吧,考虑下面的伪代码为澄清:

class Proxy extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     // Perform a new request to get contents from secured page 
     HttpClient client = new HttpClient(); 
     Credentials credentials = new UsernamePasswordCredentials("user", "pass"); 
     client.getState().setCredentials(authScope, credentials); 
     GetMethod method = new GetMethod("/secure_page.jsp"); 
     client.executeMethod(client.getHostConfiguration();, method); 

     // write result to the outputstream 
     resp.getWriter().write(method.getResponseBodyAsString()); 
    } 
} 

什么这个servlet的作用是获取安全的页面为您服务。你需要把这个servlet挂在你的web描述符中。这对于将例如/proxy.jsp请求映射到它是必要的。然后你可以在你的jsp页面中做什么就像<c:import value="proxy.jsp"/>