[英]Java - How set throws Exception in a Servlet?
我已經為servlet寫了代碼,在運行javascript代碼關閉窗口之前,我想從該servlet調用另一個類中的方法。
另一個方法是main()
和另一個稱為Teste()
,它們都是String
方法,我必須從中返回一些內容。 這個main()
方法在PC上的特定文件夾中創建一個.xlsm
文件,而Teste()
創建一個.jsp文件,指示main()
方法成功創建了該文件。
Teste()
和main()
方法包含throws Exception
。 問題是編譯器不允許Servlet拋出Exception
。
我已經搜索了一個。 Servlet不支持該拋出。 但是,我確實需要這樣做。 或者,如果有人知道一種方法,可以在main()
方法上運行此javascript代碼。
我也嘗試過使用WebDriver,但是我不希望代碼向我打開瀏覽器,我想手動訪問然后運行代碼。 而且WebDriver不支持附加網址
我如何達到目標?
我的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];
}
}
您可以環繞處理異常的方法,以使代碼正常工作,並使依賴於拋出的異常的代碼正常工作。
例:
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>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.