如何在 resp.sendRedirect 之后获取 HttpServletRequest 属性
How to get a HttpServletRequest attribute after resp.sendRedirect
好的,这个问题不是很复杂,但是很难解释。
我正在开发一个纯 Jee 网络应用程序(没有 Spring)。
我有一个由用户填写的简单表格,我想要两个不同的结果。
如果在我验证数据或将其保存到数据库时出现问题,它会被发送回相同的表单,并显示一些错误(那部分没问题)。
但是如果表格和存储没问题,我希望将它发送到用户的仪表板,并显示一行显示 "recording complete".
我以为我可以做一个
this.getServletContext().getRequestDispatcher("/WEB-INF/dashboard.jsp").forward(req, resp);
但无法通过 jsp 重定向到我的仪表板,因为当我显示仪表板时,会执行 sql 请求。
所以我认为我可以做到:
resp.sendRedirect("/dashboard.do");
再次通过我的 servlet,以执行我的请求。
我在添加到请求的模型中使用了一个布尔值 "result",但是如果我执行 resp.sendRedirect
就无法取回它
我试图在响应中设置一个 header 并进行测试,但没有成功。
这是我的 servlet 的摘录:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (path.equals("/saveSpot.do")){
.......
CheckForm form = new CheckForm();
form.checkAndSave(req, "com.alain.dao.entities.Spot", spotDao);
//here are the treatments on the form
....
if (form.getResult()){
resp.setHeader("result","true");
resp.sendRedirect("/dashboard.do");
} else {
doGet(req,resp);
}
}
}
...
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(path.equals("/ajoutSpot.do") || path.equals("/saveSpot.do")){
// If the Post had a problem, it is sent back here
...
some stuff are done
...
// and the same jsp is called to display the errors
this.getServletContext().getRequestDispatcher("/WEB-INF/ajoutSpot.jsp").forward(req, resp);
}else if(path.equals("/dashboard.do")){
//If the form is ok it is sent here
//I try to test the header but it doesnt work
if (resp.containsHeader("result")){
req.setAttribute("result",true);
}
// I do my request
SpotDaoImpl spotDao = new SpotDaoImpl();
List<Spot> listSpots = spotDao.findAll();
req.setAttribute("spots", listSpots);
// And display the dashboard
this.getServletContext().getRequestDispatcher("/WEB-INF/dashboard.jsp").forward(req, resp);
}
我认为我对结果的处理很笨拙,但我不知道如何做得更好。
感谢您的宝贵时间!
为了结束这个话题,我只是在 URL resp.sendRedirect("/dashboard.do?resultat=true");
中传递了一个布尔参数
好的,这个问题不是很复杂,但是很难解释。 我正在开发一个纯 Jee 网络应用程序(没有 Spring)。
我有一个由用户填写的简单表格,我想要两个不同的结果。 如果在我验证数据或将其保存到数据库时出现问题,它会被发送回相同的表单,并显示一些错误(那部分没问题)。 但是如果表格和存储没问题,我希望将它发送到用户的仪表板,并显示一行显示 "recording complete".
我以为我可以做一个
this.getServletContext().getRequestDispatcher("/WEB-INF/dashboard.jsp").forward(req, resp);
但无法通过 jsp 重定向到我的仪表板,因为当我显示仪表板时,会执行 sql 请求。
所以我认为我可以做到:
resp.sendRedirect("/dashboard.do");
再次通过我的 servlet,以执行我的请求。
我在添加到请求的模型中使用了一个布尔值 "result",但是如果我执行 resp.sendRedirect
就无法取回它我试图在响应中设置一个 header 并进行测试,但没有成功。
这是我的 servlet 的摘录:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (path.equals("/saveSpot.do")){
.......
CheckForm form = new CheckForm();
form.checkAndSave(req, "com.alain.dao.entities.Spot", spotDao);
//here are the treatments on the form
....
if (form.getResult()){
resp.setHeader("result","true");
resp.sendRedirect("/dashboard.do");
} else {
doGet(req,resp);
}
}
}
...
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(path.equals("/ajoutSpot.do") || path.equals("/saveSpot.do")){
// If the Post had a problem, it is sent back here
...
some stuff are done
...
// and the same jsp is called to display the errors
this.getServletContext().getRequestDispatcher("/WEB-INF/ajoutSpot.jsp").forward(req, resp);
}else if(path.equals("/dashboard.do")){
//If the form is ok it is sent here
//I try to test the header but it doesnt work
if (resp.containsHeader("result")){
req.setAttribute("result",true);
}
// I do my request
SpotDaoImpl spotDao = new SpotDaoImpl();
List<Spot> listSpots = spotDao.findAll();
req.setAttribute("spots", listSpots);
// And display the dashboard
this.getServletContext().getRequestDispatcher("/WEB-INF/dashboard.jsp").forward(req, resp);
}
我认为我对结果的处理很笨拙,但我不知道如何做得更好。 感谢您的宝贵时间!
为了结束这个话题,我只是在 URL resp.sendRedirect("/dashboard.do?resultat=true");