cihm / javaGuidelines

javaGuidelines
0 stars 1 forks source link

Tomcat eclipse java Web service #newbie #2

Open cihm opened 9 years ago

cihm commented 9 years ago

基本流程

  1. Servlet網站的Project類型是Dynamic Web Project。
  2. 可以在Web.xml裡面設定Servlet對應路徑等,這邊勾選的"Generate web.xml deployment descriptor"指的就是那個xml(也叫做Deployment Descriptor)
  3. 增加一個Servlet,對新建的project點右鍵選New->Servlet:
  4. 預設URL Mapping是對應到和這個Servlet名稱一樣(這個例子是HelloWorld),可以修改Pattern
  5. 設定Target Runtime 如果沒有設定Target Runtime這個時候Servlet沒有辦法Build。因為會需要Servlet相關物件,因此需要加入對應的實作class。使用Tomcat,所以要先把他加入才可以。 先加一個Tomcat的Server。在View 「Servers」裡面選擇新增,並且選Tomcat 7(路徑同上一篇)
  6. 最後需要把那個Server的class加進去。對Project點右鍵「Properties」找到"Java Build Path"選擇"Add Library"選"Server Runtime"
  7. 增加get顯示頁面 回到「HelloWorld.java」,設定doGet()的內容:
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
            // TODO Auto-generated method stub      
            response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        String requestUri = request.getRequestURI();

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello world</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Hello World from frisrt servlet");

        out.println("<form action='" + requestUri + "' method='post'>");
        out.println("<input type='text' name='name' />");
        out.println("<input type='submit' value='submit' />");
        out.println("</form>");

        out.println("</body>");
        out.println("</html>");

        out.flush();
        out.close();

    }

增加post顯示頁面 這邊是把輸入的東西在顯示出來:

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
            // TODO Auto-generated method stub
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        String name = request.getParameter("name");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

               out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello world</title>");
        out.println("</head>");
        out.println("<body>");

        out.println("Hello: " + name);

        out.println("</body>");
        out.println("</html>");

        out.flush();
        out.close();
    }

把Server Start起來以後,可以再IE直接輸入: http://localhost/ServletSample/TestServlet

2

cihm commented 9 years ago

problem

[1] if tomcat use window7 64 bit , jdk too

[2] 砍掉tomcat7後 無法再新增一次

  1. Close Eclipse
  In {workspace-directory}/.metadata/.plugins/org.eclipse.core.runtime/.settings delete the following two files:
   *   org.eclipse.wst.server.core.prefs
   *   org.eclipse.jst.server.tomcat.core.prefs 
  2. Restart Eclipse