我有2个servlet:登录和管理员。登录后,我想去管理页面。
登录
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if ("admin".equals(username)) {
RequestDispatcher requestDispatcher= request.getRequestDispatcher("/admin");
request.setAttribute("param1", "value1");
request.setAttribute("param2", "value2");
requestDispatcher.forward(request, response);
}
}
管理
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/pages/admin.jsp");
requestDispatcher.forward(request, response);
}
结果是
我想要什么
网址未更改的原因是我使用转发。但我想将一些参数传递给jsp页面,所以在这种情况下我不能使用 sendRedirect 。
我该怎么办?
答案 0 :(得分:0)
在HttpSession中存储您需要的属性:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if ("admin".equals(username)) {
HttpSession session = request.getSession();
session.setAttribute("param1", "value1");
session.setAttribute("param2", "value2");
response.sendRedirect("/admin");
}
}
这些属性可以在JSP中访问" admin" doGet(...)
转发,网址栏将包含所需内容: