TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1.下面的Servlet程序读取手机程序发送过来的两个参数信息,一个是用户账号,另一个是密码,然后将这两个信息返回到手机程序。
package com.Test; import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
-
- public class HelloWorld extends HttpServlet {
- protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
- // Read the parameters sent from MIDlet
- String acct = req.getParameter("account"), pwd = req
- .getParameter("password");
- if (acct == null || pwd == null) {
- res.sendError(HttpServletResponse.SC_BAD_REQUEST,
- "Unable to read parameters");
- return;
- }
- res.setContentType("text/plain");
- PrintWriter out = res.getWriter();
- out.print(this.getServletInfo() + "
- ");
- out.print("acct:" + acct + "
- pwd:" + pwd);
- out.close();
- }
- protected void doPost(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- doGet(req, res);
- }
- /*--------------------------------------------------
- * Information about servlet
- *-------------------------------------------------*/
- public String getServletInfo() {
- return "Hello,World
";
- }
- }
- 2、下面的J2ME程序通过HTTP连接方式访问servlet,并发送GET的请求,然后将servlet程序的响应信息通过一个Form显示在手机屏幕上。
- /*
- * 使用Get方式访问Servlet
- */
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.io.*;
- import java.io.*;
- public class HttpGetExample extends MIDlet implements CommandListener {
- private Display display;
- private Form mainForm;
- private Command exitCommand;
- String account = "newuser";
- String password = "123456";
- public HttpGetExample() {
- display = Display.getDisplay(this);
- exitCommand = new Command("Exit", Command.EXIT, 1);
- mainForm = new Form("Data from servlet");
- mainForm.addCommand(exitCommand);
- mainForm.setCommandListener(this);
- }
-
- public void startApp() {
- display.setCurrent(mainForm);
- //访问服务器servlet
- try {
- callServlet();
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- private void callServlet() throws IOException {
- HttpConnection http = null;
- InputStream iStrm = null;
- // Data is passed at the end of url for GET
- String url = "http://127.0.0.1:8080/examples/hello" + "?" + "account="
- + account + "&" + "password=" + password;
- try {
- http = (HttpConnection) Connector.open(url);
- //使用HttpConnection.GET方式
- // 发送Get请求
- http.setRequestMethod(HttpConnection.GET);
- // 服务器响应
- if (http.getResponseCode() == HttpConnection.HTTP_OK) {
- iStrm = http.openInputStream();
- // 获得头信息,没有返回
- //获得数据信息
- int length = (int) http.getLength();
- if (length > 0) {
- byte servletData[] = new byte[length];
- iStrm.read(servletData);
- //显示返回信息
- mainForm.append("验证通过:
- "
- + new String(servletData));
- } else
- mainForm.append("不能访问数据!");
- }
- } catch (Exception e) {
- mainForm.append("网络出错");
- System.out.println(e.toString());
- } finally {
- //关闭连接对象
- if (iStrm != null)
- iStrm.close();
- if (http != null)
- http.close();
- }
- }
- public void commandAction(Command c, Displayable s) {
- if (c == exitCommand) {
- destroyApp(false);
- notifyDestroyed();
- }
- }
- }
复制代码
源码下载:http://203.93.208.26/kj/cwb/dir7/http1.zip |
|