Java - 如何在Servlet中设置抛出异常?

时间:2018-05-15 17:07:17

标签: java jsp servlets apache-poi

我已经编写了一个servlet代码,在运行javascript代码关闭窗口之前,我想从中调用另一个类中的方法。

另一种方法是main(),另一种方法是Teste(),两者都是String方法,我必须从中返回一些东西。这个main()方法在我的PC上的特定文件夹中创建.xlsm文件,Teste()创建一个.jsp文件,表明main()方法成功创建了文件。

Teste()main()方法包含throws Exception。问题是编译器不允许servlet抛出Exception

我已经搜索了一个。 Servlet不支持此抛出。但是,我真的需要这样做。或者,如果有人知道我可以在main()方法上运行此javascript代码。

我也尝试过使用WebDriver,但我不希望代码打开浏览器给我,我想手动访问,然后代码运行。 WebDriver不支持附加URL的

我如何实现目标?

我的Servlet代码:

package com.passaservletjava.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletJava extends HttpServlet {

    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
     RecebeServlet recebeservlet = new RecebeServlet();
     out.println("<html><head><script></script></head><body><h1>"+recebeservlet.Teste(request.getRequestURL().toString())+"</h1></body></html>");
     out.println("<html><head><script>window.close();</script></head><body></body></html>");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
        String param = request.getParameter("texto");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
        String param = request.getParameter("texto");
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

我的其他两种方法:

package com.passaservletjava.servlet;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.File;

public class RecebeServlet {

     public String Teste(String url) throws Exception{
         String[] args = new String[1];
         args[0] = url;
         return main(args);
     }

     public static String main(String[] args) throws Exception {
      Class.forName("com.mysql.jdbc.Driver");
      Connection connect = DriverManager.getConnection( 
         "jdbc:mysql://localhost:3306/database" , 
         "user" , 
         "password"
      );

      Statement statement = connect.createStatement();
      ResultSet resultSet = statement.executeQuery("select * from `table`");
      FileInputStream file = new FileInputStream(new File("C:\\Users\\PC\\Desktop\\Folder1.xlsm"));
      XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(file)); 
      XSSFSheet spreadsheet = wb.getSheet("Planilha1");

      XSSFRow row = spreadsheet.createRow(1);
      XSSFCell cell;
      int i = 1;

      while(resultSet.next()) {
         /*
         ...............
         */
        i++;
      }

      XSSFRow linhacontador = spreadsheet.getRow(1);
       if (linhacontador == null) {
        linhacontador = spreadsheet.createRow(1);
       }

      XSSFCell colunacontador = linhacontador.getCell(31);
       if (colunacontador == null) {
        colunacontador = linhacontador.createCell(31);
       }

       colunacontador.setCellType(XSSFCell.CELL_TYPE_FORMULA);
       colunacontador.setCellFormula("COUNTIF(AE:AE, \"<>\")");

      XSSFRow linhaformula = spreadsheet.getRow(1);
       if (linhaformula == null) {
        linhaformula = spreadsheet.createRow(1);
       }

      XSSFCell colunaformula = linhaformula.getCell(26);
       if (colunaformula == null) {
        colunaformula = linhaformula.createCell(26);
       }

      colunaformula.setCellType(XSSFCell.CELL_TYPE_STRING);
      colunaformula.setCellValue("0,0");

      XSSFCellStyle style = wb.createCellStyle();
      XSSFFont font = wb.createFont();
      style.setVerticalAlignment(VerticalAlignment.TOP);
      style.setAlignment(HorizontalAlignment.CENTER);
      font.setFontHeight((short)(72*20));
      style.setFont(font);
      colunaformula.setCellStyle(style);

      CellRangeAddress range = new CellRangeAddress(1, i-1, 26, 26);
      spreadsheet.addMergedRegion(range);

      FileOutputStream out = new FileOutputStream(new File("C:\\Users\\PC\\Desktop\\excelwithmacro.xlsm"));
      wb.write(out);
      out.close();
      connect.close();

      return args[0];
   }
}

1 个答案:

答案 0 :(得分:0)

您可以自行处理处理异常的方法,使代码正常工作并保留依赖抛出异常的代码。

示例:

    public String TesteHandleException(String url)
    {
     try {
     return Teste(url)
     } catch (Exception ex)
     {
      // Handle Exception
      return "Exception occured";
     }
    }

    public String Teste(String url) throws Exception
    {
     String[] args = new String[1];
     args[0] = url;
     return main(args);
    }

并更改通话:

out.println("<html><head><script></script></head><body><h1>"+recebeservlet.TesteHandleException(request.getRequestURL().toString())+"</h1></body></html>");