Matlab与Java集成用于web部署的jsp问题
我对Matlab和MatlabBuilder是个新手。我试图通过调用Matlab函数来创建一个简单的javaweb。所以,我有一个模型。m文件。我已经将函数作为库放入服务器中运行,并有一些变量供用户输入,它们是“时间”(例如20)和“剂量”(例如120)。我尝试集成所有代码,并包括Matlab库。但不幸的是,只有这一点。可以运行html文件。下面是我得到的代码和错误。希望有人能帮我检查一下我是否正在编译模型。m文件正确,Java文件编码也正确。请参考,我正在使用Eclipse开普勒和Tomcat 7.0.54。提前谢谢
首先,这是我在运行应用程序时遇到的错误
已编辑 HTTP状态500-
java.lang.NullPointerException
com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23)
com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:833)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:731)
com.sun.proxy.$Proxy11.mclFeval(Unknown Source)
com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:406)
runPKmodelV1.Function.runPKmodelV1(Function.java:217)
SecondServlet.doGet(SecondServlet.java:78)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
这就是模型。m文件,该文件具有所需的所有功能,并带有输出图形
MATLAB模型。m
function w = model(tf_sim,y0)
%%--------------------------------------------------------------------
% Running ODE model - Initial Conditions and setting up ode solver
%---------------------------------------------------------------------
%Initial Conditions:
%Initial Conditions:
% Dose_Central = 1;
% Drug_Central = 0;
% Dose_Peripheral = 0;
% Drug_Peripheral = 0;
%tf_sim=10; %Simulation time
% y0(1) = 1; % Dose in Central Compartment
% y0(2) = 0; % Drug in Central Compartment
% y0(3) = 0; % Dose in Peripheral Compartment
% y0(4) = 0; % Drug in Peripheral Compartment
options = odeset('RelTol',1e-4,'AbsTol',1e-5);
%%% Solving ODEs, final time = 2e+9 sec (aprox. 63 years) %%%
[t,y]=ode15s(@PK_Model_v1,[0 tf_sim],y0,options);
%%% Plotting state variables %%%
f = figure;
subplot(2,2,1);
plot(t,y(:,1),'r-');
subplot(2,2,2);
plot(t,y(:,2),'b-');
subplot(2,2,3);
plot(t,y(:,3),'m-');
subplot(2,2,4);
plot(t,y(:,4),'k-');
w = webfigure(f);
close(f);
end
function dy = PK_Model_v1(time,y)
%Parameter Values:
Tk0_Central = 1;
TLag_Central = 1;
Km_Central = 1;
Vm_Central = 30;
Tk0_Peripheral = 1;
TLag_Peripheral = 1;
Q12 = 1;
k12 = 1;
k21 = 1;
Central = 1;
Peripheral = 1;
ka_Central = 0.5;
ka_Peripheral = 0.1;
%Fluxes:
ReactionFlux1 = ka_Central*y(1);
ReactionFlux2 = Vm_Central*y(2)/(Km_Central+y(2));
ReactionFlux3 = ka_Peripheral*y(3);
ReactionFlux4 = (k12*y(2))*Central-(k21*y(4))*Peripheral;
dy(1,1) = -ReactionFlux1;
dy(2,1) = 1/Central*(ReactionFlux1 - ReactionFlux2 - ReactionFlux4);
dy(3,1) = -ReactionFlux3;
dy(4,1) = 1/Peripheral*(ReactionFlux3 + ReactionFlux4);
end
这是欢迎页面,所有变量都由用户输入
Eclipse页面。html
<form action="SecondServlet">
<p> </p>
<p>PK Model Example</p>
<p> </p>
<p>Time</p>
<input type="text" name="tf_sim" value="" />
<p>Dosage</p>
<input type="text" name="y0" value="" />
<!-- Submit -->
<input type="submit" value="Display" name="DoPLot" />
<p> </p>
</form>
索引中没有任何内容。jsp页面。只有“从页面提交”按钮后的webfigure。单击html
日食指数。jsp
<div ALIGN="CENTER">
<wf:web-figure root="WebFigures" name="Project_Figure" scope="session" />
</div>
这些代码用于获取用户输入的数据,最后使用Matlab库计算结果,并将结果发送到索引中。jsp
已编辑 eclipsesecondservlet。java
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Object[] param = new MWArray[2];
Object[] result = null;
param[0] = new MWNumericArray(Integer.parseInt(request
.getParameter("tf_sim")), MWClassID.DOUBLE);
String[] str_elems = request.getParameter("y0").split("\\s+");
int[] numbers = new int[str_elems.length];
for (int i = 0; i < str_elems.length; i++) {
numbers[i] = Integer.parseInt(str_elems[i]);
}
try {
result = pkModel.model(1, param);
WebFigure webFig = (WebFigure) ((MWJavaObjectRef) result[0]).get();
// Set the figure scope to session
request.getSession().setAttribute("Project_Figure", webFig);
// Bind the figure's lifetime to session
request.getSession().setAttribute("Project_Figure_Binder",
new MWHttpSessionBinder(webFig));
updateSession(request.getSession(), result);
RequestDispatcher dispatcher = request
.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
} catch (MWException e) {
e.printStackTrace();
} finally {
MWArray.disposeArray(result);
}
}
public void updateSession(HttpSession session, Object[] param) {
int outputCount = param.length - 1;
session.setAttribute("numOutputs", outputCount);
}
真希望有人能帮忙。谢谢
# 1 楼答案
请查看错误消息:
第一行说明问题是数字格式,第三行告诉我们在使用
parseInt
方法时遇到问题,最后一行告诉我们问题在doGet
方法中快速查看doGet方法,我们可以看到:
现在,根据您之前对数据格式的评论判断
y0
应该是一个由四个数字组成的数组。问题是Interger.parseInt
用于解析单个数字,而不是它们的数组要解决这个问题,您需要添加一个单独的步骤,将输入拆分为单个数字,并一次解析一个数字。大概是这样的:
或者类似的东西,修改为考虑MWNumericArray的输入首选项,我找不到相关文档