title: cannot find symbol getServletContext() error in upload file on server using servlet and html
body: <p>Uploading a txt file on server using servlets with HTML form, but it is showing the following error:  </p>
  
  <pre><code>    cannot find symbol filePath = getServletContext().getInitParameter("file-upload");  
  </code></pre>
  
  <p>Jar files added:  </p>
  
  <pre><code>commons-fileupload-1.3.jar, commons-io-2.4.jar, servlet-api-3.0.jar
  </code></pre>
  
  <p>Using NetBeansIDE and parameters set in web.xml with path "C:\apache-tomcat-7.0.40\webapps\ROOT\WEB-INF"</p>
  
  <pre><code>&lt;context-param&gt; 
      &lt;description&gt;destination storing uploaded file&lt;/description&gt; 
      &lt;param-name&gt;file-upload&lt;/param-name&gt; 
      &lt;param-value&gt;
       C:\apache-tomcat-7.0.40\webapps\data\
       &lt;/param-value&gt; 
  &lt;/context-param&gt;  
  </code></pre>
  
  <p>Tried the following code:FileUpload3  </p>
  
  <pre><code>package fileupload3;
  
  import java.io.*;
  import java.util.*;
  
  import javax.servlet.*;
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.FileUploadException;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  import org.apache.commons.io.output.*;
  
  
  public class FileUpload3 {
  
      private boolean isMultipart;
  
     private String filePath;
     private int maxFileSize = 50 * 1024;
     private int maxMemSize = 4 * 1024;
     private File file ;
  
     public void init()  {
        // Showing cannot find symbol getServletContext() error here
        filePath = getServletContext().getInitParameter("file-upload");   
  
     }
     public void doPost(HttpServletRequest request, 
                 HttpServletResponse response)
                throws ServletException, java.io.IOException {
  
        isMultipart = ServletFileUpload.isMultipartContent(request);
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter( );
        if( !isMultipart ){
           out.println("&lt;html&gt;");
           out.println("&lt;head&gt;");
           out.println("&lt;title&gt;Servlet upload&lt;/title&gt;");  
           out.println("&lt;/head&gt;");
           out.println("&lt;body&gt;");
           out.println("&lt;p&gt;No file uploaded&lt;/p&gt;"); 
           out.println("&lt;/body&gt;");
           out.println("&lt;/html&gt;");
           return;
        }
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // maximum size that will be stored in memory
        factory.setSizeThreshold(maxMemSize);
        // Location to save data that is larger than maxMemSize.
        factory.setRepository(new File("c:\\temp"));
  
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        // maximum file size to be uploaded.
        upload.setSizeMax( maxFileSize );
  
        try{ 
        // Parse the request to get file items.
        List fileItems = upload.parseRequest(request);
  
        // Process the uploaded file items
        Iterator i = fileItems.iterator();
  
        out.println("&lt;html&gt;");
        out.println("&lt;head&gt;");
        out.println("&lt;title&gt;Servlet upload&lt;/title&gt;");  
        out.println("&lt;/head&gt;");
        out.println("&lt;body&gt;");
        while ( i.hasNext () ) 
        {
           FileItem fi = (FileItem)i.next();
           if ( !fi.isFormField () )  
           {
              // Get the uploaded file parameters
              String fieldName = fi.getFieldName();
              String fileName = fi.getName();
              String contentType = fi.getContentType();
              boolean isInMemory = fi.isInMemory();
              long sizeInBytes = fi.getSize();
              // Write the file
              if( fileName.lastIndexOf("\\") &gt;= 0 ){
                 file = new File( filePath + 
                 fileName.substring( fileName.lastIndexOf("\\"))) ;
              }else{
                 file = new File( filePath + 
                 fileName.substring(fileName.lastIndexOf("\\")+1)) ;
              }
              fi.write( file ) ;
              out.println("Uploaded Filename: " + fileName + "&lt;br&gt;");
           }
        }
        out.println("&lt;/body&gt;");
        out.println("&lt;/html&gt;");
     }catch(Exception ex) {
         System.out.println(ex);
     }
     }
     public void doGet(HttpServletRequest request, 
                         HttpServletResponse response)
          throws ServletException, java.io.IOException {
  
          throw new ServletException("GET method used with " +
                  getClass( ).getName( )+": POST method required.");
     } 
  }  
  </code></pre>
  
  <p>HTML Form: newhtml1.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;HTML Form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h3&gt;File Upload:&lt;/h3&gt;
  Select a file to upload: &lt;br /&gt;
  &lt;form action="FileUpload3" method="post" enctype="multipart/form-data"&gt;
  &lt;input type="file" name="file" /&gt;
  &lt;br /&gt;
  &lt;input type="submit" value="Upload File" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>