Servlet Tutorial First Servlets
Servlet Tutorial First Servlets
First Servlets
This tutorial is now out of date. Please see the following sites for up-to-date information with downloadable source code. Intermediate servlet and JSP tutorial Advanced servlet and JSP tutorial JSF 2.0 tutorial Apache Tomcat Tutorial -- setup and introduction (includes Eclipse integration) Ajax tutorial GWT tutorial (Google Web Toolkit) jQuery tutorial Java tutorial
1. Basic Servlet Structure 2. A Simple Servlet Generating Plain Text 3. A Servlet that Generates HTML
4. Simple HTML-Building Utilities 5. Servlet and JSP Tutorial: Top 6. Servlet and JSP Training Courses On-site at your company or at public venues.
10/4/12
} }
(Download template source code -- click with the right mouse on the link or hold down SHIFT while clicking on the link.) To be a servlet, a class should extend HttpServlet and override d G tor d P s (or both), depending on whether oe oot the data is being sent by G Tor by P S . These methods take two arguments: an HttpServletRequest and an E OT HttpServletResponse. The H t S r l t e u s has methods that let you find out about incoming information tpeveRqet such as FORM data, HTTP request headers, and the like. The H t S r l t e p n ehas methods that lets you tpeveRsos specify the HTTP response line (200, 404, etc.), response headers (C n e t T p , S t C o i , etc.), and, otn-ye e-oke most importantly, lets you obtain a PrintWriter used to send output back to the client. For simple servlets, most of the effort is spent in p i t nstatements that generate the desired page. Note that d G tand d P s throw two rnl oe oot exceptions, so you are required to include them in the declaration. Also note that you have to import classes in j v . o(for P i t r t r etc.), j v x s r l t(for H t S r l t etc.), and j v x s r l t h t (for aai rnWie, aa.eve tpeve, aa.eve.tp H t S r l t e u s and H t S r l t e p n e Finally, note that d G tand d P s are called by the tpeveRqet t p e v e R s o s ). oe oot s r i emethod, and sometimes you may want to override s r i edirectly, e.g. for a servlet that handles both evc evc G Tand P S request. E OT
2.1 HelloWorld.java
You can also download the source or try it on-line.
pcaehl; akg al ipr jv.o* mot aai.; ipr jvxsrlt* mot aa.eve.; ipr jvxsrltht.; mot aa.eve.tp* pbi casHlool etnsHtSrlt{ ulc ls elWrd xed tpeve pbi vi dGtHtSrlteus rqet ulc od oe(tpeveRqet eus, HtSrltepnersos) tpeveRsos epne trw Srltxeto,IEcpin{ hos eveEcpin Oxeto Pitrtrot=rsos.eWie(; rnWie u epnegtrtr) otpitn"el Wrd) u.rnl(Hlo ol"; } }
10/4/12
do the same if you are using a Web server that is used by other people and doesn't have a good infrastructure for "virtual servers" to prevent these conflicts automatically. Thus, H l o o l . a aactually goes in a subdirectory elWrdjv called h l in the s r l t directory. Note that setup on most other servers is similar, and the servlet and JSP al eves examples in the tutorial have also been tested using BEA WebLogic and IBM WebSphere 3.0. WebSphere has an excellent mechanism for virtual servers, and it is not necessary to use packages solely to prevent name conflicts with other users. If you've never used packages before, there are two main ways to compile classes that are in packages. One way is to set your C A S A Hto point to the directory above the one actually containing your servlets. You LSPT can them compile normally from within the directory. For example, if your base directory is C \ a a e S r e \ e v e sand your package name (and thus subdirectory name) is h l , and you were on :JvWbevrsrlt al Windows, you'd do:
DS stCASAHC\aaeSre\eves%LSPT% O> e LSPT=:JvWbevrsrlt;CASAH DS c C\aaeSre\eveshl O> d :JvWbevrsrlt\al DS jvcYuSrltjv O> aa oreve.aa The first part, setting the C A S A H you probably want to do permanently, rather than each time you start a new LSPT, DOS window. On Windows 95/98 you'd typically put the "s t C A S A H ..." statement in your a t e e . a e LSPT= uoxcbt file somewhere after the line that set the C A S A Hto point to s r l t j rand j p j r On Windows NT, LSPT eve.a s.a.
you'd go to the Start menu, select Settings, select Control Panel, select System, select Environment, then enter the variable and value. Note also that if your package were of the form n m 1 n m 2 n m 3rather than simply ae.ae.ae n m 1as here, you'd still have the C A S A Hpoint to the top-level directory of your package hierarchy (the one ae LSPT containing n m 1 a e ). A second way to compile classes that are in packages is to go to the directory above the one containing your servlets, and then do "j v c d r c o y Y u S r l t j v " (Windows; note the backslash) or "j v c aa ietr\oreve.aa aa d r c o y Y u S r l t j v " (Unix; note the forward slash). For example, suppose again that your base ietr/oreve.aa directory is C \ a a e S r e \ e v e sand your package name (and thus subdirectory name) is h l , and :JvWbevrsrlt al you were on Windows. In that case, you'd do the following:
DS c C\aaeSre\eves O> d :JvWbevrsrlt DS jvchl\oreve.aa O> aa alYuSrltjv
Note that, on Windows, most JDK 1.1 versions of j v crequire a backslash, not a forward slash, after the aa directory name. This is fixed in JDK 1.2, but since many Web servers are configured to use JDK 1.1, many servlet authors stick with JDK 1.1 for portability. Finally, another advanced option is to keep the source code in a location distinct from the .class files, and use j v c "- " option to install them in the location the Web server expects. a a 's d
10/4/12
3.1 HelloWWW.java
You can also download the source or try it on-line.
pcaehl; akg al ipr jv.o* mot aai.; ipr jvxsrlt* mot aa.eve.; ipr jvxsrltht.; mot aa.eve.tp* pbi casHloW etnsHtSrlt{ ulc ls elWW xed tpeve pbi vi dGtHtSrlteus rqet ulc od oe(tpeveRqet eus, HtSrltepnersos) tpeveRsos epne trw Srltxeto,IEcpin{ hos eveEcpin Oxeto rsos.eCnetye"ethm"; epnestotnTp(tx/tl) Pitrtrot=rsos.eWie(; rnWie u epnegtrtr) otpitn"!OTP HM PBI \-/3/DDHM 40"+ u.rnl(<DCYE TL ULC "/WC/T TL . "rniinl/N"\"+ Tastoa/E\>n "HM>n + <TL\" "HA>TTEHloWW/IL>/ED\"+ <ED<IL>el W<TTE<HA>n "BD>n + <OY\" "H>el WW/1\"+ <1Hlo W<H>n "/OY<HM>) <BD>/TL"; } }
www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html
4/6
10/4/12
10/4/12
pcaehl; akg al ipr jv.o* mot aai.; ipr jvxsrlt* mot aa.eve.; ipr jvxsrltht.; mot aa.eve.tp* pbi casHloW2etnsHtSrlt{ ulc ls elWW xed tpeve pbi vi dGtHtSrlteus rqet ulc od oe(tpeveRqet eus, HtSrltepnersos) tpeveRsos epne trw Srltxeto,IEcpin{ hos eveEcpin Oxeto rsos.eCnetye"ethm"; epnestotnTp(tx/tl) Pitrtrot=rsos.eWie(; rnWie u epnegtrtr) otpitnSrlttlte.edihil(HloWW)+ u.rnl(eveUiiishaWtTte"el W" "BD>n + <OY\" "H>el WW/1\"+ <1Hlo W<H>n "/OY<HM>) <BD>/TL"; } }
Tutorials
Apache Tomcat Tutorial Intermediate Servlets & JSP Tutorial Advanced Servlets & JSP Tutorial Jakarta Struts Tutorial JSF 2 Tutorial Java Tutorial Ajax Tutorial jQuery Tutorial GWT Tutorial
Programming Resources
Specifications & Documentation Servers IDEs Java-Related Jobs JavaServer Faces (JSF) Books Web Hosting Providers Java Programming Resources
Books
Core Servlets & JSP 2nd Ed. M ore Servlets & JSP Core Web Programming Core Servlets & JSP 1st Ed. Core Servlets & JSP E-Book M ore Servlets & JSP E-Book
Consulting
Our consulting services We are hiring!
Jobs
www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html