4

I'm trying to upload pdf files in the server. And i;m using the following block of code into the controller:

 @RequestMapping(value = /submit, method = RequestMethod.POST)
 public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) {

   //some code here

   String name = request.getServletContext().getRealPath("/pdf/" + filename);
   File dest = new File(name);
   try {
        file.transferTo(dest);
   }catch(Exception e){
        System.err.println(e);
   }

   return "redirect:/details";

I'm doing this in order to store the pdf's into the pdf file. In my localhost works fine but when i'm executing this on the server i'm taking the following exception:

exception

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
frontend.controller.EsteemRatingsController.handleFormUpload(EsteemRatingsController.java:113)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

If i remove the lines that provide above in the controller class is working(ofcource without uploading the pdf's). Can anyone help me with this?

1
  • What web container are you using? Looks like you're running on something old. Commented Jun 19, 2012 at 14:40

1 Answer 1

21

That method request.getServletContext() was introduced in servlet 3.0. Make sure your container/library support that version.

edit: tomcat 6 only have servlet 2.5, see http://tomcat.apache.org/whichversion.html

it can be autowired: ServletContext and Spring MVC

public class Xxxx{
    @Autowired
    ServletContext context;

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) {

       //some code here

       String name = context.getRealPath("/pdf/" + filename);
...
Sign up to request clarification or add additional context in comments.

5 Comments

So instead of this method which method can i use that apache 6.0.24 can support it?
Can spring inject ServletContext for you? Haven't tried, but i guess it can.
Thank you a lot for you replies and your time. I saw on the internet that it can but i'm very new in all this and i can't understand.Can you help me?
just add ServletContext to your method signature: public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session, ServletContext ctx) { and spring should do the magic for you.
Thank you again but there is a problem.The ServletContext is an inerface and so i have an error which says "Could not instantiate bean class [javax.servlet.ServletContext]: Specified class is an interface"

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.