我在我的应用程序中使用jsp文件,它包含jspInit()方法来初始化一些资源。我尝试在jsp文件的init方法中获取协议( http/https /ftp),以启用具有http/https连接的应用程序url。
代码:
<%!public void jspInit() {
String realPath = getServletContext().getRealPath("/");
ServletContext servletContext = getServletConfig().getServletContext();
// how to get ServletRequest like servletcontext, So that we can get protocol from servletRequest.
}
%> 我见过ServletRequest接口有'getScheme()‘方法,但我不知道如何在jspInit()中获取ServletRequest。
谁能告诉我如何在jsp中获取模式?
发布于 2017-09-22 22:26:33
简短的答案是:没有办法在jsp的初始化时(即在jspInit方法中)获得uri模式。
您无法在jspInit方法中获取请求的架构,因为没有请求。实际上,方法ServletRequest.getSchema()从客户机用来向jsp页面/servlet发出请求的url中提取方案部分。jspInit方法在初始化jsp时由容器调用,这在servlet生命周期中只发生一次。因此,当调用jspInit方法时,没有实际的请求来获取方案。
如果您需要“通过http/https连接启用应用程序url”,则jspInit不是正确的位置。例如,如果您正在使用apache tomcat,您可以阅读本教程(configure-tomcat-to-support-ssl-or-https)或this ssl-howto,了解您需要采取的步骤。
发布于 2017-09-23 06:30:26
从jspInit()访问请求是没有意义的。因为它只在JSP编译时被调用。
您应该正在阅读请求中的方案。下面是用于从JSP中的请求中获取方案的EL。
<c:if test="${pageContext.request.scheme eq 'https'}">
//this section will be exectured only if it is Https request.
</c:if>https://stackoverflow.com/questions/46365325
复制相似问题