Title: Servlets and Java Server Pages
1Servlets and Java Server Pages
- Object-Oriented Programming V22.0470
- Written by Haytham Allos (Instructor)
- New York University (NYU)
2Static content
- Web Server delivers contents of a file (html)
- 2. Web Server reads file from disk
- 1. Browser sends request to Web Server
- 3. Web Server sends HTML to Browser
3Dynamic Content
- CGI(Common Gateway Interface)program generates
HTML that is returned to Browser
- 2. Web Server loads CGI program from disk
- 1. Browser sends request to Web Server
- 3. Web Server starts CGI program
- 5. Web Server sends HTML to Browser
- 4. CGI program generates and returns HTML
4CGI has issues
- performance
- Web Server must create new process for each
request, limits scalability - maintenance
- presentation and business logic tightly coupled
5Alternatives
- FastCGI - persistent process
- mod_perl - perl interpreter embedded in Apache
web server - Server Extensions - Netscape(NSAPI),
Microsoft(ISAPI) - Active Server Pages
6Java Servlets
- replaces CGI
- a Java program
- runs in Servlet Container or Engine
- generates dynamic content(HTML, XML)
- now part of J2EE architecture
- good performance
7Java Servlets Continued...
- Advantages
- generally faster, more efficient than CGI
- runs as a thread not an OS process
- Convenience - Java APIs
- secure - does not run in a shell
- portable, vendor independent
8Java Servlet Diagram
- 3. Servlet Engine runs the servlet
- 1. Browser sends request to Web Server
- 2. Web Server sends request to Servlet Engine
- 6. Web Server sends HTML to Browser
- 5. Servlet generates and returns HTML
- 4. Servlet can access database
9Simple Servlet
public class HelloWorld extends HttpServlet
public void doGet( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
response. setContentType(" text/ html")
PrintWriter out response. getWriter()
out. println( "lt HTMLgt" ) out.
println( "lt HEADgtlt TITLEgt Hello Worldlt/ TITLEgtlt/
HEADgt) out. println( "lt BODYgt)
out. println( "lt H1gt Hello Worldlt/ H1gt)
out. println( "lt/ BODYgtlt/ HTMLgt")
10Servlet Issues
- servlet generating HTML results in presentation
and business logic tightly coupled with
accompanying maintenance issues.
11Java Server Pages(JSP)
- Built on Servlet technology
- simplified way to create pages containing
dynamically generated content. - converted into servlet, compiled and run as a
normal servlet - timestamp checked at specified interval,
regenerated if necessary.
12Java Server Pages(JSP) continued...
- jsp page contains
- directives, comments, fixed template data (HTML,
XML), jsp expression or tags, scriptlets
13Java Server Pages(JSP) continued...
- Can be invoked from a browser via URL or from a
Servlet - Java code can be in the jsp source
- keep simple
- Java code can be in a Java Bean
- separates User Interface from Business Logic
- Used via UseBean tag
14Java Server Page Diagram
- 3. JSP Engine generates servlet
- 4. Servlet Container runs the JSP (Servlet)
- 1. Browser sends request to Web Server
- 2. Web Server sends request to Servlet Engine
- 6. JSP(Servlet) returns HTML
- 6. Web Server sends HTML to Browser
- 5. JSP interacts with Java Bean(s) and/or
Servlets
15Simple Java Server Page
- ltHTMLgt
- ltBODYgt
- Hello World. You are using a computer called lt
request.getRemoteHost() gt ! - lt/BODYgt
- lt/HTMLgt
16MVC Architecture
- Model data (java bean)
- View presentation / user interface (jsp)
- Controller directs things / traffic cop (servlet)
17J2EE Architecture
18Servlet/JSP specifications
- Servlet 2.0 JSP 1.0
- Servlet 2.2 JSP 1.1
- Servlet 2.3 JSP 1.2
- Draft stage
- J2EE
19Resources
- http//java.sun.com/products/jsp
- http//java.sun.com/products/servlet
- Books
- OReilly - http//www.ora.com/
- Prentice Hall - http//vig.prenhall.com/
- Addison-Wesley - http//www.aw.com
20Servlet/JSP Fundamentals
- Servlets like any other Java class, except
- Must extend HttpServlet
- Must contain a doPost or doGet method
- Come equipped to handle the laborious points of
http - Http requests and responses are made into Java
objects - Best alternative to traditional CGI
21Servlet/JSP Fundamentals
- Anatomy of a Servlet
- Optional init method, executes the first time a
servlet is invoked - doGet/doPost methods execute when request is made
- method signature accepts http request and
response as parameters - Optional destroy method
- www.servlets.com Samples
22Servlet/JSP Fundamentals
- One instance of Servlet for each Servlet name
- Same instance serves all requests to that name
- Instance members persist across all requests to
that name. - Local /block variables in doPost doGet are
unique to each request
23JSP as presentation model
Servlet Performs the hard work
Helper objects Persistent bean objects DB
Connections, etc
App logic
Simple calls to bean properties
Client request
JSP Successful results
JSP Unsuccessful results
Subsequent client request
Presentation
No servlet need if bean still set
24JSP alternatives
- JSP can still result in unwieldy code creeping
into HTML - Most HTML editors dont like it
- JSPTaglibs and other templating programs may
help. - Much depends on how HTML documents are produced
and what business logic you are presenting
25In Depth Examples
- Encapsulation of SQL calls
- List results and view thread detail
- Post reply and add to favorites
- Set all bean properties w/ request
- Edit users
- Query central
26Pitfalls, surprises, and work-arounds
- HTTP works via requests and responses. Client
caching of responses poses a problem. - Guard against evil use of the back button
- Dont assume everyones client will respond
properly to the defaults - Guard against easy spoofs of any GET request
which alters data.
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)
31(No Transcript)