Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring 3 MVC accessing HttpRequest from controller

    • 0
    • 2
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 2.50k
    Comment on it

    Spring 3 MVC accessing HttpRequest from controller

    To access HttpRequest from controller you just need to define HttpServletRequest and HttpServletResponse as parameters in a function signature of your Controller. by doing this you allowing Spring MVC to pass these objects to you controller method.

    Here is the sample code to access HttpRequest from controller:

    login.jsp:

    <!DOCTYPE html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
    </head>
    
    <body>
    <div class="wrapper">
        <div>
            <form name="LoginForm"  action="authenticateUser">
                <div class="header"></div>
                <h2>Flippadoo Login</h2>
                    <dl>
                        <dt>userid</dt>
                        <dd><input type="text"name="userName"></dd>
                        <dt>password</dt>
                        <dd><input type="password" name="userPassword"></dd>
                    </dl>
                    <div><input type="submit" name="button1" value="login"></div>
             </form>
    
    
        </div>
    </div>
    </body>
    </html
    

    Now define the below function in your controller:

    @RequestMapping(value = "/authenticateUser", method = RequestMethod.GET)
    public String authenticateUser(HttpServletRequest request,
            HttpServletResponse response, @RequestParam("userName") String userName,
            @RequestParam(value = "userPassword", required = false) String userPassword)
    {
        if(userName != null && !userName.trim().equals(""))
        {
            //here you can use your method to check user credentials
            LoginResponse loginResponse = userService.getUserCredentials(userName, userPassword);
            if (loginResponse != null && loginResponse.getStatus() == ServiceAPIStatus.OK.getStatus())
            {
                logger.debug("User Passed ::");
                //Here I'm using request object to set user object into the seesion for further use
                request.getSession().setAttribute("user", loginResponse.getUser());
                return "redirect:home";
            }
        }
    
        //You can use response object like:
        //response.getOutputStream().write("write anything");
    
    
        logger.debug("User Failed ::");
        return "redirect:loginPage?error=true";
    }
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: