我想从一个servlet获取我的Web应用程序的根URL.
如果我在"www.mydomain.com"中部署我的应用程序,我想获得像" http://www.mydomain.com " 这样的根URL .
如果我将它部署在具有8080端口的本地tomcat服务器中它应该给予同样的事情 http://localhost:8080/myapp
谁能告诉我如何从servlet获取我的Web应用程序的根URL?
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rootURL="";
//Code to get the URL where this servlet is deployed
}
}
Run Code Online (Sandbox Code Playgroud) 我希望能够从Service spring bean 动态检索我的spring Web应用程序的" servlet上下文路径 "(例如http://localhost/myapp或http://www.mysite.com).
这样做的原因是我想在将要发送给网站用户的电子邮件中使用此值.
虽然从Spring MVC控制器执行此操作非常容易,但从Service bean执行此操作并不是那么明显.
任何人都可以建议吗?
编辑:附加要求:
我想知道是否有一种方法可以在启动应用程序时检索上下文路径,并且可以通过我的所有服务随时检索它?
在Spring MVC中获取Web应用程序的根/基本URL的最佳方法是什么?
Base Url = http://www.example.com或http://www.example.com/VirtualDirectory
我想从Controller中找到Spring中webapp的绝对URL.我知道JSTL c:url,但我需要来自Controller内部的这些信息.
@Controller
public class AuthorizeController {
@Autowired
private Authorizer auth;
@RequestMapping("/auth")
public String sendToAuthorization() {
String baseUrl = "http://localhost:8080/tasks/";
return "redirect:" + auth.getAuthorizationUrl(baseUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,baseUrl是硬编码的,我可以通过Spring配置将它提供给Authorizer类,但我确信可以在Controller中从Spring获取此信息.我试图google"spring mvc url"并找不到解决这个问题的方法.
我正在使用Spring Web Services将服务公开为Web服务.在我的spring配置xml文件中,我有一个bean,它是DefaultWsdl11Definition的一个实例.需要设置的属性之一是locationUri.这需要是一个完全合格的Uri,但是当应用程序从dev升级到uat和生产时,我不想更改此值.Spring知道Web应用程序上下文根是什么,所以我可以在配置文件中指定一个变量来访问它吗?
就像是:
<bean id="myWebServices"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schemaCollection">
<bean
class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="xsds" ref="xsdList"/>
<property name="inline" value="true" />
</bean>
</property>
<property name="portTypeName" value="myWebServices" />
<property name="locationUri" value="${webContextRoot}/webServices" />
</bean>
Run Code Online (Sandbox Code Playgroud) 我正在使用版本2.4,Servlet我需要ContextPath通过init()在服务器启动时调用的方法,所以我没有任何可以调用的Request对象,getContextPath()因为Servlet版本我没有getContextPath()方法ServletContext.
有办法以ContextPath()某种方式得到这个吗?
我正在创建一个用于处理邮件发送的类.对于其中一些邮件,我需要构建链接(帐户激活,重置密码,确认等).
我不想硬编码链接的主机,因为dev,test和prod的主机不同.所以我需要在我的邮件类中以编程方式获取主机名和应用程序上下文.
我们在这个项目中使用Spring MVC.
我用一个例子说明我想要的东西.
在我们的JSP中,我们有这样的代码片段:
<script src="<%=request.getContextPath()%>/js/jquery-1.7.js" type="text/javascript">
Run Code Online (Sandbox Code Playgroud)
在此上下文中,request.getContextPath()将解析为
http:// localhost:8080/applicationName
这就是我想要的邮件课程.
我试图在我的班级中实现ServletContextAware,以为我能够使用servletContext.getContextPath()类似的结果.但是这个语句只解析为/ applicationName
所以我的问题是:如何在我的邮件类中获得完整的主机和上下文(http:// localhost:8080/applicationName)?我可以使用除ServletContextAware之外的其他接口吗?请帮忙 :-)
关心丹尼尔
我使用Icefaces和JSF,我遇到了这个问题:我有以下网址:
http://myMappedServer/myApp/followingThings
我想在我的xHtml页面中获取值http://myMappedServer/myApp
如何在不使用托管bean的情况下实现此目的?
java ×5
contextpath ×4
servlets ×4
spring-mvc ×3
spring ×2
base-url ×1
java-ee ×1
jsf ×1
spring-ws ×1
url ×1