⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0016.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
<title>网络新时代,软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
</head>

<body>

<p align="center"><script src="../../1.js"></script> </p>

<p align="center"><big><strong>四、范例说明</strong></big></p>
<font color="#000066"><b>

<p align="center">作者:林立伟 东海大学资讯科学系</b></font> </p>

<p>  当我们要设计servlet,而没有指定某个protocol时,我们通常是继承在GenericServlet下(图五)。我们会覆写service() 
method,因为当server有请求进来时,servlet会呼叫service() method来回应这个请求。图六便是说明GenericServlet如何回应client的请求。</p>

<p align="center"><br>
<img src="images/serv_f6.jpg" width="253" height="101"> <br>
图六、Generic Servlet的架构图 </p>

<p>  我们所举的范例是专为http protocol所设计的servlet,所以我们是继承在HttpServlet下,我们所覆写的method不是service() 
method,而是覆写doGet() method来处理GET请求,和覆写doPost() method来处理POST请求。图七说明了Http 
Servlet如何回应请求机制。</p>

<p align="center"><br>
<img src="images/serv_f7.jpg" width="280" height="92"> <br>
图七、Generic Servlet的架构图 </p>

<p>  以下是一个简单的HttpServlet范例:</p>

<table width="70%">
<TBODY>
  <tr>
    <td class="detail"><pre>

<font color="red">
注:此处以&quot;[]&quot;代
换&quot;&lt;&gt;&quot;,避免HTML
的显示错误,请注
意 自行代换。
</font>


public class SimpleServlet extends HttpServlet { 

    public void doGet (HttpServletRequest request,
	                  HttpServletResponse response)
    throws ServletException,IOException {
	PrintWriter out; 
    String title = &quot;Simple Servlet&quot;; 
    response.setContentType(&quot;text/html&quot;);
           out = response.getWriter();
           out.println(&quot;[HTML][HEAD][TITLE]&quot;);
           out.println(title);

           out.println(&quot;[/TITLE][/HEAD][BODY]&quot;);

           out.println(&quot;[H1]&quot; + title + &quot;[/H1]&quot;);

           out.println(&quot;[P]This is output from SimpleServlet.&quot;);

           out.println(&quot;[/BODY][/HTML]&quot;);

           out.close();
    }
}

</pre>
    </td>
  </tr>
</TBODY>
</table>

<ul>
  <li>SimpleServlet是一个继承至 HttpServlet的子类别,而HttpServlet 是一个实作Servlet 
    介面的类别。 </li>
  <li>SimpleServlet覆写 doGet()method,当一个GET的http请求产生时,doGet( ) 
    这段程式便会被执行,在这里是回应一份简单的HTML网页。 </li>
  <li>在doGet()中, client端的请求被 HttpRequest物件所取代,server端的回应被 
    HttpResponse所取代。其中 PrintWriter物件经由HttpResponse将资料传回cleint端。 
  </li>
</ul>

<p><br>
<br>
                [搜集整理] </p>

<p align="center"><script src="../../2.js"></script> </p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -