Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:53:08+00:00 2026-06-13T06:53:08+00:00

From the server Code (in an HttpServlet) I’m throwing an exception if the file

  • 0

From the server Code (in an HttpServlet) I’m throwing an exception if the file is too large:

 public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
 ...
 // Check if the blob has correct size, otherwise delete it
 final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
 long size = blobInfo.getSize();
 if(size > 0 && size <= BasicConstants.maxImageSize){
    res.sendRedirect("/download?blob-key=" + blobKey.getKeyString());
 } else { // size not allowed
    bs.delete(blobKey);
    throw new RuntimeException(BasicConstants.fileTooLarge);
 }

From the client code I’m missing to successfully catch the exception with this snippet:

try {
    uploadForm.submit(); // send file to BlobStore, where the doPost method is executed
} catch (Exception ex) {
    GWT.log(ex.toString());
}

However, from this other client code snippet I’m somehow detecting when the exception was thrown with an ugly workaround that I don’t trust at all:

uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

    @Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// This is what gets the result back - the content-type *must* be
// text-html
String imageUrl =event.getResults();

    // This ugly workaround apparently manages to detect when the server threw the exception
if (imageUrl.length() == 0) { // file is too large
  uploadFooter.setText(BasicConstants.fileTooLarge);
} else { // file was successfully uploaded
       ...
    }

The Development Mode view in Eclipse reports an error of type “uncaught exception”, which suggests that I’m really doing a bad job at detecting it.

Can anyone tell me how to properly catch the exception, and if the workaround I’m using makes any sense?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:53:10+00:00Added an answer on June 13, 2026 at 6:53 am

    Your first attempt

    try {
        uploadForm.submit(); // send file to BlobStore, where the doPost method is executed
    } catch (Exception ex) {
        GWT.log(ex.toString());
    }
    

    doesn’t work, because submit() doesn’t wait until the browser receives the response (it’s an asynchronous call).

    uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
    
      @Override
      public void onSubmitComplete(SubmitCompleteEvent event) {
        ...
    

    Here you actually receive the response from the server. But it’s a form submission, not a GWT-RPC call, so the result is just plain text, not GWT Java Objects.

    When you throw a RuntimeException in a Servlet, the server will simply send a response with an error code (probably ‘500’, but ideally use the “Network” tab in Firebug or Chrome Developer Tools to see the actual response and response code.) So in the success case you’ll get the URL, otherwise the response is empty.

    Possible solution

    You can catch Exceptions on the server side, and send better descriptions explicitly:

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    
      try {
    
          ...
          if (...) {
            throw new MyTooLargeException();
          } else {
              ...
            res.getWriter().write("ok " + ...);
          }
    
      } catch (MyTooLargeException e) {
         res.getWriter().write("upload_size_exceeded"); // just an example string 
                                                        // (use your own)
    
         res.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
      }
    }
    

    Then, on the client, check for

    "upload_size_exceeded".equals(event.getResults()).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have servlet - @WebServlet(/servlet123) public class servlet123 extends HttpServlet { protected void doPost(HttpServletRequest
Im getting data from a server by executing this code: - (void) sendGeneral:(NSString *)
i want to send a file from client to server. My code: Client side
in ext.net library, how can I get from server side code the double clicked
I am using following code for loading images from server using following code.When i
I'm trying to dynamically create a book from code server-side. That part's working fine,
I am prepared code for download images from server and store in sdcard.For that
i have a code which download the apk from server by using Asynctask, Currently
in order to recover data from server I use XMLHttpRequest and my code is
I am getting a date from server side C# using the following code: DateTime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.