535WebJavaServletJSP18.0.JavaWebJavaCGICGICGIWebJavaWebMicrosoftASPPHPJavaServletJSP1JavaWebJITJavaJava5Java//netweb/servlets_jsp/FiveInts.javaRandomr=newRandom();1SunMicrosoftODBCJavaJDBCMicrosoftASPJavaJSP536for(inti=0;i5;i++)System.out.println(r.nextInt());HTMLPrintln()HTMLJavaServletServletHTMLHTMLJavaHTMLJavaJSPServletJSPServletServlet()doGet()/doPost()javax.servlet.ServletServletWebSunJavaWebServlet537WebJavaServletJSPHTMLCGIWebHttpServletjavax.servlet.httppublicvoidservice(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException;service()requestresponserequestresponseWebHTTPHTMLHTTPGETURLGETURL=123456bookmarkURLlKB%2020ASCIIPOSTHTTPservice()ServletGETdoGet()POSTdoPost()requestresponseservice()doGet()doPost()SunHttpServlet18-118-1HelloServlet.Javaimportjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;/**HelloWorldServlet*/publicclassHelloServletextendsHttpServlet{538publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{PrintWriterout=response.getWriter();response.setContentType(text/html);out.println(H1HellofromaServlet/H1);out.println(PThisservletranat);out.println(newDate().toString());out.println(PCourtesyofHelloServlet.java1.2);}}18-118-118-1ServletServlet18-1doGet()doPost()18-218-2TermsServlet.java/**Servlet*/publicclassTermsServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsIOException{PrintWriterout=resp.getWriter();out.println(HTML);out.println(TITLEIanDarwin'sComputerTermsandAcronyms/TITLE);539WebJavaServletJSPout.println(BODY);out.println(H1IanDarwin'sComputerTermsandAcronyms/H1);out.println(TABLEBORDER=2);out.println(TRTHTermTHMeaning/TR);//Servlet//TRTDJSPTDJavaServerPages,aneattoolfor...TermsAccessortax=newTermsAccessor(terms.txt);Iteratore=tax.iterator();while(e.hasNext()){Termt=(Term)e.next();out.print(TRTD);out.print(t.term);out.print(TD);out.print(t.definition);out.println(/TR);}out.println(/TABLE);out.println(HR/HR);out.println(AHREF=servlet/TermsServletPDFPrinter-friendly(AcrobatPDF)version/A);out.println(HR/HR);out.println(AHREF=mailto:compquest@darwinsys.com/subject=QuestionAskaboutanotherterm/A);out.println(HR/HR);out.println(AHREF=index.htmlBacktoHS/AAHREF=../BacktoDarwinSys/A);out.println(HR/HR);out.println(H6Producedby$Id:TermsServlet.java,v1.12000/04/06ianExp$);out.print(using);out.print(tax.ident);out.println(/H6);}}ServletServletAllaireJRun16.5Servlet54018.2.ServletServletHTMLrequestgetParameter()HTMLFORMINPUTrequest18-218-2HTML8GetYours18-3541WebJavaServletJSPHTMLJavaServletHTML18-3FORMINPUT18-3Servlet18-3IntsServlet.htmHTMLHEADTITLERandomNumbersPage/TITLE/HEADBODYBGCOLOR=whiteH1RandomNumbersPage/H1PThissitewillletyoupicksomerandomnumbersforLottery,luckynumberorotherpurposes,allelectronically./PFORMMETHOD=POSTACTION=/servlets/IntsServletH4HowManyNumbersDoYouWantToday?/H4INPUTNAME=howmanySIZE=2(defaultis5)BRINPUTTYPE=SUBMITVALUE=GetYOURS!/FORM/BODY/HTML18-4JavaServletgetParameter()18-4IntsServlet.javaimportjava.io.*;importjava.util.Random;542importjavax.servlet.*;importjavax.servlet.http.*;publicclassIntsServletextendsHttpServlet{protectedfinalintDEFAULT_NUMBER=5;/***/publicvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsIOException{resp.setContentType(text/html);PrintWriterout=resp.getWriter();//HTMLout.println(HTML);out.println(HEAD);out.println(BODYBGCOLOR=\white\);//HTMLout.println(TITLEYourPersonalRandomNumbers/TITLE);out.println(H1YourPersonalRandomNumbers/H1);out.println(PHereareyourpersonalrandomnumbers,);out.println(carefullyselectedbya);out.println(AHREF=\);out.println(OL);//intn=DEFAULT_NUMBER;Stringnum=req.getParameter(howmany);if(num!=null&&num.length()!=0){try{n=Integer.parseInt(num);}catch(NumberFormatExceptione){out.println(PIdidn'tthinkmuchof);out.println(num);out.println(asanumber./P);}}//Randomr=newRandom();for(inti=0;in;i++){out.print(LI);out.println(r.nextInt(49));//forLotto6/49}out.println(/OL);//out.println(HR/HR);out.println(AHREF=\index.html\BacktomainPage/A);out.println(/HTML);}}543WebJavaServletJSPOrderServlet18.3.cookiecookiecookieNetscapeMSIELynxcookiecookieNetscapecookiecookie.txtCookiejavax.servlet.httpServletexpirytime-1cookiecookie197011UnixcookieHTTPgetCookies()for(inti=0;imySiteCookies.length;i++){Cookiec=mySiteCookies[i];if(c.getName().equals(name-you're-looking-for)){544someString=c.getValue();break;}}Servletprefs.bgcolorcookieServletCookieServletcookieHTMLServletCookieServlet18-518-5CookieServlet.javaimportjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;/**CookieServlet*/publicclassCookieServletextendsHttpServlet{