最近看了寫http協議的學習資料,因此想要實現下andriod平台和服務器通信,那就servlet吧,哎喲,還不錯哦!順便說下,我這個servlet服務是想獲得用戶名和密碼進行校驗,然后反給客戶端狀態碼;數據庫啥的沒來得及做,周末時間還是短了點......
Eclipse的java環境搭建好了
1.下載tomcat,解壓版的,這里我用的是以前下載的apache-tomcat-6.0.35,解壓到任意盤=>C:\apache-tomcat-6.0.35;
2.下載tomcat插件,解壓到Eclipse插件目錄=>H:\eclipse\plugins目錄,啟動Eclipse就可以看見小貓了;
3.Eclipse下的Window->Preferences->Tomcat設置下版本和tomcat主目錄,版本別搞錯了!省的麻煩...
完成,其實就是這么簡單,點擊下“啟動小貓”就可以啟動了,只要沒顯示什么異常,沒顯示空指針的log;而是顯示了啟動時間就ok了;
4.新建一個tomcat工程,我的是TaxiServlet;
5.在 TaxiServlet/WEB-INF/src下新建一個包,我的是com.servlet.login;
6.在包下新建類,我的是LoginAction.java,內容稍后說;
7.在WEB-INF下新建web.xml文件;
8.首先LoginAction.java內容可以如下:
9.web.xml內容:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>login</servlet-name> <!-- 名字隨便 --> <servlet-class>com.servlet.login.LoginAction</servlet-class> <!-- servlet類名--> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> <!-- url訪問虛擬路徑,最后我們就是通過工程名/login進行訪問的,像這樣http://127.0.0.1:8000/LoginAction/login--> </servlet-mapping> </web-app>
這里我客戶端的用戶名和密碼是通過post請求的,因此doPost方法會被調用;doGet方法可以測試下網頁,用http://127.0.0.1:port/servlet工程名稱/url-pattern虛擬路徑 進行測試;
我的工程:
package com.servlet.login; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; /** * 登錄模塊校驗Servlet * @author Administrator * */ public class LoginAction extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public LoginAction() { super(); } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); res.setCharacterEncoding("utf-8"); PrintWriter out = res.getWriter(); out.println("Hello, Brave new World!"); out.close(); } protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { if (null == req) { return; } res.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); res.setCharacterEncoding("utf-8"); PrintWriter out = res.getWriter(); String username = req.getParameter("user_name"); String password = req.getParameter("password"); if (username.equals("admin")) { if (password.equals("123")) { out.println("0"); ///< 正確 } else { out.println("2"); ///< 密碼錯誤 } } else { out.println("1"); ///< 用戶名錯誤 } out.flush(); out.close(); } }
測試以下:http://127.0.0.1:8000/TaxiServlet/login
web.xml的詳細配置大家可以網上搜搜,我也是經過測試才了解怎么回事的,很多網絡並沒有說明到底怎么回事?因此對於我這樣的初學者總是很疑惑的,相信只有了解了本質才知道,也才能寫出好的code...也進步最快,,,,,,
我的doPost方法將用於andriod應用程序登錄時校驗,數據庫校驗慢慢完善...先這樣
感謝網友的分享,通過你們我學到了很多東西.... 網友http://www.linuxidc.com/Linux/2012-06/63935.htm,其它就不舉例了,嘿嘿...
對了還要說下tomcat的配置,不然可能會出現404錯誤;C:\apache-tomcat-6.0.35\conf\tomcat-users.xml文件一般來說默認用戶配置都是注釋着的,你需要配置至少一個用戶,我的如下:
點擊(此處)折疊或打開
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <tomcat-users> <!-- NOTE: By default, no user is included in the "manager-gui" role required to operate the "/manager/html" web application. If you wish to use this app, you must define such a user - the username and password are arbitrary. --> <!-- NOTE: The sample user and role entries below are wrapped in a comment and thus are ignored when reading this file. Do not forget to remove <!.. ..> that surrounds them. --> <!-- <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <user username="admin" password="admin" roles="manager"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="role1" password="tomcat" roles="role1"/> --> <role rolename="manager"/> <user username="admin" password="admin" roles="manager"/> </tomcat-users>
其它的根據需要來吧....
配置端口號:
C:\apache-tomcat-6.0.35\conf\server.xml用來配置端口號,如果你電腦安裝了n多軟件,可能默認的8080端口無法使用,記得進去找到它,改改哈!多練習練習就熟了,,,編程也是寫字..
Eclipse下tomcat不能指定發布ServerLocation的問題
在Eclipse下建立了Web應用,但發布的時候卻發現,只能發布到eclipse的目錄下。而且雙擊服務器,發現“Server Locations中選項都是灰色”,不讓指定自己希望的發布路徑。
解決方法是:首先刪除當前Eclipse下建立的所有Server,並且將服務器與應用的關聯也要一並徹底刪除。然后重新添加一個外部服務器。先不要將應用與該Server綁定,雙擊該服務器,進入屬性編輯器中,這時會發現“Server Locations”中選項都是可選的了,可以選擇一個你希望發布到的路徑。如我希望將項目發布到我本地自行安裝的tomcat下。即可