Question

I have seen everywhere in the Spring MVC examples on net that we can use HttpServlet request & response objects in the method parameter in the controller. But when I am using it. Given in the below code.

  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.ModelAndView;

  @Controller
  public class StudentController {

  public void testSyntax(HttpServletRequest request, HttpServletResponse response)
  {
System.out.println("Inside testSyntax");
  }

  }

The compiler is throwing an error. HttpServletRequest request cannot be resolved to a type. I am using Spring MVC 3.0. Can anyone tell me the reason for it.

Was it helpful?

Solution

You are missing a couple of import statements.

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

Where you get these classes from typically is different per application container you use. My favorite example is Tomcat:

<tomcat_base_dir>/lib/servlet-api.jar
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top