Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 101

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>13 Servlets</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node16.html#chapter.applets">&lt;&lt; Prev</A></TD><TD><A href="node15.html">- Up -</A></TD></TR></TABLE><DIV id="chapter.servlets"><H1><A name="chapter.servlets">13 Servlets</A></H1><P>A <EM>servlet</EM> is a small application that exists on a Web server and that can be invoked by a CGI command. A servlet is usually called a <EM>CGI script</EM>. CGI (Common Gateway Interface) is a protocol that defines how data is passed between a Web server and a servlet. </P><P>A servlet is a program that accepts an input and calculates a result. To be precise, it does the following steps: </P><UL><LI><P>Get the arguments for the servlet by calling <CODE>Application<SPAN class="keyword">.</SPAN>getCgiArgs</CODE>. A standard application would call <CODE>Application<SPAN class="keyword">.</SPAN>getCmdArgs</CODE> for this purpose. The former is used in exactly the same way as the latter, but instead of parsing command line arguments, it parses CGI arguments. </P></LI><LI><P>Calculate the result. </P></LI><LI><P>Print a header on <CODE>stdout</CODE> that defines the content type. The content type tells the Web browser what the type of the result is, so that it knows how to display it. For example, if the servlet outputs HTML, you have to print something like: </P><BLOCKQUOTE class="code"><CODE>'Content-type:&nbsp;text/html\n\n'</CODE></BLOCKQUOTE><P></P><P>(without the quotes). The <CODE>Open<SPAN class="keyword">.</SPAN>html</CODE> class has support for this (see example below). </P></LI><LI><P>Output the real data. For example, text in HTML format.</P></LI></UL><P></P><P>The following example follows this structure. It uses a class <CODE>Open<SPAN class="keyword">.</SPAN>html</CODE> to generate HTML code on the fly. You can test it by sending a URL that looks like this: </P><BLOCKQUOTE class="code"><CODE><SPAN class="string">'http://www.you.edu/~you/small.cgi?number=10&amp;text=Hi'</SPAN></CODE></BLOCKQUOTE><P></P><P>In this example, the value <CODE>10</CODE> is passed for the argument <CODE><SPAN class="string">'number'</SPAN></CODE> and the value <CODE><SPAN class="string">&quot;Hi+Guys&quot;</SPAN></CODE> for the argument <CODE><SPAN class="string">'text'</SPAN></CODE> (in CGI argument syntax, the plus is used to quote a space). </P><BLOCKQUOTE class="code"><CODE><SPAN class="keyword">functor</SPAN>&nbsp;<BR><SPAN class="keyword">import</SPAN>&nbsp;Application&nbsp;Open<BR><SPAN class="keyword">define</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;%%&nbsp;<SPAN class="comment">Parse&nbsp;the&nbsp;arguments<BR></SPAN>&nbsp;&nbsp;&nbsp;Args={Application<SPAN class="keyword">.</SPAN>getCgiArgs&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record(number(single&nbsp;type:int&nbsp;default:0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text(single&nbsp;type:string&nbsp;default:<SPAN class="string">&quot;none&quot;</SPAN>))}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;%%&nbsp;<SPAN class="comment">A&nbsp;file&nbsp;that&nbsp;supports&nbsp;HTML&nbsp;output<BR></SPAN>&nbsp;&nbsp;&nbsp;Out={New&nbsp;<SPAN class="keyword">class</SPAN>&nbsp;<SPAN class="type">$</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;Open.file&nbsp;Open.html</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(name:stdout)}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;%%&nbsp;<SPAN class="comment">Print&nbsp;MIME&nbsp;content&nbsp;header<BR></SPAN>&nbsp;&nbsp;&nbsp;{Out&nbsp;header}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;%%&nbsp;<SPAN class="comment">Print&nbsp;HTML&nbsp;output<BR></SPAN>&nbsp;&nbsp;&nbsp;{Out&nbsp;tag(html(head(title(<SPAN class="string">'My&nbsp;First&nbsp;CGI'</SPAN>))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body(bgcolor:<SPAN class="string">'#f0f0e0'</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h1(<SPAN class="string">'My&nbsp;First&nbsp;CGI'</SPAN>)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p(<SPAN class="string">'The&nbsp;number&nbsp;is:&nbsp;'</SPAN><SPAN class="keyword">#</SPAN>Args<SPAN class="keyword">.</SPAN>number)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p(<SPAN class="string">'The&nbsp;text&nbsp;is:&nbsp;'</SPAN><SPAN class="keyword">#</SPAN>Args<SPAN class="keyword">.</SPAN>text))))}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;%%&nbsp;<SPAN class="comment">Terminate<BR></SPAN>&nbsp;&nbsp;&nbsp;{Application<SPAN class="keyword">.</SPAN>exit&nbsp;0}<BR><SPAN class="keyword">end</SPAN></CODE></BLOCKQUOTE><P> </P><P>In order to compile this servlet, you have to do: </P><BLOCKQUOTE class="code"><CODE>ozc&nbsp;--execpath=</CODE><I>OZHOME</I><CODE>/bin/ozengine&nbsp;-x&nbsp;small.oz&nbsp;-o&nbsp;small.cgi</CODE></BLOCKQUOTE><P></P><P>Where <I>OZHOME</I> denotes the installation directory of the Mozart system. The <CODE>execpath</CODE> argument is needed because the servlet needs an absolute path. Servlets are normally executed by the Web server in an extremely minimal user environment. The user is typically called <CODE>nouser</CODE> or <CODE>www</CODE> and has almost no rights. In particular you cannot expect the Mozart system to be in the path of the user! This is why you need an absolute pathname when compiling the servlet. </P><P>On a Unix system, you can more simply invoke: </P><BLOCKQUOTE class="code"><CODE>ozc&nbsp;--execpath=`which&nbsp;ozengine`&nbsp;-x&nbsp;small.oz&nbsp;-o&nbsp;small.cgi</CODE></BLOCKQUOTE><P> </P><P>The final step is to install the servlet in your Web server. For this you should contact your local Web site administrator. </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node16.html#chapter.applets">&lt;&lt; Prev</A></TD><TD><A href="node15.html">- Up -</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~duchier/">Denys&nbsp;Duchier</A>, <A href="http://www.ps.uni-sb.de/~kornstae/">Leif&nbsp;Kornstaedt</A> and&nbsp;<A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>