所有请求重定向特定网址

时间:2013-02-28 06:24:59

标签: java jsp tomcat servlets web

我在localhost:8084 / ues /中提供我的应用程序。我想将所有请求从localhost:8084 / ues / *重定向到localhost:8084 / ues / index.jsp。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

在部署描述符中定义过滤器,如

  <filter>
    <filter-name>Filter-Name</filter-name>
    <filter-class>Fully-Qualified-Class-Name</filter-class>
</filter>
  <filter-mapping>
    <filter-name>Filter-Name</filter-name>
    <url-pattern>ues/*</url-pattern>
  </filter-mapping>

并从Filter

重定向到您想要的Jsp

答案 1 :(得分:0)

web.xml中填写以下代码,

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/ues/*</url-pattern>
</servlet-mapping>


<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>com.myPackage.redirectServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>


public class redirectServletextends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) 
{

        try 
        {
            RequestDispatcher dispatcher = request.getRequestDispatcher("/pages/index.jsp");
            dispatcher.forward(request, response);
        } 
        catch(Exception e)
                {
                  //catch your exceptions
                }
}
}