1

Following are my get() and post() method of my servlet. I am very new to servlet. When I call from my client, this servlet create a new page and overwrite all my html elements in my client. What I want to do is I just stay remain in my html page and let servlet do his job alone.

public void doPost(HttpServletRequest request, 
               HttpServletResponse response)
              throws ServletException, java.io.IOException {
      // Check that we have a file upload request
      isMultipart = ServletFileUpload.isMultipartContent(request);

      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();

      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("\\") >= 0 ){
               file = new File( filePath + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            fi.write( file ) ;

         }
      }

   }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.");
   } 
ksh
  • 141
  • 1
  • 3
  • 16

2 Answers2

4

What I want to do is I just stay remain in my html page and let servlet do his job alone.

This is the perfect candidate for using Asynchronous call AKA Ajax.

Learn AJAX.

http://api.jquery.com/jquery.ajax/

How to use Servlets and Ajax?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

If you want servlet should do process and it should show html page then you can use RequestDispatcher at the end of your try block in servlet. So when click on any action from your html page it will o to servlet and do proper action their and again it will redirect to html.

RequestDispatcher rd = request.getRequestDispatcher("index.html");
            rd.include(request, response);
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31