请教:关于 Applet ,servlet,JNI的问题
applet与servlet通信,让后在servlet中使用本地代码,即 JNI,出现错误,调试了好久,还是出错,请各位大侠指教!谢谢! 代码如下:
applet端:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
* Simple demonstration for an Applet <-> Servlet communication.
*/
public class RobotApplet extends Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();
/**
* Setup the GUI.
*/
public void init() {
// set new layout
setLayout(new GridBagLayout());
// add title
Label title = new Label( "监控界面 ", Label.CENTER);
title.setFont(new Font( "SansSerif ", Font.BOLD, 14));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
add(title, c);
// add input label, field and send button
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label( "距离: ", Label.RIGHT), c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(inputField, c);
Button sendButton = new Button( "Forward ");
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
add(sendButton, c);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSendData();
}
});
// add output label and non-editable field
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label( "反馈结果: ", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 50.0;
add(outputField, c);
outputField.setEditable(false);
// add exception label and non-editable textarea
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label( "异常结果: ", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(exceptionArea, c);
exceptionArea.setEditable(false);
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Connection zum Servlet 鰂fnen
URL urlServlet = new URL(getCodeBase(), "echo ");
URLConnection con = urlServlet.openConnection();
// konfigurieren
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type ",
"application/x-java-serialized-object ");
// und zur點kliefern
return con;
}
/**
* Send the inputField data to the servlet and show the result in the outputField.
*/
private void onSendData() {
try {
// get input data for sending
String input = inputField.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
outputField.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
exceptionArea.setText(ex.toString());
}
}
}
servlet 端:
import java.io.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.lang.Math;
public class KongZhiServlet extends HttpServlet
{
static{
System.loadLibrary( "control ");
}
public native boolean SKConnect();
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
try
{
response.setContentType( "application/x-java-serialized-object ");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
boolConnect = this.SKConnect();
if (boolConnect)
{
oos.writeObject( "nihao ");
}
oos.flush();
oos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这段代码的本意就是由applet发送一个简单的字符给 servlet,然后再servlet中调用本地的函数实现一些功能,然后再返回数据之类的给 applet.
如果不加上 本地代码即 static{
System.loadLibrary( "control ");
}
这段,applet 和 servlet之间是可以互相通信的,加上后就会出现异常,
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8088/echo/echo
另外我用的是 Tomcat ,将本地代码生成的 .Dll 文件放在了
C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\echo\WEB-INF\classes 下面
请各位指教,谢谢!!!
[解决办法]
怎么没人
[解决办法]
再看看你的配置文件吧。
你还真敢想,把对象写到客户端。
[解决办法]
把你的dll设置到环境变量
[解决办法]
up
[解决办法]
.Dll 文件,摆放的路径应该在path中设置,或者直接放system32 文件目录下就可以;