




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Tomcat 上如何部署Servlet及Android中如何与服务器通信下载Tomcat并安装 Apache Tomcat powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations. Some of these users and their stories are listed on the PoweredBy wiki page.Apache Tomcat, Tomcat, Apache, the Apache feather, and the Apache Tomcat project logo are trademarks of the Apache Software Foundation. I add this English description due to junk baidu wenkus rulesEclipse中编写Servlet代码:创建java工程package com.webservice.test;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class WebserviceTest extends HttpServlet private static final long serialVersionUID = 512;private List list;protected void doPost(HttpServletRequest req,HttpServletResponse response) throws ServletException,IOExceptiondoGet(req, response);protected void doGet(HttpServletRequest req,HttpServletResponse response) throws ServletException,IOExceptionresponse.setContentType(test/plain);response.setCharacterEncoding(UTF-8);PrintWriter outPrintWriter = response.getWriter();JSONArray jsonArray = new JSONArray();list = initData();for(Student bean:list) JSONObject jsonObject = new JSONObject();tryjsonObject.put(stunum, bean.getStunum();jsonObject.put(name,bean.getName();jsonObject.put(chinese, bean.getChinese();jsonObject.put(math, bean.getMath();jsonObject.put(english, bean.getEnglish();catch (Exception e) e.printStackTrace();jsonArray.put(jsonObject);outPrintWriter.write(jsonArray.toString();outPrintWriter.flush();outPrintWriter.close();private List initData()List studentlist= new ArrayList();Student stuOne = new Student();stuOne.setStunum(10001);stuOne.setName(sloden);stuOne.setChinese(82);stuOne.setMath(88);stuOne.setEnglish(80);studentlist.add(stuOne);Student stuTwo = new Student();stuTwo.setStunum(10002);stuTwo.setName(candy);stuTwo.setChinese(81);stuTwo.setMath(87);stuTwo.setEnglish(80);studentlist.add(stuTwo);Student stuThree = new Student();stuThree.setStunum(10003);stuThree.setName(tenny);stuThree.setChinese(89);stuThree.setMath(85);stuThree.setEnglish(89);studentlist.add(stuThree);return studentlist;/*public static void main(String args)*/class Studentprivate int stunum;private String name;private int chinese;private int math;private int english;public int getStunum() return stunum;public void setStunum(int stunum) this.stunum = stunum;public String getName() return name;public void setName(String name) = name;public int getChinese() return chinese;public void setChinese(int chinese) this.chinese = chinese;public int getMath() return math;public void setMath(int math) this.math = math;public int getEnglish() return english;public void setEnglish(int english) this.english = english;代码中用到了HttpServlet ,JSONArray ,JSONObject 类,需要导入jar包,HttpServlet可以从TomCatlibservlet-api.jar,JSONArray ,JSONObject 需要导入json-lib-1.1-jdk13.jar包,eclipse中可以在项目名称上右键选择properties,现在java build path,然后选择Libraries,点击Add External JARs,选择jar包代码编写好后,在项目下面bin中会生成对应的class文件:这些class文件最后都是要部署到Tomcat中去的,包括包名文件夹,比如:com/webservice/test这些目录都是要拷贝进去的。Tomcat中配置:在Tomcat的webapp中新建工程文件夹,命名为“server-test”,然后在server-test中新建文件夹“WEB-INF”,在“WEB-INF”文件夹中新建文件夹“classes”,用于存放工程的class文件。在在“WEB-INF”文件夹中新建文件web.xml,内容如下: Tomcat Manager Application A scriptable management web application for the Tomcat Web Server; Manager lets you view, load/unload/etc particular web applications. test com.webservice.test.WebserviceTest test /test 将编译生成的class 文件放到 项目中的WEB-INF目录中的classes目录下,启动Tomcat:然后在浏览器中输入:http:/localhost:8080/server-test/test如果出现如下问题:出现这样的问题,需要再环境变量中导入jar包,在我做实验的过程中导入了如下几个包:commons-beanutils.jar、commons-logging.jar、ezmorph-1.0.2.jar、commons-lang-2.1.jar、json-lib-1.1-jdk13.jar、servlet-api.jar,其中除了servlet-api.jar包外,其他jar包可以从网上下载,servlet-api.jar可以从Tomcat中lib文件夹中得到:配置好后再浏览器中输入:http:/localhost:8080/server-test/test,就好出现如下界面:下载文件,用UltraEdit 打开,可以看到如下内容:math:88,stunum:10001,name:sloden,english:80,chinese:82,math:87,stunum:10002,name:candy,english:80,chinese:81,math:85,stunum:10003,name:tenny,english:89,chinese:89这个时候Tomcat中的项目就部署好了!Android中客户端与服务端通信Android代码如下:package jiao.jiao;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.json.JSONArray;import org.json.JSONObject;import android.app.Activity;import .ConnectivityManager;import .NetworkInfo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class WebClientForAndroidActivity extends Activity /* Called when the activity is first created. */public EditText showEditText,queryEditText;public Button getButton;public static final String TAG = client;public static final int CONNECTMESSAGE_SUCCESS = 10051;public static final int CONNECTMESSAGE_ERROR = 10052;public static final int CONNECTMESSAGE_TIMEOUT = 10053;public StringBuilder stringBuilder;public Handler handler;public static final String httpUrl = 1:8080/server-test/test; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); handler = new Handler()Overridepublic void handleMessage(Message msg) / TODO Auto-generated method stub/super.handleMessage(msg);if (msg.arg1 = CONNECTMESSAGE_SUCCESS) if (stringBuilder!=null) try String jsonString = stringBuilder.toString();StringBuffer stringBuffer = new StringBuffer();/JSONArray jsonArray = new JSONArray(jsonString);JSONArray jsonArray = new JSONArray(jsonString);/jsonArray.put(jsonString.toString();/jsonArray.put(jsonString);for (int i = 0; i jsonArray.length(); i+) JSONObject jsonObject = jsonArray.getJSONObject(i);stringBuffer.append(-the +i+ student info-);stringBuffer.append(stunum:).append(jsonObject.getInt(stunum).append(n);stringBuffer.append(name:).append(jsonObject.getString(name).append(n);stringBuffer.append(math:).append(jsonObject.getInt(math).append(n);stringBuffer.append(english:).append(jsonObject.getInt(english).append(n);stringBuffer.append(chinese:).append(jsonObject.getInt(chinese).append(n);showEditText.setText(stringBuffer.toString(); catch (Exception e) e.printStackTrace();if (msg.arg1 = CONNECTMESSAGE_ERROR) showEditText.setText(can not get info for service); ; queryEditText = (EditText) this.findViewById(R.id.stunum); showEditText = (EditText) this.findViewById(R.); getButton = (Button) this.findViewById(R.id.getinfo); getButton.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) / TODO Auto-generated method stubnew Thread()Overridepublic void run() / TODO Auto-generated method stubconnectService();.start();); if (checkNetWork() Log.d(TAG, -network is ok);else getButton.setEnabled(false); public boolean checkNetWork() ConnectivityManager cm = (ConnectivityManager) this .getSystemService(this.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null & info.isConnected() return true; else Log.d(TAG, Its cant connect the Internet!); return false; public void connectService() HttpPost request = new HttpPost(httpUrl); HttpClient httpClient = new DefaultHttpClient(); List params = new ArrayList(); params.add(new BasicNameValuePair(stunum,queryEditText.getText().toString(); HttpResponse response; try HttpEntity entity = new UrlEncodedFormEntity(params, UTF-8); request.setEntity(entity); response = httpClient.execute(request); int status = response.getStatusLine()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 预防疟疾主题班会课件
- 化学工业企业安全质量环保标准化培训体系
- 项目集成管理工程师课件
- 项目介绍框架课件
- 音乐里的中国年课件
- 门诊护理培训课件
- 汽车配套产业基地项目环境影响报告书(范文)
- 城市污水管网建设工程招投标方案(参考模板)
- 2025年煤矿钻孔监测系统合作协议书
- 高效节能电机项目申请报告
- 2024年宜宾市叙州区区内外选调在编在职教师笔试真题
- 2025年广东省中考英语试题(附答案)
- 2024年广东省烟草专卖局系统招聘考试真题及答案
- 社区网格员(综合治理)笔试试题及答案
- 餐饮革新与市场机遇
- 交通运输行政执法课件培训
- 中国肉类加工设备行业发展趋势及发展前景研究报告2025-2028版
- 2025年新疆中考数学试卷真题(含答案解析)
- 高考数学专题-基本不等式求最值的常用方法(解析版)
- 中国上海市酒店行业市场调查研究及投资前景预测报告
- 2025年广西专业技术人员继续教育公需科目(三)答案
评论
0/150
提交评论