Android 本地tomcat服務器接收處理手機上傳的數據之環境搭建


 
本篇文章
 
環境:win7 + jdk1.7 + tomcat v8.0.53
 
工具:
1.Eclipse 
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
 
 
2. tomcat v8.0.53
 
知識點:java + servlet + tomcat
 
創建web工程方法
1. File --- new  --- Dynamic Web Project 
 
 

 

 

 

看圖說話:
a. 填寫web工程名:First
b. 點擊“New Runtime...“,”選擇“Apache Tomcat v8.0”(PS:由於Eclipse luna最大支持 Apache Tomcat v8.0,所以PC端最大只能安裝tomcat v8版本,不支持v9)
c. 點擊"Next"
 
然后如下:
 

 

 
a. 選擇tomcat 在本地的位置
b. 選擇"JRE"
c. 點擊finish
 
然后如下:
 

 

 
點擊“Next”
 
 

 

 
點擊“Next”
 
 

 

 
勾選“Generate web.xml ...”,這樣,可以自動生成web.xml文件,點擊“finish”按鈕
 
項目創建完成:
 
 

 

 
2. 創建servlet
 
 

 

 
 
點擊“Next”
 
 
 

 

點擊“Next”
 

 

 
 
直接點擊“finish”
 
 

 

 
修改 ServletDemo1.java文件,內容如下:
 
package com.servlet.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class ServletDemo1
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDemo1" })
public class ServletDemo1 extends HttpServlet {
       private static final long serialVersionUID = 1L;
       /**
        * The doGet method of the servlet. <br>
        *
        * This method is called when a form has its tag value method equals to get.
        *
        * @param request
        *            the request send by the client to the server
        * @param response
        *            the response send by the server to the client
        * @throws ServletException
        *             if an error occurred
        * @throws IOException
        *             if an error occurred
        */
       public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
             response.setContentType("text/html");
             PrintWriter out = response.getWriter();
             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
             out.println("<HTML>");
             out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
             out.println("  <BODY>");
             out.print("    This is ");
             out.print(this.getClass());
             out.println(", using the GET method");
             out.println("  </BODY>");
             out.println("</HTML>");
             out.flush();
             out.close();
       }
       /**
        * The doPost method of the servlet. <br>
        *
        * This method is called when a form has its tag value method equals to
        * post.
        *
        * @param request
        *            the request send by the client to the server
        * @param response
        *            the response send by the server to the client
        * @throws ServletException
        *             if an error occurred
        * @throws IOException
        *             if an error occurred
        */
       public void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
             response.setContentType("text/html");
             PrintWriter out = response.getWriter();
             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
             out.println("<HTML>");
             out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
             out.println("  <BODY>");
             out.print("    This is ");
             out.print(this.getClass());
             out.println(", using the POST method");
             out.println("  </BODY>");
             out.println("</HTML>");
             out.flush();
             out.close();
       }
}
 
3. 修改web.xml 文件為:
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
       <welcome-file-list>
             <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
        <servlet>
             <servlet-name>ServletDemo1</servlet-name>
             <servlet-class>com.servlet.demo.ServletDemo1</servlet-class>
       </servlet>
       <servlet-mapping>
             <servlet-name>ServletDemo1</servlet-name>
             <url-pattern>/test/ServletDemo1</url-pattern>
       </servlet-mapping>
</web-app>
 
備注:上面高亮部分的名稱要一致
 
 
 

 

 
4. 開始部署
 
 
 

 

 

 
直接點擊“finish”按鈕,如下(雖然出現Not Found,此時不要擔心,還沒開始運行servlet 呢,繼續往下看):
 
 

 

 
5. 運行servlet
 
選中“ServletDemo1”--- 右鍵 --- Run As --- Run on Server
 
 

 

 
 

 

 
點擊“finish”按鈕,運行效果如下,成功!
 
 

 


 
 
 
PS:
 
 
2.打印log方法:
 
方法一:
將 commons-logging-1.1.1.jar 拷貝到 libs文件夾:
 
 
private static Log log1 = LogFactory.getLog(ServletDemo1.class);
 
doGet方法內加入:log1.info("!!!!");
 
方法二:
System.out.println("xxx");
 
方法三:
getServletContext().log("xxx");
 
注意:加入log日志后,如果log不生效,clean下工程:

 

然后在瀏覽器輸入:http://localhost:8080/First/test/ServletDemo1,回車后,可以看到日志輸出

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM