我在Mandriva Linux上实现了托管在Tomcat 6服务器上的Servlet。我已经能够让客户端与Servlet进行通信。响应请求,Servlet尝试实例化位于同一目录中的另一个类(名为KalmanFilter)。 KalmanFilter尝试创建四个矩阵(使用Jama Matrix包)。但此时Servlet停止而没有任何例外!
然而,从同一目录中的另一个测试代码,我已经能够创建KalmanFilter类的实例,并且没有任何错误地继续。只有当我的Servlet尝试实例化KalmanFilter类并创建矩阵时才会出现问题。任何的想法?
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
PrintWriter out = null; //response.getWriter();
try{
System.out.println("creating new KalmanFilter");
KalmanFilter filter = new KalmanFilter();
out = response.getWriter();
out.print("filter created");
}catch(Exception ex){
ex.printStackTrace();
System.out.println("Exception in doGet(): " + ex.getMessage());
ex.printStackTrace(out);
}
}
}
import Jama.Matrix;
public class KalmanFilter {
protected Matrix X, X0;
protected Matrix F, Q;
//protected Matrix F, B, U, Q;
protected Matrix H, R;
protected Matrix P, P0;
private final double EPSILON = 0.001;
public KalmanFilter(){
System.out.println("from constructor of KalmanFilter");
createInitialMatrices();
}
private void createInitialMatrices(){
System.out.println("from KalmanFilter.createInitialMatrices()");
double[][] pVals = {
{1.0, 0.0},
{0.0, 1.0}
};
double[][] qVals = {
{EPSILON, EPSILON},
{EPSILON, EPSILON}
};
double[][] hVals = {
{1.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{0.0, 1.0}
};
double[][] xVals = {
{0.0},
{0.0},
};
System.out.println("creating P Q H X matrices in createInitialMatrices()");
try{
this.P = new Matrix(pVals);
System.out.println("created P matrix in createInitialMatrices()");
this.Q = new Matrix(qVals);
System.out.println("created Q matrix in createInitialMatrices()");
this.H = new Matrix(hVals);
System.out.println("created H matrix in createInitialMatrices()");
this.X = new Matrix(xVals);
System.out.println("created X matrix in createInitialMatrices()");
System.out.println("created P Q H X matrices in createInitialMatrices()");
}catch(Exception e){
System.out.println("Exception from createInitialMatrices()"+ e.getMessage());
e.printStackTrace();
}
System.out.println("returning from createInitialMatrices()");
}
}
答案 0 :(得分:1)
如果从servlet中删除try / catch块,将使您更容易诊断。此代码抛出的唯一已检查异常是IOException
,而doGet
允许您在不自行捕获的情况下抛出异常。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
System.out.println("creating new KalmanFilter");
KalmanFilter filter = new KalmanFilter();
out = response.getWriter();
out.print("filter created");
}
这样,异常将显示在浏览器上,而不是消失在日志文件中。当然,这对用户来说并不是很好,但对于您的开发,它会让生活更轻松。