//however, if I use ServletContext context = config.getServletContext(); //then it compiles //why is that? //shouldn't the HttpServlet.getServletConfig() return the same ServletConfig that config points to? [ April 02, 2005: Message edited by: Joseph Sweet ]
A servlet configuration object used by a servlet container used to pass information to a servlet during initialization.
[ April 02, 2005: Message edited by: Ben Souther ]
Joined: Jan 29, 2005
I am trying to upload files from a client to the server.
As I mentioned before, you cannot use "this.getServletContext()" inside a service method (like doPost, service...) since this method is not defined in the HttpServlet class, it is defined in the ServletConfig class, and you get the one that matches your specific servlet by.... calling to getServletConfig() from within your service methods (doPost, service...).
You can see the examples for that on the web.
Okay here is the full code. I replaced config.getServletContext().getRealPath("/tempUpload/") with this.getServletContext().getRealPath("/tempUpload/") as you suggested and of course I got
my question is: why does the code work when I use config.getServletContext().getRealPath("/tempUpload/") (I got the config value through the HttpServlet's init() method), but it won't work when I use getServletConfig().getServletContext().getRealPath("/tempUpload/")
response.setContentType("text/html"); PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
out.println("Content Type = " + request.getContentType());
boolean fileExistsArray; fileExistsArray = new boolean[ServletUtilities.getAttachNum()];
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory. // including form field factory.setSizeThreshold(4096); //defualt is 1024 byte
//the location for saving data that is larger than getSizeThreshold() factory.setRepository(new File(this.getServletContext().getRealPath("/tempUpload/"))); out.println(config.getServletContext().toString());
ServletFileUpload upload = new ServletFileUpload(factory);
//maximum size before a FileUploadException will be thrown //-1 for no maximum. upload.setSizeMax(ServletUtilities.getMaxUploadedFileSize());
Then why does this line: getServletContext().getRealPath("/tempUpload/")
The answer is in the API:
getRealPath public java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
Parameters: path - a String specifying a virtual path Returns: a String specifying the real path, or null if the translation cannot be performed
By over-riding the init method you are preventing the base class from initializing.
Your should either invoke the base class init method from yours, or bettter yet, over-ride the init() method that takes no paramters. This method is called from the base class init method and so you don't have to worry about the base class initialization.
If you use init(servletconfig) and forgot to call super.init(config) then servletconfig object will not be set to the actual one and you will not get the servletconfig object. As a result, in this case, you are calling getServletContext() on a null servletConfig object hence getting null pointer exception.
It is BETTER to use init(). If you use init() you have no such worries as calling super.init().