使用RequestDispatcher将数据从servlet传递到另一个servlet
问题描述:
我正在尝试使用RequestDispatcher将数据从一个servlet传递到另一个.这是我的调度程序代码.
I am trying to pass data from one servlet to another using the RequestDispatcher. This is my code for the Dispatcher.
String address;
address = "/Java Resources/src/coreservlets/MapOut.java";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
当我尝试运行它时,它给我一个错误,指出该路径不可用.我是否必须包括一些东西,以便调度程序发送到另一个servlet?
When I try to run it, it gives me an error saying the path is unavailable. Do I have to include something for the dispatcher to send to another servlet?
答
您只需要在getRequestDispatcher
中传递servlet-mapping
的url-pattern
.
假设您的servlet映射是web.xml
中"MapOut" Servlet的"myMap".那么应该是
Let say your servlet mapping is "myMap" for the "MapOut" Servlet in the web.xml
.Then it should be
RequestDispatcher dispatcher = request.getRequestDispatcher("/myMap");
dispatcher.forward(request,response);
转发的Servlet的
doGet()
将被调用.
doGet()
of forwarded Servlet will be called.
示例:web.xml
Example: web.xml
<servlet>
<description></description>
<servlet-name>MapOut</servlet-name>
<servlet-class>coreservlets.MapOut</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MapOut</servlet-name>
<url-pattern>/myMap</url-pattern> <!-- You can change this-->
</servlet-mapping>