You are on page 1of 4

Chapter 42 Servlets

1. Common gateway interface is a protocol for server-side programs to generate dynamic


Web content.

2. BothGETandPOSTmethodssendrequeststotheWeb
server.ThePOSTmethodalwaystriggersthecorresponding
CGIprogramtobeexecuted.TheGETmethodmaynotcause
theCGIprogramtobeexecuted,iftheprevioussame
requestiscachedintheWebbrowser.Webbrowsersoften
cacheWebpagessothesamerequestcanbequickly
respondedwithoutcontactingtheWebserver.Thebrowser
checkstherequestsentthroughtheGETmethodasaURL
querystring.IftheresultsfortheexactsameURLare
cachedondisk,thenthepreviousWebpagesfortheURL
mightbedisplayed.ToensureanewWebpagealwaystobe
displayed,usethePOSTmethod.Forexample,aPOST
methodshouldbeusediftherequestwillactuallyupdate
thedatabase.Ifyourrequestisnottimesensitive,such
asfindinganaddressofastudentinthedatabase,you
shouldusetheGETmethodtospeeduptheperformance.

3. YoucansubmitaGETrequestdirectlyfromaURL,buta
POSTrequestmustalwaysbesubmitteddirectlyfroman
HTMLform.

4. Youshoulduse+forspace,donotusequotes.

http://liang:8080/findscore?name=P+Yates

5. ThedifferencesbetweenCGIandservletsare:1.servlets
areJavaprograms.YoucanuseavastcollectionofJava
classestodevelopservlets;2.servletsaremore
efficientthanCGI.

6.YoucandisplayanHTMLfile(e.g.c:\test.html)by
typingthecompletefilenameintheAddressfieldofInternet
Explorer?Canyourunaservletbysimplytypingtheservlet
classfilename?

7.IntheNewProjectdialogbox,chooseWebinthe
CategoriessectionandchooseWebApplicationintheProjects
section.

8. RightclickthewebprojecttochooseNew Servlet to
display the New Servlet dialog box and create a servlet from
the dialog box.
9. Rightclicktheservlet.javafiletochooseRuntorunthe
servlet.

10. Thedefaultportnumberis8080.Ifitisalreadyin
use,NetBeansautomaticallyfindsanotheravailableport
numberafter8080.

11. The.warfileislikethe.jarfile.The.warfileisa
compressedfilethatcontainsruntimefilesforaWeb
project.Wheneveryourprojectisbuiltorrebuilt,a.war
fileisautomaticallycreatedinNetBeans.

12. Thisinit,service,anddestroymethodsareknownas
lifecyclemethodsandarecalledinthefollowingsequence.

1. Theinitmethodiscalledwhentheservletisfirst
created,andisnotcalledagainaslongasthe
servletisnotdestroyed.Thisresemblestheapplets
initmethod,whichisinvokedwhentheappletis
created,andisnotinvokedagainaslongasappletis
notdestroyed.

2. Theservicemethodisinvokedeachtimetheserver
receivesarequestfortheservlet.Theserverspawns
anewthreadandinvokesservice.

3. Thedestroymethodisinvokedonceallthreadswithin
theservlet'sservicemethodhaveexitedoraftera
timeoutperiodhaspassed.Thismethodreleases
resourcesfortheservlet.

13. Whentheservletwasfirstinvoked,itdisplays
Constructorcalled
initcalled
doGetcalled

whentheservletwasinvokedforthesecondtime,itdisplays

doGetcalled

whenTomcatisshutdown
destroycalled

14. Ifyouchangethecontenttypetohtml/plainin
Example27.1,therawHTMLfilewouldbedisplayedonthe
browser.

15. SinceoutisaninstanceofPrintWriter,alltheprint
methodsinPrintWriterdonotthrowIOException.

16. Itreturnsnulliftheparameterdoesnotexist.
17. Textfield:e.g.,LastName<inputtype="text"
name="lastName"size="20">

Radiobutton:e.g.,<inputtype="radio"name="gender"
value="male"checked>Male<inputtype="radio"name="gender"
value="Female">Female</p>

Combobox:e.g.,Major<selectname="major"size="1">
<option>ComputerScience<option>Mathematics
<option>English<option>Chinese</select>

List:e.g.,Minor<selectname="minor"size="2"multiple>
<option>ComputerScience<option>Mathematics
<option>English<option>Chinese</select></p>

Checkbox:e.g.,Hobby:<inputtype="checkbox"
name="tennis">Tennis<inputtype="checkbox"name="golf">
Golf<inputtype="checkbox"name="pingPong"checked>Ping
Pong</p>

Textarea:e.g.,<textareaname="remarks"rows="3"
cols="56"></textarea></p>

18. UsegetParameter(attributeName)methodofarequest
(HttpServletRequestinstance)toobtainavaluefora
parameterandusegetParameterValues(attributeName)to
obtainvaluesinanarrayofstrings.

19.Thedriver.jarfile(e.g.,Oracleclasses12.jar)should
beplacedintoc:\jakartatomcat4.1.27\common\lib.

20.Asessioncanbedefinedasaseriesofrelated
interactionsbetweenasingleclientandtheWebserver
overaperiodoftime.Totrackdataamongrequestsina
sessionisknownassessiontracking.Threetechniquesfor
sessiontrackingare:usinghiddenvalues,usingcookies,
andusingthesessiontrackingtoolsfromservletAPI.

21.Tocreateacookie,usenewCookie(Stringname,String
value).Tosendacookietotheclient,use
response.addCookie(cookie).Togetcookies,use
getCookies()fromHttpServletRequest.UsegetName()to
returnthenameofthecookie.UsegetValue()toreturnthe
valueofthecookie.UsethesetValue(Stringvalue)method
tosetanewvalueinthecookie.UsesetMaxAge(int)toset
anexpirationtime.

22.Yes.EachCookieobjectallowsyoutosetonlyone
key/valuepair.SoyouneedfiveCookieobjectsinorderto
storefivecookies.
23.Togetasession,use

HttpSession session = request.getSession(true);

Tosetanattribute,use
session.setAttribute(String name, Object value)

Togetanattribute,use
session.getAttribute(String name)

24.Sincethemaxinactiveintervalisjust1second,the
sessionislostwhentheuserclickstheConfirmbutton.
Theservletwillreturnnullforstudentinline82inthe
doPostmethod.ANullPointerExceptionwillbethrownin
line85wheninvikingstoreStudent(student).

25.Thesessionwillneverexpire.

26.Tosendimagestothebrowser,getoutputstreambyusing
response.getOutputStream.

27.Youmayembedagetrequestfordisplayingtheimageinthe
HTMLcontent.WhentheHTMLcontentisdisplayed,a
separategetrequestforretrievingtheimageisthensent
totheserver.Sothetextandimageareobtainedthrough
twoseparategetrequests.

You might also like