简体   繁体   中英

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.

My app packages up as myapp.war . So when it's served from a local Tomcat instance, I can access it by going to http://localhost:8080/myapp .

Very simply, I have a welcome-file ( myapp.html ) that I want served if Tomcat receives the following requests:

  • localhost:8080/myapp
  • localhost:8080/myapp/
  • localhost:8080/myapp/#
  • localhost:8080/myapp/#<blah>

...where <blah> is any string/regex after the pound symbol (#).

So if the user goes to http://localhost:8080/myapp , then serve back myapp.html . If the user goes to http://localhost:8080/myapp/#fjrifjri , then guess what? Serve back 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.

Currently my web.xml looks like:

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

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

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

     <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> 
  • in the doFilter() method use the following code.
  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. For opening specific servlet you can use the servlet tag as mentioned by Andrew.

您可以随时编写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:

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

<?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. I did not put welcome file list in web.xml as you can see above.

When I point the browser to http://localhost:8080/appctx or http://localhost:8080/appctx/ I land on the index file.

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

I hope this will help. good luck.

As others mentioned, you only have to sort out servlet mappings for /myapp ; 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. 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.

No need to create a filter. The simple solution is:

  1. Edit web.xml

     <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> 
  2. Rename myapp.html to index.html

  3. Edit index.html and put a tag with id="blah" attribute somewhere in there. This is to make localhost:8080/myapp/#<blah> work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM