【发布时间】:2014-01-06 20:33:28
【问题描述】:
如果我有两个 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 中没有任何映射。
【问题讨论】:
-
映射对于 servlet 是强制性的...
-
但我需要对用户不可见的 Servlet2。它无法从浏览器访问。如果我使用映射用户可以访问 servlet。
-
您确定要 Servlet 而不是 JSP(这是一种 servlet,不会在 web.xml 中设置任何注释或地址)吗?
-
太糟糕了,因为通过将您的 JSP 放在
WEB-INF/yourJspFile.jsp中,您可以通过请求调度程序向它发送请求。它还会从浏览器中隐藏这个 JSP“地址”。
标签: java servlets redirect requestdispatcher