4

I have a controller class in Spring MVC project. My code is like below:

@Controller
public class MainController {

@RequestMapping(value = "/doLogin")
public String login(@RequestParam("email") String email, @RequestParam("pwd") String password, Model model){
   //some code
  }
}

I want to pass HttpSession as method parameter but I'm not able to access HttpSession. I've attached a screenshot: enter image description here

1
  • Did my answer solve your problem? If solved then you can also accept my answer for fellow SO users. Commented Oct 10, 2017 at 12:27

3 Answers 3

4

This is because you don't have servlet api dependency in your classpath.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to have java servlet in your project. Add this to your pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

3 Comments

I just posted it before seeing your post !
You can compare time :) My answer was posted before yours
What's the matter ? I just told you that yes you posted it before it's just when I was writting the answer. I didn't see your post !
2

You can get your HttpSession from HttpServletRequest. req.getSession(false) returns a HttpSession object if already created,if not then null returned. req.getSession(true) check if no session created, then returned a new one instead of null. N.B: Also make your that javax servlet api jar added to your class path.

@Controller
public class MainController {
@RequestMapping(value = "/doLogin")
public String login(
    HttpServletRequest req, 
    @RequestParam("email") String email,
    @RequestParam("pwd") String password, 
    Model model
){
    HttpSession session = req.getSession(false);
   //some code
  }
}

2 Comments

Unfortunately, this was not my actual problem. I couldn't use HttpSession even inside of any method. I've solved this problem after importing javax.servlet jar library
I also said it in last line (N.B) btw. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.