Servlets and Java Servlets and Java Server Pages Server Pages Object-Oriented Programming V22.0470 Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) Written by: Haytham Allos (Instructor) New York University (NYU) New York University (NYU)
31
Embed
Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Servlets and Java Server Servlets and Java Server PagesPages
Written by: Haytham Allos (Instructor)Written by: Haytham Allos (Instructor)
New York University (NYU)New York University (NYU)
Static contentStatic content
• Web Server delivers contents of a Web Server delivers contents of a file (html)file (html)
•1. Browser sends request to Web Server
•3. Web Server sends HTML to Browser
•2. Web Server reads file from disk
•b
row
ser
•Web Server
Dynamic Content Dynamic Content
• CGI(CGI(Common Gateway InterfaceCommon Gateway Interface)program )program generates HTML that is returned to generates HTML that is returned to BrowserBrowser
•1. Browser sends request to Web Server
•5. Web Server sends HTML to Browser
•User Computer
•Server
•2. Web Server loads CGI program from disk
•CGI Program
•3. Web Server starts CGI program
•4. CGI program generates and returns HTML
•b
row
ser
•Web Server
CGI has issuesCGI has issues
• performanceperformance– Web Server must create new process Web Server must create new process
for each request, limits scalabilityfor each request, limits scalability
• maintenance maintenance – presentation and business logic tightly presentation and business logic tightly
coupledcoupled
AlternativesAlternatives
• FastCGI - persistent processFastCGI - persistent process
• mod_perl - perl interpreter mod_perl - perl interpreter embedded in Apache web serverembedded in Apache web server
• Server Extensions - Server Extensions - Netscape(NSAPI), Microsoft(ISAPI)Netscape(NSAPI), Microsoft(ISAPI)
• Active Server PagesActive Server Pages
Java ServletsJava Servlets
• replaces CGIreplaces CGI
• a Java programa Java program
• runs in Servlet Container or Engineruns in Servlet Container or Engine
• servlet generating HTML results in servlet generating HTML results in presentation and business logic presentation and business logic tightly coupled with accompanying tightly coupled with accompanying maintenance issues.maintenance issues.
Java Server Pages(JSP)Java Server Pages(JSP)
• Built on Servlet technologyBuilt on Servlet technology
• simplified way to create pages containing simplified way to create pages containing dynamically generated content.dynamically generated content.
• converted into servlet, compiled and run converted into servlet, compiled and run as a normal servletas a normal servlet
• timestamp checked at specified interval, timestamp checked at specified interval, regenerated if necessary.regenerated if necessary.
Java Server Pages(JSP) Java Server Pages(JSP) continuedcontinued......
• JSP can still result in unwieldy code JSP can still result in unwieldy code creeping into HTML creeping into HTML
• Most HTML editors don’t like itMost HTML editors don’t like it
• JSP:Taglibs and other “templating” JSP:Taglibs and other “templating” programs may help.programs may help.
• Much depends on how HTML documents Much depends on how HTML documents are produced and what business logic you are produced and what business logic you are presentingare presenting
In Depth ExamplesIn Depth Examples
• Encapsulation of SQL callsEncapsulation of SQL calls
• List results and view thread detailList results and view thread detail
• Post reply and add to favoritesPost reply and add to favorites
• Set all bean properties w/ requestSet all bean properties w/ request
• Edit usersEdit users
• Query centralQuery central
Pitfalls, surprises, and Pitfalls, surprises, and work-aroundswork-arounds
• HTTP works via requests and responses. HTTP works via requests and responses. Client caching of responses poses a Client caching of responses poses a problem.problem.
• Guard against evil use of the “back” buttonGuard against evil use of the “back” button
• Don’t assume everyone’s client will respond Don’t assume everyone’s client will respond properly to the “defaults”properly to the “defaults”
• Guard against easy spoofs of any GET Guard against easy spoofs of any GET request which alters data.request which alters data.