Reputation: 105
I need to redirect to a page only after clicking the submit button, but not directly typing the url in the address bar. If it was directly typed in address bar, then it should display the home page.
Iam using session.getAttribute to do the above process. I am thinking if there is any altrnative for this, as I need to do this for every post method...
The below method is for first page, where I created a session attribute which is used in the next page.
@RequestMapping(value = "/payment", method = RequestMethod.POST)
public String submitForPayment(@ModelAttribute("deposit") Deposit deposit, ModelMap model, HttpServletRequest request) throws IOException
{
try {
HttpSession sessionForNextPage = request.getSession(true);
sessionForNextPage.setAttribute("vNumber",
deposit.getValidityNumber());
return "redirect:success";
} catch (NullPointerException exception) {
return "redirect:payment";
}
}
The method below is for the next page where the session declared above is used.
@RequestMapping(value = "/success", method = RequestMethod.GET)
public String showSuccess(ModelMap model, HttpServletRequest request)
{
try {
view = "success";
HttpSession session = request.getSession(false);
int vNumber = (int) session.getAttribute("vNumber");
System.out.println(vNumber);
if (vNumber != 0) {
request.getSession(false).removeAttribute("vNumber");
return view;
}
else
return "pay";
} catch (Exception e) {
return "redirect:pay";
}
}
Is there any other way to do this, as I have to do this for all the methods...
Upvotes: 3
Views: 2334
Reputation: 6079
Use the interceptor and determine the referrer url in the interceptor method. Call interceptor for every action or some of the actions you need to intercept.
public class AppInterceptor extends HandlerInterceptorAdapter{
//before the actual handler will be executed
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
return true;
}
//after the handler is executed
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
String referrer = request.getHeader("referer");
// if the referrer string is null, it means the url is invoked by typing into address bad and then you can decide of redirecting user to home page.
}
}
This will work.
Upvotes: 0
Reputation: 315
Whenever i have to do the post submit , I always use a intermediate page to capture POST data and store the data in database and store the record key in the session and redirect it to the display page where i check for the record id if its there then i retrieve data from DB and display it if not display a error msg.
So even if somebody access your display page directly (Typed in url) it will display a error msg , And in most cases people will not see the intermediate page url but even if they do you can use random token for the your html FORM and store in session and validate it on intermediate page.
Hope this will help you.
Upvotes: 3