您可以使用RequestDispatcher.forward
servlet 中的方法:
public class YourServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = getServletConfig().getServletContext()
.getRequestDispatcher("/yourJsp.jsp");
rd.forward(request, response);
}
}
另请参阅javaee 5 教程
编辑:我已经在 tomcat 7 示例上测试了这种方法:
- 我已启用示例定时过滤器
- 我调用了 servletToJsp 示例(在此示例中,servlet 转发到 jsp)
一切都很完美。过滤器完成了他的工作,没有例外。
您应该从一开始就指定您有问题并提供堆栈跟踪等。
关于评论中的问题:
为什么不 request.getRequestDispatcher ?
在我提供的示例中,因为我调用的是绝对路径,所以没有区别。request.getRequestDispatcher
处理相对路径时很有用。请参阅这个SO 问题。
编辑2:
我使用过滤器、servlet 和 jsp 附加源。
筛选:
package tests.filters;
import javax.servlet.*;
import java.io.IOException;
public final class ExampleFilter implements Filter {
private String attribute = null;
private FilterConfig filterConfig = null;
@Override
public void destroy() {
this.attribute = null;
this.filterConfig = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (attribute != null)
request.setAttribute(attribute, this);
long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
filterConfig.getServletContext().log
(this.toString() + ": " + (stopTime - startTime) +
" milliseconds");
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig = fConfig;
this.attribute = fConfig.getInitParameter("attribute");
}
@Override
public String toString() {
if (filterConfig == null)
return ("TimingFilter()");
StringBuilder sb = new StringBuilder("TimingFilter(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}
}
小服务程序:
package tests.servlets;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletToJsp extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet (HttpServletRequest request,
HttpServletResponse response) {
try {
request.setAttribute ("servletName", "servletToJsp");
getServletConfig().getServletContext().getRequestDispatcher(
"/jsp/jsp-to-servlet/hello.jsp").forward(request, response);
} catch (Exception ex) {
ex.printStackTrace ();
}
}
}
JSP:
<html>
<body bgcolor="white">
<h1>
I have been invoked by
<% out.print (request.getAttribute("servletName").toString()); %> Servlet.
</h1>
</html>
web.xml
<web-app 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"
version="3.0"
metadata-complete="true">
<filter>
<filter-name>Timing Filter</filter-name>
<filter-class>tests.filters.ExampleFilter</filter-class>
<init-param>
<param-name>attribute</param-name>
<param-value>tests.filters.ExampleFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Timing Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ServletToJsp</servlet-name>
<servlet-class>tests.servlets.ServletToJsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletToJsp</servlet-name>
<url-pattern>/servletToJsp</url-pattern>
</servlet-mapping>
</web-app>
我还提供了一个下载链接,其中包含打包为战争的示例:http: //goo.gl/GaV5w(7 天可用)
只需将文件复制到您的tomcat 7 webapps 目录中并从浏览器调用:http://localhost:8080/webapp-javaee6-1.0-SNAPSHOT/servletToJsp
. 您将在 logs/localhost.currentDate.log 文件中看到过滤器打印处理时间,而 jsp 显示其内容。
我不能再做任何事情来说服您这种方法有效并且您的问题(如果您有问题)在其他地方。