




已阅读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届广东汕尾甲子镇瀛江学校八年级数学第二学期期末联考模拟试题含解析
- 血液臭氧治疗
- 重症护理核心理念与实务
- 手写护理文书标准化管理
- 高一新生住宿管理规范与实施策略
- 与法律有关的职业考试题及答案
- 经典诵读活动总结模版
- 2025年厨师职业技能鉴定高级试卷:餐饮企业品牌建设与推广
- 智能垃圾分类箱项目投资商业计划书范本(投资融资分析)
- 五年级数学下试卷及答案
- 2025至2030中国胸腺法新行业深度调查及投资前景研究报告
- 2025年高考政治答题模板:选必修123主观题答题语言总结
- 区块链在特种设备数据共享交换模型中的研究
- 辽宁省沈阳市沈北新区2024-2025学年初三下学期质量调研考试(一模)语文试题含解析
- 2025年九年级中考数学三轮冲刺训练一次函数中面积相关问题训练
- 钻探高级工试题及答案
- 《明朝的边疆政策》课件
- 湖北省武汉市2025届高中毕业生四月调研考试生物试题及答案(武汉四调)
评论
0/150
提交评论