java - GWT 捕获 HttpServlet 抛出的异常

标签 java forms gwt servlets exception

从服务器代码(在 HttpServlet 中),如果文件太大,我将抛出异常:

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

从客户端代码中,我缺少成功捕获此代码段的异常:

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

但是,从另一个客户端代码片段中,我以某种方式检测到何时抛出异常,并使用我根本不信任的丑陋解决方法:

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
       ...
    }

Eclipse 中的“开发模式” View 报告“未捕获的异常”类型的错误,这表明我在检测它方面确实做得很糟糕。

谁能告诉我如何正确捕获异常,以及我使用的解决方法是否有意义?

谢谢!

最佳答案

你的第一次尝试

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

不起作用,因为 submit() 不会等到浏览器收到响应(这是一个异步调用)。

uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

  @Override
  public void onSubmitComplete(SubmitCompleteEvent event) {
    ...

这里你实际上收到了服务器的响应。但这是表单提交,而不是 GWT-RPC 调用,因此结果只是纯文本,而不是 GWT Java 对象。

当您在 Servlet 中抛出 RuntimeException 时,服务器将简单地发送带有错误代码的响应(可能是“500”,但最好使用 Firebug 或 Chrome 开发者工具中的“网络”选项卡来查看实际响应和响应代码。)因此,在成功的情况下,您将获得 URL,否则响应为空。

可能的解决方案

您可以在服务器端捕获异常,并显式发送更好的描述:

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

然后,在客户端上检查

"upload_size_exceeded".equals(event.getResults()).

关于java - GWT 捕获 HttpServlet 抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12977306/

相关文章:

gwt - 如何用 safeHtml 值填充 UiRenderer?

java - 在为mifos源代码构建war文件的过程中无法修复liqibase maven插件更新sql?

java - Spring中不带@Configuration导入配置类

ruby-on-rails - rails 形式的隐藏字段

html - 这个 HTML 输入表单安全吗?

apache - GWT-RPC、Apache、Tomcat 服务器数据大小检查

java - Ruby 字符串到日期时间格式

java - 基于用户组的可变文件存储路径

css - 超链接按钮

gwt - GWT 中引发 NoSuchMethod 异常