📄 servlet.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD> <TITLE>Java(TM) Language Basics, Part1, Lesson 5: Writing Servlets</TITLE> <META NAME="AUTHOR" CONTENT="Monica Pawlan"> <META NAME="KEYWORDS" CONTENT="programming, basics, Java 2"> <META NAME="OWNER" CONTENT="Editorial/JDC"> <META NAME="revision" CONTENT="@(#)servlet.src 1.24 03/09/00 JDC"> <STYLE TYPE="text/css"> <!-- CODE {font-family: Courier, Monospace; font-size: 12pt} PRE {font-family: Courier, Monospace; font-size: 11pt} --> </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%"> <TR ALIGN="CENTER" VALIGN="TOP"> <TD WIDTH="157" ALIGN="LEFT"> <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT=""> <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A> <BR> <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD> <TD> <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD ALIGN="RIGHT"> <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD WIDTH="100%"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD BGCOLOR="#CC9966" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> </TABLE> </TD> <TD ALIGN="LEFT"> <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD VALIGN="CENTER"> <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD> <TD VALIGN="CENTER"> <FONT FACE="Helvetica" SIZE="1"> <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD> <TD VALIGN="CENTER"> <INPUT TYPE="image" SRC="/images/search.button.gif" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM> <P> <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR VALIGN="TOP"> <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD> <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152"> <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" WIDTH="165" HEIGHT="22" ALT="Online Training" BORDER=0></A></TD> </TR> </TABLE> </TD> </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD> <DIV ALIGN="RIGHT"> <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> </DIV></TD></TR><TR><TD> </TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR> <TD><!-- INSERT SUB-NAV INFO --> </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="TOP"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A NAME="top"></A> <DIV ALIGN="RIGHT"> <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT> <H2>Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Programming Language Basics, Part 1<BR> <FONT SIZE="3">Lesson 5: Writing Servlets</FONT></H2> <FONT SIZE="-1">[<A HREF="front.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="data.html">NEXT>></A>]</FONT></DIV><P>A servlet is an extension to a server that enhancesthe server's functionality. The most common use fora servlet is to extend a web server by providing dynamic web content. Web servers display documents written inHyperText Markup Language (HTML) and respond to userrequests using the HyperText Transfer Protocol (HTTP).HTTP is the protocol for moving hypertext files acrossthe internet. HTML documents contain text that has been marked up for interpretation by an HTML browser such as Netscape.<P>Servlets are easy to write. All you need is the Java® 2 Platform software, and JavaServer<FONT SIZE="-2"><SUP>TM</SUP></FONT> Web Development Kit (JWSDK). You can download a free copy of the <A HREF="http://java.sun.com/products/servlet/index.html">JWSDK</A>. <P>This lesson shows you how to create a very simple form that invokes a basic servlet to process end user data entered on the form. <UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#about">About the Example</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#html">HTML Form</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#serv">Servlet Backend</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#more">More Information</A></FONT></UL><HR><A NAME="about"></A><H3>About the Example</H3>A browser accepts end user input through an HTML form. The simple form used in this lesson has one text input fieldfor the end user to enter text and a Submit button. When the end user clicks the Submit button, the simple servlet isinvoked to process the end user input. <P>In this example, the simple servlet returns an HTML page that displaysthe text entered by the end user. <P><IMG SRC="./Art/overview.gif" WIDTH="366" HEIGHT="127" ALT=""> <A NAME="html"></A><H3>HTML Form</H3>The HTML form is embedded in this <A HREF="./Code/simpleHTML.html">HTML file</A>. The diagram shows how the HTML page looks when it isopened in a browser.<P><IMG SRC="./Art/form.gif" WIDTH="291" HEIGHT="203" ALT="" ALIGN="LEFT">The HTML file and form are similar to the simpleapplication and applet examples in <A HREF="front.html">Lesson 4</A> so you cancompare the code and learn how servlets, applets, and applications handle end user inputs. <P>When the user clicks the <CODE>Click Me</CODE> button, the servlet gets the entered text, and returns an HTML page with the text.<P>The HTML page returned to the browser by the <A HREF="./Code/ExampServlet.java">ExampServlet.java</A>servlet is shown below. The servlet code to retrieve theuser's input and generate the HTML page follows with a discussion.<P><TABLE BORDER="2" CELLPADDING="0"><TR><TD><IMG SRC="./Art/serv.gif" WIDTH="221" HEIGHT="86" ALT=""></TD></TR></TABLE><BLOCKQUOTE><HR><STRONG>Note:</STRONG>To run the example, you have to put the servlet and<CODE>HTML</CODE> files in the correct directories forthe Web server you are using. For example, with Java WebServer 1.1.1,you place the servlet in the <CODE>~/JavaWebServer1.1.1/servlets</CODE>and the <CODE>HTML</CODE> file in the<CODE>~/JavaWebServer1.1.1/public_html</CODE> directory.<HR></BLOCKQUOTE><A NAME="serv"></A><H3>Servlet Backend</H3><A HREF="./Code/ExampServlet.java">ExampServlet.java</A> builds an HTML page to return to the end user. This means the servlet code does not use any Project Swing or Abstract Window Toolkit (AWT) components or have event handling code.For this simple servlet, you only need to import these packages:<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>java.io</CODE> for system input and output. The<CODE>HttpServlet</CODE> class uses the <CODE>IOException</CODE>class in this package to signal that an input or outputexception of some kind has occurred. </FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>javax.servlet</CODE>, which contains generic(protocol-independent) servlet classes. The <CODE>HttpServlet</CODE> class uses the <CODE>ServletException</CODE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -