Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信_第1頁
Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信_第2頁
Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信_第3頁
Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信_第4頁
Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Tomcat 上如何部署Servlet及Android中如何與服務(wù)器通信下載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代碼:創(chuàng)建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 類,需要導(dǎo)入jar包,HttpServlet可以從TomCatlibservlet-api.jar,JSONArray ,JSONObject 需要導(dǎo)入json-lib-1.1-jdk13.jar包,eclipse中可以在項目名稱上右鍵選擇properties,現(xiàn)在java build path,然后選擇Libraries,點擊Add External JARs,選擇jar包代碼編寫好后,在項目下面bin中會生成對應(yīng)的class文件:這些class文件最后都是要部署到Tomcat中去的,包括包名文件夾,比如:com/webservice/test這些目錄都是要拷

12、貝進去的。Tomcat中配置:在Tomcat的webapp中新建工程文件夾,命名為“server-test”,然后在server-test中新建文件夾“WEB-INF”,在“WEB-INF”文件夾中新建文件夾“classes”,用于存放工程的class文件。在在“WEB-INF”文件夾中新建文件web.xml,內(nèi)容如下: 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如果出現(xiàn)如下問題:出現(xiàn)這樣的問題,需要再環(huán)境變量中導(dǎo)入jar包,在我做實驗的過程中導(dǎo)入了如下幾個包: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包可以從網(wǎng)上下載,servlet-api.jar可以從Tomcat中l(wèi)ib文件夾中得到:配置好后再瀏覽器中輸入:http:/localhost:8080/server-test/test,就好出現(xiàn)如下界面:下載文件,用UltraEdit 打開,可以看到如下內(nèi)容: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中客戶端與服務(wù)端通信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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論