I have two servlets (S1 and S2). S1 renders a HTML-Page which acces S2 via an URL (img src="URL"). I know the servlet name of S2, but not the URL. The URL is configured in the web.xml of course, but how can I access that from S1?
我有两个servlet(S1和S2)。 S1呈现一个HTML页面,它通过URL(img src =“URL”)访问S2。我知道S2的servlet名称,但不知道URL。当然,URL在web.xml中配置,但是如何从S1访问?
4 个解决方案
#1
1
Use:
HttpServletResponse.encodeURL(String)
Which in your case should be something like this:
在你的情况下应该是这样的:
response.encodeURL("/S2");
This method will take care of any URL re-writing that needs to take place to maintain session state and I think will prepend the necessary path info to the URL.
此方法将处理需要进行的任何URL重写以维护会话状态,并且我认为将在URL之前添加必要的路径信息。
I use JSTL these days so this I'm a little rusty on that last point but if the path info isn't prepended you can get it from the request and add it yourself.
这些天我使用JSTL,所以我在最后一点上有点生疏,但是如果没有前面的路径信息,你可以从请求中获取它并自己添加它。
#2
1
I would guess, that most implementations of the ServletConfig hold that mapping informations (org.apache.catalina.core.StandardWrapper does), but since the ServletConfig-Interface don't provides a getter, you'll have to do some tricks to get it and would bind your application to a specific implementation or application server.
我猜,ServletConfig的大多数实现都持有映射信息(org.apache.catalina.core.StandardWrapper),但由于ServletConfig-Interface不提供getter,你必须做一些技巧才能获得它会将您的应用程序绑定到特定的实现或应用程序服务器。
Maybe you just read it from the web.xml. Just select all "servlet-mapping" Elements with the given "servlet-name" and read the "url-pattern". Since this is in the spec, that should work on ever app server out there.
也许你只是从web.xml中读取它。只需选择具有给定“servlet-name”的所有“servlet-mapping”元素,并阅读“url-pattern”。由于这是在规范中,这应该适用于任何app服务器。
EDIT:
Here is the dirty example. Getting the URL mappings using refelction:
这是一个肮脏的例子。使用refelction获取URL映射:
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class<?> clazz = config.getClass();
Field configField = clazz.getDeclaredField("config");
configField.setAccessible(true);
StandardWrapper standardWrapper = (StandardWrapper) configField.get(config);
clazz = standardWrapper.getClass();
Field mappingsField = clazz.getDeclaredField("mappings");
mappingsField.setAccessible(true);
List<?> mappings = (List<?>) mappingsField.get(standardWrapper);
System.out.println(mappings);
}catch (Exception e) {
logger.error("", e);
}
}
That works in my JSF, Tomcat environment. The config Object is "org.apache.catalina.core.StandardWrapperFacade" and has a field called "config" which hold a "org.apache.catalina.core.StandardWrapper", which has a field called "mappings".
这适用于我的JSF,Tomcat环境。 config对象是“org.apache.catalina.core.StandardWrapperFacade”并且有一个名为“config”的字段,它包含一个“org.apache.catalina.core.StandardWrapper”,它有一个名为“mappings”的字段。
But as I said, this is a dirty hack!
但正如我所说,这是一个肮脏的黑客!
#3
1
Perhaps you should supply the URL of the second servlet to the first as a servlet parameter. I realise this means encoding the URL twice in the web.xml (which I really abhor), but to avoid problems you can always build the web.xml as part of your build, and populate from a properties file.
也许您应该将第二个servlet的URL作为servlet参数提供给第一个servlet。我意识到这意味着在web.xml中对URL进行两次编码(我真的很厌恶),但为了避免出现问题,您可以始终将web.xml构建为构建的一部分,并从属性文件中填充。
A little bit nasty and fiddly, I appreciate, but in the absence of any cross-container API solution, perhaps it's the most pragmatic solution.
我很欣赏,但是在没有任何跨容器API解决方案的情况下,这可能是最实用的解决方案。
#4
0
You can do it easily in your doGet() or doPost() Method following is the code to be written in doGet() or doPost() metod of servlet to get the mapping associated with a servlet.
您可以在doGet()或doPost()方法中轻松完成。以下是要在servlet的doGet()或doPost()方法中编写的代码,以获取与servlet关联的映射。
public void doGet(HttpServletRequest request,HttpServletResponse response){
String nameOfServlet = "S2";
ServletContext context = getServletContext();
String[] mappinggs=context.getServletRegistration(nameOfServlet).getMappings().toArray();
//mappings will contain all the mappings associated with servlet "S2"
//print take the first one if there is only one mapping
System.out.println(mappings[0]);
}
#1
1
Use:
HttpServletResponse.encodeURL(String)
Which in your case should be something like this:
在你的情况下应该是这样的:
response.encodeURL("/S2");
This method will take care of any URL re-writing that needs to take place to maintain session state and I think will prepend the necessary path info to the URL.
此方法将处理需要进行的任何URL重写以维护会话状态,并且我认为将在URL之前添加必要的路径信息。
I use JSTL these days so this I'm a little rusty on that last point but if the path info isn't prepended you can get it from the request and add it yourself.
这些天我使用JSTL,所以我在最后一点上有点生疏,但是如果没有前面的路径信息,你可以从请求中获取它并自己添加它。
#2
1
I would guess, that most implementations of the ServletConfig hold that mapping informations (org.apache.catalina.core.StandardWrapper does), but since the ServletConfig-Interface don't provides a getter, you'll have to do some tricks to get it and would bind your application to a specific implementation or application server.
我猜,ServletConfig的大多数实现都持有映射信息(org.apache.catalina.core.StandardWrapper),但由于ServletConfig-Interface不提供getter,你必须做一些技巧才能获得它会将您的应用程序绑定到特定的实现或应用程序服务器。
Maybe you just read it from the web.xml. Just select all "servlet-mapping" Elements with the given "servlet-name" and read the "url-pattern". Since this is in the spec, that should work on ever app server out there.
也许你只是从web.xml中读取它。只需选择具有给定“servlet-name”的所有“servlet-mapping”元素,并阅读“url-pattern”。由于这是在规范中,这应该适用于任何app服务器。
EDIT:
Here is the dirty example. Getting the URL mappings using refelction:
这是一个肮脏的例子。使用refelction获取URL映射:
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class<?> clazz = config.getClass();
Field configField = clazz.getDeclaredField("config");
configField.setAccessible(true);
StandardWrapper standardWrapper = (StandardWrapper) configField.get(config);
clazz = standardWrapper.getClass();
Field mappingsField = clazz.getDeclaredField("mappings");
mappingsField.setAccessible(true);
List<?> mappings = (List<?>) mappingsField.get(standardWrapper);
System.out.println(mappings);
}catch (Exception e) {
logger.error("", e);
}
}
That works in my JSF, Tomcat environment. The config Object is "org.apache.catalina.core.StandardWrapperFacade" and has a field called "config" which hold a "org.apache.catalina.core.StandardWrapper", which has a field called "mappings".
这适用于我的JSF,Tomcat环境。 config对象是“org.apache.catalina.core.StandardWrapperFacade”并且有一个名为“config”的字段,它包含一个“org.apache.catalina.core.StandardWrapper”,它有一个名为“mappings”的字段。
But as I said, this is a dirty hack!
但正如我所说,这是一个肮脏的黑客!
#3
1
Perhaps you should supply the URL of the second servlet to the first as a servlet parameter. I realise this means encoding the URL twice in the web.xml (which I really abhor), but to avoid problems you can always build the web.xml as part of your build, and populate from a properties file.
也许您应该将第二个servlet的URL作为servlet参数提供给第一个servlet。我意识到这意味着在web.xml中对URL进行两次编码(我真的很厌恶),但为了避免出现问题,您可以始终将web.xml构建为构建的一部分,并从属性文件中填充。
A little bit nasty and fiddly, I appreciate, but in the absence of any cross-container API solution, perhaps it's the most pragmatic solution.
我很欣赏,但是在没有任何跨容器API解决方案的情况下,这可能是最实用的解决方案。
#4
0
You can do it easily in your doGet() or doPost() Method following is the code to be written in doGet() or doPost() metod of servlet to get the mapping associated with a servlet.
您可以在doGet()或doPost()方法中轻松完成。以下是要在servlet的doGet()或doPost()方法中编写的代码,以获取与servlet关联的映射。
public void doGet(HttpServletRequest request,HttpServletResponse response){
String nameOfServlet = "S2";
ServletContext context = getServletContext();
String[] mappinggs=context.getServletRegistration(nameOfServlet).getMappings().toArray();
//mappings will contain all the mappings associated with servlet "S2"
//print take the first one if there is only one mapping
System.out.println(mappings[0]);
}