web.xml和/或过滤器返回welcome-file

时间:2013-12-27 21:54:17

标签: java tomcat servlets web.xml servlet-filters

我需要为特定功能配置我的Tomcat WAR,并且不确定它是否可以通过web.xml完成,或者我是否需要实现1+自定义Filter,或者使用其他一些hackery。

我的应用程序打包为myapp.war。因此,当它从本地Tomcat实例提供服务时,我可以通过转到http://localhost:8080/myapp来访问它。

很简单,如果Tomcat收到以下请求,我想要一个welcome-filemyapp.html):

  • 本地主机:8080 / MyApp的
  • 本地主机:8080 / MyApp的/
  • 本地主机:8080 / MyApp的/#
  • 本地主机:8080 / MyApp的/#<等等>

...其中<blah>是井号(#)后面的任何字符串/正则表达式。

因此,如果用户转到http://localhost:8080/myapp,则返回myapp.html。如果用户转到http://localhost:8080/myapp/#fjrifjri,那么猜猜是什么?退回myapp.html

但是,如果用户转到http://localhost:8080/myapp/fizz,那么我想要正常的web.xml servlet-mapping逻辑启动,我希望Tomcat能够回送任何servlet映射到/fizz

目前我的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>

我该如何做到这一点?

9 个答案:

答案 0 :(得分:11)

如果您需要使用网址,则需要使用 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>

哪里的demo是你的包,你的fizz.java发誓 要修改附加到当前URL的URL和文件,您需要使用 servlet-mapping 标记,其中名为fizz的servlet映射到/ fizz

这将允许您更改要查找的设置。

希望它有所帮助...

答案 1 :(得分:2)

使用过滤器 -

<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);
}
}

答案 2 :(得分:2)

实现此目的的一种方法是使用过滤器如下:

  • 创建一个映射到/中的根web.xml的过滤器,如下所示

    <filter>
      <filter-name>myWelcomeFilter</filter-name>
      <filter-class>com.test.MyWelcomeFilter</filter-class>
    </filter> 
    
  • 和过滤器映射如下

<filter-mapping>
  <filter-name>myWelcomeFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
    doFilter()方法中的
  • 使用以下代码。
 String pathInfo = request.getPathInfo();
 if(null == pathInfo || pathInfo.startsWith("#") ){
 response.sendRedirect("/myapp/myapp.html")
 }else{
  chain.doFilter(request, response);
 }

答案 3 :(得分:1)

我认为你要做的是默认行为。我不知道你为什么要为此目的使用过滤器。网址:http://localhost:8080/myapp/#fjrifjri  如果你在web.xml中使用myapp.html作为欢迎文件,那么任何类似的东西都会出现在myapp.html中。要打开特定的servlet,您可以使用Andrew提到的servlet标记。

答案 4 :(得分:1)

您可以随时编写javascript或jsp或servlet代码,您可以在其中判断URL并转发请求。

答案 5 :(得分:1)

我最近遇到了同样的问题,至少在你的前两个要求方面是这样。这就是我解决问题的方法:

我的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>

我的应用程序上下文(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>

我将索引文件(index.jsp)放在TOMCAT_HOME / webapps / appctx / WEB-INF / jsp中。我没有把欢迎文件列表放在web.xml中,如上所示。

当我指向浏览器时     http://localhost:8080/appctxhttp://localhost:8080/appctx/我登陆索引文件。

我使用了spring-framework-3.1.2并在Windows 7上的tomcat-7.0.27中运行应用程序。(我的tomcat开发服务器中只有两个应用程序--ROOT和appctx

我希望这会有所帮助。祝你好运。

答案 6 :(得分:1)

正如其他人提到的,你只需要为/myapp整理出servlet映射; #之后的映射实际上是多余的。 #之后的内容,以及哈希本身are never sent to the server anyway

答案 7 :(得分:1)

web.xml和servlet无法识别URL中的“#”,这纯粹是一个前端构造,它会告诉浏览器您想要访问的页面中的哪个链接。您需要做的是为主页设置映射,如下所示:

<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>

然后照常执行其余的servlet映射。

答案 8 :(得分:1)

无需创建过滤器。简单的解决方案是:

  1. 编辑web.xml

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
  2. 将myapp.html重命名为index.html

  3. 编辑index.html并在其中放置一个id="blah"属性的标记。这是为了 让localhost:8080/myapp/#<blah>工作。