Tomcat 上如何部署Servlet及Android中如何与服务器通信_第1页
Tomcat 上如何部署Servlet及Android中如何与服务器通信_第2页
Tomcat 上如何部署Servlet及Android中如何与服务器通信_第3页
Tomcat 上如何部署Servlet及Android中如何与服务器通信_第4页
Tomcat 上如何部署Servlet及Android中如何与服务器通信_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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 f

2、eather, 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 ja

3、va.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 stati

4、c 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,IOExceptionrespon

5、se.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,

6、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 Lis

7、t 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.setCh

8、inese(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(St

9、ring 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;p

10、ublic 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 ,JSONAr

11、ray ,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这些目录都是要拷

12、贝进去的。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 parti

13、cular 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

14、.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,

15、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.

16、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

17、.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 andro

18、id.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 = clien

19、t;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;

20、 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 (str

21、ingBuilder!=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 jsonArr

22、ay.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.

23、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) showEd

24、itText.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

25、 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 .

26、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 =

27、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().g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论