Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)

Similar presentations


Presentation on theme: "Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)"— Presentation transcript:

1 Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)

2 Static content Web Server delivers contents of a file (html)Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML to Browser 2. Web Server reads file from disk browser Web Server

3 Dynamic Content CGI( Common Gateway Interface )program generates HTML that is returned to BrowserCGI( Common Gateway Interface )program generates HTML that is returned to Browser 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 browser Web Server

4 CGI has issues performanceperformance –Web Server must create new process for each request, limits scalability maintenancemaintenance –presentation and business logic tightly coupled

5 Alternatives FastCGI - persistent processFastCGI - persistent process mod_perl - perl interpreter embedded in Apache web servermod_perl - perl interpreter embedded in Apache web server Server Extensions - Netscape(NSAPI), Microsoft(ISAPI)Server Extensions - Netscape(NSAPI), Microsoft(ISAPI) Active Server PagesActive Server Pages

6 Java Servlets replaces CGIreplaces CGI a Java programa Java program runs in Servlet Container or Engineruns in Servlet Container or Engine generates dynamic content(HTML, XML)generates dynamic content(HTML, XML) now part of J2EE architecturenow part of J2EE architecture good performancegood performance

7 Java Servlets Continued... AdvantagesAdvantages –generally faster, more efficient than CGI – runs as a thread not an OS process –Convenience - Java API’s –secure - does not run in a shell –portable, vendor independent

8 Java Servlet Diagram Extends Web Server Extends Web Server 1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 5. Servlet generates and returns HTML browser Web Server Servlet Container DB TopLink 4. Servlet can access database jdbc 3. Servlet Engine runs the servlet

9 Simple 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( " " ); out. println( " Hello World ”); out. println( " ”); out. println( " Hello World ”); out. println( " "); }

10 Servlet Issues servlet generating HTML results in presentation and business logic tightly coupled with accompanying maintenance issues.servlet generating HTML results in presentation and business logic tightly coupled with accompanying maintenance issues.

11 Java Server Pages(JSP) Built on Servlet technologyBuilt on Servlet technology simplified way to create pages containing dynamically generated content.simplified way to create pages containing dynamically generated content. converted into servlet, compiled and run as a normal servletconverted into servlet, compiled and run as a normal servlet timestamp checked at specified interval, regenerated if necessary.timestamp checked at specified interval, regenerated if necessary.

12 Java Server Pages(JSP) continued... jsp page contains jsp page contains –directives, comments, fixed template data (HTML, XML), jsp expression or tags, scriptlets

13 Java Server Pages(JSP) continued... Can be invoked from a browser via URL or from a ServletCan be invoked from a browser via URL or from a Servlet Java code can be in the jsp sourceJava code can be in the jsp source –keep simple Java code can be in a Java BeanJava code can be in a Java Bean –separates User Interface from Business Logic –Used via UseBean tag

14 Java Server Page Diagram 1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 6. JSP(Servlet) returns HTML browser Web Server Servlet Container DB TopLink 5. JSP interacts with Java Bean(s) and/or Servlets jdbc 4. Servlet Container runs the JSP (Servlet) jsp JSP Engine Servlet source code Java Compiler JSP (Servlet) Java Bean 3. JSP Engine generates servlet

15 Simple Java Server Page Hello World. You are using a computer called !

16 MVC Architecture Model: data (java bean)Model: data (java bean) View: presentation / user interface (jsp)View: presentation / user interface (jsp) Controller: directs things / traffic cop (servlet)Controller: directs things / traffic cop (servlet) Browser Servlet(controller) JSP(View)Java Bean(Model) DB Servlet Container request response 1 instantiate 2 3 5 4

17 J2EE Architecture

18 Servlet/JSP specifications Servlet 2.0 JSP 1.0Servlet 2.0 JSP 1.0 Servlet 2.2 JSP 1.1Servlet 2.2 JSP 1.1 Servlet 2.3 JSP 1.2Servlet 2.3 JSP 1.2 –Draft stage –J2EE

19 Resources http://java.sun.com/products/jsphttp://java.sun.com/products/jsp http://java.sun.com/products/servlethttp://java.sun.com/products/servlet BooksBooks –O’Reilly - http://www.ora.com/ –Prentice Hall - http://vig.prenhall.com/ –Addison-Wesley - http://www.aw.com

20 Servlet/JSP Fundamentals Servlets like any other Java class, except:Servlets like any other Java class, except: Must extend HttpServletMust extend HttpServlet Must contain a doPost or doGet methodMust contain a doPost or doGet method Come equipped to handle the laborious points of httpCome equipped to handle the laborious points of http Http requests and responses are made into Java objectsHttp requests and responses are made into Java objects Best alternative to traditional CGIBest alternative to traditional CGI

21 Servlet/JSP Fundamentals Anatomy of a ServletAnatomy 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 www.servlets.com Sampleswww.servlets.com Samples

22 Servlet/JSP Fundamentals One instance of Servlet for each Servlet nameOne instance of Servlet for each Servlet name Same instance serves all requests to that nameSame instance serves all requests to that name Instance members persist across all requests to that name.Instance members persist across all requests to that name. Local /block variables in doPost & doGet are unique to each requestLocal /block variables in doPost & doGet are unique to each request

23 JSP as presentation model Servlet Performs the hard work Clientrequest JSPSuccessfulresults Helper objects Persistent “bean” objects DB Connections, etc JSPUnsuccessful results results App logic Presentation Subsequent client client request request Simple calls to bean properties No servlet need if bean still set

24 JSP alternatives JSP can still result in unwieldy code creeping into HTMLJSP can still result in unwieldy code creeping into HTML Most HTML editors don’t like itMost HTML editors don’t like it JSP:Taglibs and other “templating” programs may help.JSP:Taglibs and other “templating” programs may help. Much depends on how HTML documents are produced and what business logic you are presentingMuch depends on how HTML documents are produced and what business logic you are presenting

25 In 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

26 Pitfalls, surprises, and work- arounds HTTP works via requests and responses. Client caching of responses poses a problem.HTTP works via requests and responses. Client caching of responses poses a problem. Guard against evil use of the “back” buttonGuard against evil use of the “back” button Don’t assume everyone’s client will respond properly to the “defaults”Don’t assume everyone’s client will respond properly to the “defaults” Guard against easy spoofs of any GET request which alters data.Guard against easy spoofs of any GET request which alters data.

27

28

29

30

31


Download ppt "Servlets and Java Server Pages Object-Oriented Programming V22.0470 Written by: Haytham Allos (Instructor) New York University (NYU)"

Similar presentations


Ads by Google