[英]web.xml and/or Filters to return welcome-file
I have a need to configure my Tomcat WAR for specific functionality, and not sure if it can all be accomplished via web.xml
, or if I need to implement 1+ custom Filter
s, or use some other type of hackery. 我需要为特定功能配置我的Tomcat WAR,并且不确定它是否可以通过
web.xml
完成,或者我是否需要实现1+自定义Filter
,或者使用其他类型的hackery。
My app packages up as myapp.war
. 我的应用程序打包为
myapp.war
。 So when it's served from a local Tomcat instance, I can access it by going to http://localhost:8080/myapp
. 因此,当它从本地Tomcat实例提供时,我可以通过访问
http://localhost:8080/myapp
来访问它。
Very simply, I have a welcome-file
( myapp.html
) that I want served if Tomcat receives the following requests: 非常简单,如果Tomcat收到以下请求,我有一个
welcome-file
( myapp.html
),我想要服务:
...where <blah>
is any string/regex after the pound symbol (#). ...其中
<blah>
是井号(#)后的任何字符串/正则表达式。
So if the user goes to http://localhost:8080/myapp
, then serve back myapp.html
. 因此,如果用户访问
http://localhost:8080/myapp
,则返回myapp.html
。 If the user goes to http://localhost:8080/myapp/#fjrifjri
, then guess what? 如果用户转到
http://localhost:8080/myapp/#fjrifjri
,那么猜猜是什么? Serve back myapp.html
. 发回
myapp.html
。
But , if the user goes to, say, http://localhost:8080/myapp/fizz
, then I want normal web.xml servlet-mapping
logic to kick in, and I want Tomcat to serve back whatever servlet is mapped to /fizz
, etc. 但是 ,如果用户转到
http://localhost:8080/myapp/fizz
,那么我想要正常的web.xml servlet-mapping
逻辑启动,我希望Tomcat能够回送任何servlet映射到的/fizz
等
Currently my web.xml
looks like: 目前我的
web.xml
看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<welcome-file-list>
<welcome-file>myapp.html</welcome-file>
</welcome-file-list>
</web-app>
How can I accomplish this? 我怎么能做到这一点?
If you need to mess around with URLs you need to use servlet and servlet-mapping tags: 如果您需要使用URL,则需要使用servlet和servlet-mapping标记:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<welcome-file-list>
<welcome-file>myapp.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>fizz</servlet-name>
<servlet-class>demo.fizz</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fizz</servlet-name>
<url-pattern>/fizz</url-pattern>
</servlet-mapping>
</web-app>
where demo is your package and fizz your fizz.java demo是你的包,你的fizz.java
To modify url and files attached to the current url you need to use servlet-mapping tags where servlet called fizz is mapped to /fizz 要修改附加到当前url的url和文件,你需要使用servlet-mapping标签,其中名为fizz的servlet映射到/ fizz
This will allow you to change settings you are looking for. 这将允许您更改要查找的设置。
Hope it helps... 希望能帮助到你...
Using filter - 使用过滤器 -
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>package.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class MyFilter implements javax.servlet.Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
//this method calling before controller(servlet), every request
HttpServletRequest request = (HttpServletRequest) req;
String url = request.getRequestURL().toString();
if(url.lastIndexOf("/fizz") > -1) //fix this as it could be /fizz33 too
{
RequestDispatcher dispatcher = request.getRequestDispatcher("fizz.jsp"); //or whatever page..
dispatcher.forward(req, res);
} else {
fc.doFilter(req, res);
}
}
One way to accomplish this is using filters is as follows: 实现此目的的一种方法是使用过滤器如下:
Create a filter which is mapped to root /
in web.xml
as follows 创建一个映射到root
/
web.xml
的过滤器,如下所示
<filter> <filter-name>myWelcomeFilter</filter-name> <filter-class>com.test.MyWelcomeFilter</filter-class> </filter>
and the filter mapping as follows 和过滤器映射如下
<filter-mapping> <filter-name>myWelcomeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
doFilter()
method use the following code. doFilter()
方法中使用以下代码。 String pathInfo = request.getPathInfo(); if(null == pathInfo || pathInfo.startsWith("#") ){ response.sendRedirect("/myapp/myapp.html") }else{ chain.doFilter(request, response); }
I think what you are trying to do is a default behavior. 我认为你要做的是默认行为。 I dont know why do you want to use a filter for this purpose.
我不知道你为什么要为此目的使用过滤器。 The url :
http://localhost:8080/myapp/#fjrifjri
or anything like that would land up in myapp.html if you use myapp.html as welcome file in web.xml. url:
http://localhost:8080/myapp/#fjrifjri
或类似的东西会在myapp.html中出现,如果你在web.xml中使用myapp.html作为欢迎文件。 For opening specific servlet you can use the servlet tag as mentioned by Andrew. 要打开特定的servlet,您可以使用Andrew提到的servlet标记。
您可以随时编写javascript或jsp或servlet代码,您可以在其中判断URL并转发请求。
I recently had the same problem as you have, at least in regards with the first two of your requirements. 我最近遇到了同样的问题,至少在你的前两个要求方面是这样。 This is how I solved my problem:
这就是我解决问题的方法:
My web.xml has this: 我的web.xml有这个:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:../myapp-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and my application context (myapp-context.xml) has this: 和我的应用程序上下文(myapp-context.xml)有这个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.toolkt.spring.mvc.app"/>
<mvc:annotation-driven />
<mvc:view-controller path="/" view-name="index" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
I put my index file (index.jsp) in TOMCAT_HOME/webapps/appctx/WEB-INF/jsp. 我把我的索引文件(index.jsp)放在TOMCAT_HOME / webapps / appctx / WEB-INF / jsp中。 I did not put welcome file list in web.xml as you can see above.
我没有把欢迎文件列表放在web.xml中,如上所示。
When I point the browser to http://localhost:8080/appctx
or http://localhost:8080/appctx/
I land on the index file. 当我将浏览器指向
http://localhost:8080/appctx
或http://localhost:8080/appctx/
I登陆索引文件时。
I used spring-framework-3.1.2 and is running the app in tomcat-7.0.27 on windows 7. (There are only two apps in my tomcat dev server - ROOT and appctx 我使用spring-framework-3.1.2并在Windows 7上的tomcat-7.0.27中运行应用程序。(我的tomcat开发服务器中只有两个应用程序--ROOT和appctx
I hope this will help. 我希望这将有所帮助。 good luck.
祝好运。
As others mentioned, you only have to sort out servlet mappings for /myapp
; 正如其他人提到的,你只需要为
/myapp
理清servlet映射; the mappings after #
are, in fact redundant. #
之后的映射实际上是多余的。 The content after #
, as well as the hash itself are never sent to the server anyway . #
之后的内容以及散列本身永远不会发送到服务器 。
web.xml and servlets do not recognize '#' in URLs, that is purely a front-end construct, it tells the browser which link within the page you want to go to. web.xml和servlet不识别URL中的'#',它纯粹是一个前端构造,它告诉浏览器你要去的页面中的链接。 What you'll need to do is setup a mapping for the home page like so:
您需要做的是为主页设置映射,如下所示:
<servlet>
<servlet-name>WelcomePage</servlet-name>
<jsp-file>/myapp.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>WelcomePage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and then do the rest of your servlet mappings as usual. 然后照常执行其余的servlet映射。
No need to create a filter. 无需创建过滤器。 The simple solution is:
简单的解决方案是:
Edit web.xml 编辑web.xml
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
Rename myapp.html to index.html 将myapp.html重命名为index.html
id="blah"
attribute somewhere in there. id="blah"
属性的标记。 This is to make localhost:8080/myapp/#<blah>
work. localhost:8080/myapp/#<blah>
工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.