Hima Rayaprolu
Hima Rayaprolu

Reputation: 123

How to know from which jsp page the request has come to the controller in Spring MVC

I am developing an app where LoginForm.jsp and UserRegistration.jsp will lead to UserAccount jsp page on specific action.In LoginForm when a user presses a 'Login' button ->UserAccount form is displayed.In UserRegistration Form when details are entered and submitted ->UserAccount form is displayed. Below is the controller code when a request comes as UserAccount.html

 @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST)
        public ModelAndView userAccountForm(@Valid @ModelAttribute("user") UserDetails user,BindingResult result) {

            if(result.hasErrors())

            {   System.out.println(result.getAllErrors());
                ModelAndView model1=new ModelAndView("UserRegistration");
                return model1;
            }
            // User validation


            System.out.println(user.getAccountType());
            userDAO.create(user);

            ModelAndView model1 = new ModelAndView("UserAccount");
            return model1;


}

LoginForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
    <h1
        style="background-color: green; color: Yellow; padding: 10px; text-align: center;">Mybank
        Online Login</h1>

    <form:errors path="user.*" />
    <form action="/MyBankProject/UserAccount.html" method="post">
        <div
            style="background-color: yellow; color: black; padding: 10px; text-align: center;"
            align="center">
            <p>
                User ID <input type="text" name="userName" />
            </p>
            <p>
                Password <input type="password" name="password" />
            </p>
            <input type="submit" value="Login" /> <a
                href="/MyBankProject/userRegistration.html">New User?</a>
        </div>
    <input type="hidden" name="page" value="LoginForm"/>

    </form>


</body>
</html>

UserRegistration.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
    <h1>${headerMessage}</h1>
    <h3>USER REGISTRATION</h3>

    <form:errors path="user.*" />

    <form action="/MyBankProject/UserAccount.html" method="post">
        <table border="1" style="width:100%" >

            <tr>
                <td>FirstName :</td>
                <td><input type="text" name="firstName" /></td>
            </tr>
            <tr>
                <td>LastName :</td>
                <td><input type="text" name="lastName" /></td>
            </tr>

            <tr>
                <td>User's EmailId :</td>
                <td><input type="text" name="emailId" /></td>
            </tr>
            <tr>
                <td>user's gender :</td>
                <td><select name="gender" multiple>
                        <option value="M">Male</option>
                        <option value="F">Female</option>
                </select></td>
            </tr>
            <tr>
                <td>user's age :</td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
                <td>user's DOB :</td>
                <td><input type="text" name="dOB" /></td>
            </tr>
            <tr>
                <td>Account Type :</td>
                <td><select name="accountType" multiple>
                        <option value="Savings">Savings</option>
                        <option value="Current">Current</option>
                        <option value="FixedDeposit">Fixed Deposit</option>
                </select>
                <td>
            </tr>
            <tr>
                <td>Amount :</td>
                <td><input type="text" name="amount" /></td>
            </tr>
        </table>
        <table>
            <tr>
                <td>UserName</td>
                <td><input type="text" name="userName" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password" /></td>
            </tr>
        </table>
        <table>
            <tr>
                <td>User's Address :</td>
            </tr>
            <tr>
                <td>country: <input type="text" name="address.country" /></td>
                <td>city: <input type="text" name="address.city" /></td>
                <td>street: <input type="text" name="address.street" /></td>
                <td>pincode:<input type="text" name="address.pincode" /></td>
            </tr>

            <tr>
                <td><button type="submit">Submit</button></td>
                <td><input type="button" onclick="history.back();"
                    value="Cancel"></td>
            </tr>
        </table>
     <input type="hidden" name="page" value="UserRegistration"/>

    </form>

</body>
</html>

Now for me I need to know the jsp page from which the UserAccount invoked ,to perform different actions based on that. I am new to Spring MVC and searched all over the internet for the solution.

Upvotes: 0

Views: 2502

Answers (2)

stevecross
stevecross

Reputation: 5684

HTTP is a stateless protocol. This means that you can not make assumptions about previous states.

I think you have two options to get information about the previous page anyways.

  1. Add a hidden field to your form and send the name of the page with the request.

    HTML:

     <form>
         <input type="hidden" name="page" value="[the name of the current page]"/>
     </form>
    

    Controller:

     @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST)
     public ModelAndView userAccountForm(@RequestParam(value="page", required=false) String page) {
         [...]
     }
    

    Now you can check the page value.

  2. Use the session. Store the current page in the controller methods that render the form:

     @RequestMapping("/login")
     public String login(HttpServletRequest request) {
         request.getSession().setAttribute("lastPage", "login");
    
         return "redirect:/UserAccount.html";
     }
    
     @RequestMapping("/register")
     public String register(HttpServletRequest request) {
         request.getSession().setAttribute("lastPage", "register");
    
         return "redirect:/UserAccount.html";
     }
    

    Check the lastPage value in userAccountForm():

     @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST)
     public ModelAndView userAccountForm(HttpServletRequest request) {
         HttpSession session = request.getSession();
         String lastPage = (String) session.getAttribute("lastPage");
    
         session.removeAttribute("lastPage");
    
         [...]
     }
    
  3. Check the referer field of the request header.

     @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST)
     public ModelAndView userAccountForm(@RequestHeader(value = "referer", required = false) String referer) {
         [...]
     }
    

    This gives you the referer as a method argument if there is one. Be careful. It is possible that there was no referer or that the field was manipulated by the client.

Upvotes: 5

greenhorn
greenhorn

Reputation: 654

Hope this will solve your need.

URL url = new URL(request.getHeader("referer")); 

System.out.println("last page url"+ url.getPath());
if(url.getPath().equals("your last url"){
//code
}

Upvotes: 1

Related Questions