自己動手搭建Tomcat下的Servlet,搭建tomcatservlet

來源:互聯網
上載者:User

自己動手搭建Tomcat下的Servlet,搭建tomcatservlet

我以一個簡單的登入執行個體,來介紹如何在tomcat下搭建一個servlet web程式

1.環境搭建

首先要保證JDK和tomcat安裝成功,並且配置好了環境變數。

通過java -version命令檢測JDK是否安轉並配置成功,如下則配置成功


接下來查看tomcat是否可以正常啟動

雙擊檔案下apache-tomcat-7.0.54\bin\startup.bat以啟動Tomcat伺服器


在瀏覽器中輸入:http://localhost:8080/



2.在tomcat安裝目錄下的webapps檔案夾下,建立專案檔夾test_servlet

並在此檔案夾中,建立WEB_INF檔案夾

進入WEB-INF目錄,建立classes和lib目錄以及web.xml 

3.編寫LoginServlet類

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request,                     HttpServletResponse response)              throws ServletException,                     IOException {//接收表單穿過來的參數值        String username = request.getParameter("username");           String password = request.getParameter("password");         //控制台列印輸出參數值以供查看        System.out.println("username=" + username);        System.out.println("password=" + password);        //設定編碼格式        response.setContentType("text/html;charset=GB18030");        //輸出瀏覽器資訊        response.getWriter().println("<html>");        response.getWriter().println("<head>");        response.getWriter().println("<title>登入資訊</title>");        response.getWriter().println("</head>");        response.getWriter().println("<body>");        response.getWriter().println("歡迎【" + username + "】使用者登入成功!!!");        response.getWriter().println("</body>");        response.getWriter().println("</html>");    }                   public void doPost(HttpServletRequest request,                     HttpServletResponse response)              throws ServletException,                     IOException {      doGet(request, response);               }                     }


編譯該類,並將產生的LoginServlet .class 檔案拷貝到WEB-INF目錄的classes檔案夾中

4.在web.xml中描述這個LoginServlet 類

<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true">  <servlet><servlet-name>example</servlet-name><servlet-class>LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>example</servlet-name><url-pattern>/loginServlet</url-pattern></servlet-mapping></web-app>

html設計

<html><head><title>登入</title></head><body><form action="loginServlet" method="post">使用者:<input type="text" name="username"><br>密碼:<input type="password" name="password"><br><input type="submit" value="登入"></form></body></html>

至此一個簡單的servlet的web程式就寫好了,下面運行一下

啟動tomcat伺服器,輸入以下URL:http://localhost:8080/test_servlet/login.html


點擊登入


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.