如果我有两个 servlet:
@WebServlet (urlPatterns = {"/s1"})
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("A", 100);
map.put("B", 200);
map.put("C", 300);
req.setAttribute("map", map);
getServletContext().getRequestDispatcher("Servlet2").forward(req, resp);
}
}
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Map map = (Map) req.getAttribute("map");
for (Object o : map.values()) {
System.out.println(o);
}
}
}
如何在它们之间进行重定向?我需要在 getRequestDispatcher 方法中使用哪个路径?还有一个条件 - Servlet2 必须在注释或 web.xml 中没有任何映射。