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

📄 aucserv.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>Writing Advanced Applications, Chapter 4: Distributed Computing</TITLE>
  <META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
  <META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
  <META NAME="OWNER" CONTENT="Editorial/JDC">
  <META NAME="revision" CONTENT="@(#)aucserv.src   1.3 07/20/99  JDC">
</HEAD>

<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- 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" 
              value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></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" 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 &amp; 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>&nbsp;</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>

<!-- 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>Writing Advanced Applications<BR>
  <FONT SIZE="3">Chapter 4 Continued: Servlets</FONT></H2>

  <FONT SIZE="-1">[<A HREF="jdbc.html">&lt;&lt;BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="jni.html">NEXT&gt;&gt;</A>]</FONT></DIV>

<P>
A servlet is a server-side program written in the Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> programming 
language that interacts with clients and is usually tied to a HyperText 
Transfer Protocol (HTTP) server. One common use for a servlet is to extend a 
web server by providing dynamic web content. 

<P>
Servlets have an advantage over other technologies in that they are
compiled, have threading capability built in, and provide a secure
programming environment. Even web sites that previously did not
provide servlet support, can do so now by using programs such
as JRun or the Java module for the Apache web server. 

<P>
The web-based <A HREF="./Auction/index.html">auction application</A>
uses a servlet to accept and process buyer and seller input through the 
browser and dynamically return auction item information to the browser.
The <CODE>AuctionServlet</CODE> program is created by extending the
<CODE>HttpServlet</CODE> class. The <CODE>HttpServlet</CODE> class
provides a framework for handling HTTP requests and responses. 

<P>
This section examines the <CODE>AuctionServlet</CODE> and includes
information on how to use <CODE>Cookie</CODE>
and <CODE>Session</CODE> objects in a servlet.

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#servlet">HttpServlet</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#init">The init Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#destroy">The destroy Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#service">The service Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#requests">HTTP Requests</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#cookie">Using Cookies in Servlets</A></FONT>
<UL>
        <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
        <A HREF="#set">Setting a Cookie</A></FONT>
        <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
        <A HREF="#retrieve">Retrieving a Cookie</A></FONT>
        <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
        <A HREF="#gen">Generating Sessions</A></FONT>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#prevent">Preventing Page Caching</A></FONT>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#use">Restricting Access and Redirection</A></FONT>
</UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#error">HTTP Error Codes</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#getpost">Reading GET and POST Values</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#thrd">Threading</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#https">HTTPS</A></FONT>
</UL>

<HR>

<A NAME="servlet"></A>
<H3>HttpServlet</H3>

The <A HREF="./Code/auction/AuctionServlet.java">AuctionServlet</A> class
extends <CODE>HttpServlet</CODE>, which is an abstract class.

<PRE>
  public class AuctionServlet extends HttpServlet {
</PRE>

A servlet can be either loaded when the web server starts up or loaded
when requested by way of an HTTP URL that specifies the servlet.
The servlet is usually loaded by a separate classloader in the web
server because this allows the servlet to be reloaded by unloading the
class loader that loaded the servlet class. However, if the servlet
depends on other classes and one of those classes changes, you
will need to update the date stamp on the servlet for it to
reload.

<P>
After a servlet loads, the first stage in its lifecycle is the web
server calls the servlet's <CODE>init</CODE> method. Once loaded and
initialized, the next stage in the servlet's lifecycle is to
serve requests.  The servlet serves requests through its <CODE>service</CODE>,
<CODE>doGet</CODE>, or <CODE>doPost</CODE> method implementations.

<P>
The servlet can optionally implement a <CODE>destroy</CODE> method to
perform clen-up operations before the web server unloads
the servlet.

<A NAME="init"></A>
<H3>The init Method</H3>

The <CODE>init</CODE> method is only called once by the
web server when the servlet is first started. The <CODE>init</CODE> method 
is passed a <CODE>ServletConfig</CODE> object containing initialization 
information pertaining to the web server where the application is running.

<P>
The <CODE>ServletConfig</CODE> object is used to access information
maintained by the web server including values from the 
<CODE>initArgs</CODE> parameter in the servlet properties file.
Code in the <CODE>init</CODE> method uses the 
<CODE>ServletConfig</CODE> object to retrieve the <CODE>initArgs</CODE>
values by calling the <CODE>config.getInitParameter("parameter")</CODE>
method.

<P> 
The <CODE>AuctionServlet.init</CODE> method also contacts the 
Enterprise JavaBeans server to create a context (<CODE>ctx</CODE>) 
object. The <CODE>ctx</CODE> object is used in the <CODE>service</CODE> 
method to establish a connection with the Enterprise JavaBeans server. 

<PRE>
Context ctx=null;
private String detailsTemplate;

public void init(ServletConfig config) 
		throws ServletException{
  super.init(config);
  try {
    ctx = getInitialContext();
  }catch (Exception e){
    System.err.println(
                "failed to contact EJB server"+e);
  }
  try {
    detailsTemplate=readFile(
	    config.getInitParameter("detailstemplate"));
  } catch(IOException e) {
    System.err.println(
		"Error in AuctionServlet &lt;init&gt;"+e);
  }
}

⌨️ 快捷键说明

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