淺談servlet與jsp的關係,淺談servletjsp

來源:互聯網
上載者:User

淺談servlet與jsp的關係,淺談servletjsp

      servlet是用java語言編寫的,是一個java類。主要功能是用來接受、處理用戶端的請求,並把處理結果返回到用戶端顯示。Jsp是servlet發展後期的產物。在沒有jsp之前,servlet利用輸出資料流動態產生整個HTML頁面,輸出內容包括每一個HTML標籤和每個在HTML頁面中出現的內容。HTML檔案包含大量標籤和大量靜態文本及格式等,以及所有的表現邏輯,包括布局、色彩及映像等。這些內容都必須耦合在java代碼中,這樣就導致servlet開發效率低下,令人不勝其煩。jsp出現後彌補了不足,因為jsp檔案是通過在標準的HTML頁面中插入java代碼形成的。其靜態部分無需java程式控制,只有那些需要從資料庫讀取並根據程式動態產生資訊時,才使用java指令碼控制。所以jsp技術出現後,主要用jsp檔案來動態產生HTML檔案,然後返回用戶端顯示。現在的servlet,當需要將整個頁面作為結果返回時,不再由其自己去處理,而是調用jsp檔案。

     下面開發部署一個簡單的servlet程式來展示:

     1.建立處理請求的servlet檔案:

 1 package com.servlet.study; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 public class HelloWorldServlet extends HttpServlet {12     @Override13     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {14         resp.setContentType("text/html;charset=UTF-8");15         String userName = req.getParameter("username");16         String passWord = req.getParameter("password");17         PrintWriter out = resp.getWriter();18         out.print("<html>");19         out.print("<head>");20         out.print("<title>Helloworld</title>");21         out.print("</head>");22         out.print("<body>");23         out.print("<hr>");24         out.println("The username is "+userName);25         out.println("The password is "+passWord);26         out.print("</body>");27         out.print("</html>");28     }29     @Override30     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {31         // TODO Auto-generated method stub32         super.doPost(req, resp);33     }34 }

     2.建立HTML檔案:

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>使用者登入頁面</title> 6 </head> 7 <body> 8 <h1 align="center">登入系統</h1><hr> 9 <form action="helloworld_servlet">10     <table>11         <tr>12             <td>使用者名稱</td>13             <td><input type="text" name="username"></td>14         </tr>15         <tr>16             <td>密碼</td>17             <td><input type="password" name="password"></td>18         </tr>19         <tr>20             <td><input type="reset" value="重填"></td>21             <td><input type="submit" value="提交"></td>22         </tr>23     </table>24 </form>25 </body>26 </html>

     3.在web.xml中配置servlet:

1 <servlet>2         <servlet-name>HelloWorldServlet</servlet-name>3         <servlet-class>com.servlet.study.HelloWorldServlet</servlet-class>//實作類別4</servlet>5     <servlet-mapping>//映射6         <servlet-name>HelloWorldServlet</servlet-name>7         <url-pattern>/helloworld_servlet</url-pattern>//“/”是必須的8     </servlet-mapping>

     註:]servlet類必須繼承HttpServlet類,而且得重寫doGet、doPost方法,並建立out對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.