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

📄 servlet.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 2 页
字号:
class in this package to indicate a servlet problem.</FONT><P> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>javax.servlet.http</CODE>, which contains HTTPservlet classes. The <CODE>HttpServlet</CODE> class is in thispackage. </FONT></UL></FONT><PRE>import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class ExampServlet extends HttpServlet {  public void doPost(HttpServletRequest request, 	 HttpServletResponse response)        throws ServletException, IOException  {    response.setContentType(&quot;text/html&quot;);    PrintWriter out = response.getWriter();    out.println(&quot;&#60;title&#62;Example&#60;/title&#62;&quot; +       &quot;&#60;body bgcolor=FFFFFF&#62;&quot;);    out.println(&quot;&#60;h2&#62;Button Clicked&#60;/h2&#62;&quot;);    String DATA = request.getParameter(&quot;DATA&quot;);    if(DATA != null){      out.println(DATA);    } else {      out.println(&quot;No text entered.&quot;);    }    out.println(&quot;&#60;P&#62;Return to 	&#60;A HREF=&quot;../simpleHTML.html&quot;&#62;Form&#60;/A&#62;&quot;);    out.close();  }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H3>Class and Method Declarations</H3>All servlet classes extend the <CODE>HttpServlet</CODE> abstract class.<CODE>HttpServlet</CODE> simplifies writing HTTP servlets by providing aframework for handling the HTTP protocol. Because <CODE>HttpServlet</CODE>is <CODE>abstract</CODE>, your servlet class must extend it and override atleast one of its methods. An <CODE>abstract</CODE> class is a classthat contains unimplemented methods and cannot be instantiated itself.</FONT><PRE>public class ExampServlet extends HttpServlet {  public void doPost(HttpServletRequest request,         HttpServletResponse response)         throws ServletException, IOException  {</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>ExampServlet</CODE> class is declared <CODE>public</CODE> so the web server that runs the servlet, which is notlocal to the servlet, can access it. <P>The <CODE>ExampServlet</CODE> class defines a <CODE>doPost</CODE>method with the same name, return type, and parameter list as the<CODE>doPost</CODE> method in the <CODE>HttpServlet</CODE> class.By doing this, the <CODE>ExampServlet</CODE> class overrides andimplements the <CODE>doPost</CODE> method in the <CODE>HttpServlet</CODE> class.<P>The <CODE>doPost</CODE> method performs the <CODE>HTTP POST</CODE>operation, which is the type of operation specified in the HTML form used for this example. The other possibility is the <CODE>HTTP GET</CODE> operation, in which case you would implement the <CODE>doGet</CODE> method instead. <P>In short, <CODE>POST</CODE> requests are for sending any amount of data directly over the connection without changing the URL, and <CODE>GET</CODE> requests are for getting limited amounts of information appended to the URL.  <CODE>POST</CODE> requests cannot be bookmarked or emailed and do not change the Uniform Resource Locators (URL) of the response.  <CODE>GET</CODE> requests can be bookmarked and emailed and add information to the URLof the response. <P>The parameter list for the <CODE>doPost</CODE> method takes a<CODE>request</CODE> and a <CODE>response</CODE> object. The browsersends a request to the servlet and the servlet sends a response backto the browser.<P>The <CODE>doPost</CODE> method implementation accesses informationin the <CODE>request</CODE> object to find out who made therequest, what form the request data is in, and which HTTP headerswere sent, and uses the <CODE>response</CODE> object to createan HTML page in response to the browser's request.  The <CODE>doPost</CODE> method throws an <CODE>IOException</CODE>if there is an input or output problem when it handles the request, and a <CODE>ServletException</CODE> if the request could not be handled. These exceptions are handled in the <CODE>HttpServlet</CODE> class.<H3>Method Implementation</H3>The first part of the <CODE>doPost</CODE> method usesthe <CODE>response</CODE> object to create an HTML page.It first sets the response content type to be <CODE>text/html</CODE>,then gets a <CODE>PrintWriter</CODE> object for formatted text output.</FONT><PRE>response.setContentType(&quot;text/html&quot;);    PrintWriter out = response.getWriter();    out.println(&quot;&#60;title&#62;Example&#60;/title&#62;&quot; +       &quot;&#60;body bgcolor=#FFFFFF&#62;&quot;);    out.println(&quot;&#60;h2&#62;Button Clicked&#60;/h2&#62;&quot;);</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The next line uses the <CODE>request</CODE> object toget the data from the text field on the form and store it in the <CODE>DATA</CODE> variable. The <CODE>getparameter</CODE> method gets the named parameter, returns <CODE>null</CODE> if theparameter was not set, and an empty string if the parameterwas sent without a value. </FONT><PRE>    String DATA = request.getParameter(&quot;DATA&quot;);</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The next part of the <CODE>doPost</CODE> method gets the data out of the <CODE>DATA</CODE> parameter and passes it to the <CODE>response</CODE> object to add to theHTML response page. </FONT><PRE>    if(DATA != null){      out.println(DATA);    } else {      out.println(&quot;No text entered.&quot;);    }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The last part of the <CODE>doPost</CODE> method creates a link to take the end user from the HTML response page back to the original form, and closes the response.</FONT><PRE>    out.println(&quot;&#60;P&#62;Return to        &#60;A HREF=&quot;../simpleHTML.html&quot;&#62;Form&#60;/A&#62;&quot;);    out.close();  }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BLOCKQUOTE><HR><STRONG>Note:</STRONG> To learn how to use the other methods available in the <CODE>HttpServlet</CODE>, <CODE>HttpServletRequest</CODE>,and <CODE>HttpServletResponse</CODE> classes, see <A HREF="http://java.sun.com/docs/books/tutorial/index.html">TheJava Tutorial</A> trail on <A HREF="http://java.sun.com/docs/books/tutorial/servlets/index.html">Servlets</A>. <HR></BLOCKQUOTE><A NAME="more"></A><H3>More Information</H3>You can find more information on servlets in the<A HREF="http://java.sun.com/docs/books/tutorial/servlets/index.html">Servlets</A>trail in<A HREF="http://java.sun.com/docs/books/tutorial">The Java Tutorial</A>. <P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">     <TR>    <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>	    <CENTER>    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">    [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>    <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> |     <A HREF="/developer/index.html">Developer Connection</A> |     <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> |     <A HREF="/developer/support/index.html">Online Support</A><BR>    <A HREF="/developer/community/index.html">Community Discussion</A> |    <A HREF="http://java.sun.com/industry/">Industry News</A> |     <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |     <A HREF="http://java.sun.com/casestudies">Case Studies</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD BGCOLOR="#CCCCCC">    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>  </TR>  <TR>    <TD ALIGN="CENTER">    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -     <A HREF="http://java.sun.com/applets/">Applets</A> -     <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -     <A HREF="http://java.sun.com/jobs/">Employment</A> -     <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> -     <A HREF="http://java.sun.com/javastore/">Java Store</A> -    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>    </FONT>    </TD>  </TR>  <TR>    <TD>    <CENTER>    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">    <a href="/siteinfo/faq.html">FAQ</a> |    <a href="/feedback/index.html">Feedback</a> |     <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |     <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>    </FONT>    </CENTER>    </TD>  </TR>    <TR>    <TD>    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">      <TR>        <TD WIDTH="50%">        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        For more information on Java technology<BR>        and other software from Sun Microsystems, call:<BR>        </FONT>        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">        (800) 786-7638<BR></FONT>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Outside the U.S. and Canada, dial your country's         <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>        </FONT>        </TD>        <TD ALIGN="RIGHT" WIDTH="50%">        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">        Copyright &copy; 1995-2000        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>        All Rights Reserved.         <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>.         <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.        </FONT>        </TD>      </TR>    </TABLE>	    </TD>  </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>

⌨️ 快捷键说明

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