gpt4 book ai didi

java - servlet 与 J2me 项目通信?

转载 作者:行者123 更新时间:2023-11-29 09:24:56 24 4
gpt4 key购买 nike

我正在使用 Eclipse 开发一个 J2ME 应用程序。在这个应用程序中,我使用了一个名为 HitServlet 的 servlet 和一个 J2me 类 HitMIDlet。我想使用 Eclipse 运行这个项目。但我不知道目录结构是什么以及我如何制作目录结构。我已经在我的 eclipse 中配置了 J2ME 插件和 Tomcat。但是我不知道我的 eclipse 中我的类的目录结构是什么。得到适当的输出。我想当我运行 j2me 类(HitMIDlet)时它命中 servlet(HitServlet)并发出放。

这是我的 J2me 类代码:

import java.io.*;

import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HitMIDlet
extends MIDlet
implements CommandListener {
private Display mDisplay;
private Form mMainForm;
private StringItem mMessageItem;
private Command mExitCommand, mConnectCommand;

public HitMIDlet() {
mMainForm = new Form("HitMIDlet");
mMessageItem = new StringItem(null, "");
mExitCommand = new Command("Exit", Command.EXIT, 0);
mConnectCommand = new Command("Connect",
Command.SCREEN, 0);
mMainForm.append(mMessageItem);
mMainForm.addCommand(mExitCommand);
mMainForm.addCommand(mConnectCommand);
mMainForm.setCommandListener(this);
}

public void startApp() {
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mMainForm);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if (c == mExitCommand)
notifyDestroyed();
else if (c == mConnectCommand) {
Form waitForm = new Form("Waiting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread() {
public void run() {
connect();
}
};
t.start();
}
}

private void connect() {
HttpConnection hc = null;
InputStream in = null;
String url = getAppProperty("HitMIDlet.URL");

try {
hc = (HttpConnection)Connector.open(url);
in = hc.openInputStream();

int contentLength = (int)hc.getLength();
byte[] raw = new byte[contentLength];
int length = in.read(raw);

in.close();
hc.close();

// Show the response to the user.
String s = new String(raw, 0, length);
mMessageItem.setText(s);
}
catch (IOException ioe) {
mMessageItem.setText(ioe.toString());
}
mDisplay.setCurrent(mMainForm);
}
}

和 servlet (HitServlet)

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class HitServlet extends HttpServlet {
private int mCount;

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String message = "Hits: " + ++mCount;

response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
}
}

最佳答案

让Eclipse生成目录结构即可。创建两个项目。一个用于 J2ME 作为 JavaME> MIDlet 项目(假设您已经正确安装了 Mobile Tools 插件),另一个用于 JavaEE 作为 Web > 动态 Web 项目(假设您正在使用 Eclipse for Java EE 和/或单独安装了 Web Tools 插件)。在手机客户端运行J2ME项目,在Tomcat服务器端运行JavaEE项目。

另见:

关于java - servlet 与 J2me 项目通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3308541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com