Download presentation
Presentation is loading. Please wait.
Published byCalvin Stephens Modified over 6 years ago
1
MSIS 655 Advanced Business Applications Programming
Week 15 Servlets In this chapter you will learn: How servlets can be used to extend a Web server's functionality. The servlet life cycle. To execute servlets with the Apache Tomcat server. 11/13/2018 15.1
2
Servlet Overview and Architecture
Small portion of the content is static text or markup Do not produce content Perform a task on behalf of the client JavaServer Pages Extension of servlet technology Most of the content is static text or markup Small portion of the content is generated dynamically Servlet container (servlet engine) Server that executes a servlet Web servers and application servers Sun Java System Application Server Microsoft’s Internet Information Server (IIS) Apache HTTP Server BEA’s WebLogic Application Server IBM’s WebSphere Application Server World Wide Web Consortium’s Jigsaw Web Server Servlet Container An application server that provides the facilities for running Java servlets. Also called a "servlet engine" and "servlet womb," examples of servlet containers are JServ and Tomcat from the Apache Jakarta Project. Today, servlet containers also support JavaServer Pages (JSPs) by converting them to servlets. For example, Tomcat supports JSPs, but its predecessor, JServ, did not. Full blown J2EE-compliant application servers generally handle servlets, JSPs and Enterprise JavaBeans (EJBs). Apache Jakarta Project and the Tomcat Server The Jakarta Project creates and maintains open source software for the Java platform. It operates as an umbrella project under the auspices of the Apache Software Foundation, and all of Jakarta products are released under the Apache License. Tomcat: A popular Java servlet container from the Apache Jakarta project. Tomcat uses the Jasper converter to turn JSPs into servlets for execution. Tomcat is widely used with the JBoss application server. 11/13/2018
3
What are Java Servlets? Servlets are Java technology's answer to CGI programming. They are programs that run on a Web server and build Web pages. Building Web pages on the fly is useful (and commonly done) for a number of reasons: The Web page is based on data submitted by the user. The data changes frequently. The Web page uses information from corporate databases or other such sources. The Web page is based on data submitted by the user. For example the results pages from search engines are generated this way, and programs that process orders for e-commerce sites do this as well. The data changes frequently. For example, a weather-report or news headlines page might build the page dynamically, perhaps returning a previously built page if it is still up to date. The Web page uses information from corporate databases or other such sources. For example, you would use this for making a Web page at an on-line store that lists current prices and number of items in stock. Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and than many alternative CGI-like technologies. (More importantly, servlet developers get paid more than Perl programmers :-). 11/13/2018
4
Fig. 26.1 | Servlet architecture.
11/13/2018
5
Servlets and JavaServer Pages (JSP)
Request-response model Packages javax.servlet javax.servlet.http javax.servlet.jsp javax.servlet.tagext Common implementation of request-response model Web browsers and Web servers Form the Web components of J2EE 11/13/2018
6
JavaServer Pages JavaServer Pages
Extension of Servlet technology Separate the presentation from the business logic Simplify the delivery of dynamic Web content Reuse existing Java components JavaBean Custom-tag libraries Encapsulate complex functionality Classes and interfaces specific to JSP Package javax.servlet.jsp Package javax.servlet.jsp.tagext JSP(JavaServer Page) An extension to the Java servlet technology from Sun that allows HTML to be combined with Java on the same page. The Java provides the processing, and the HTML provides the page layout that will be rendered in the Web browser. Dynamic Web Pages in J2EE JSPs are the primary method in the J2EE platform for displaying dynamic Web pages. Special tags let Java code be included on the page as well as inserted into HTML statements without invalidating the HTML syntax. It thus lets non-Java programmers maintain HTML pages in their favorite authoring programs without interfering with the Java code on the page. With the use of standard and custom JSP tags, the Java code can be completely hidden. 11/13/2018
7
JavaServer Pages Overview
JSPs Look like standard XHTML or XML Normally include XHTML or XML markup Known as fixed-template data Key components Directives Actions Scripting elements Tag libraries When server receive the first JSP request JSP container translates a JSP into a servlet Handle the current and future requests Code that represents the JSP Placed in servlet’s _jspService method JSP errors Translation-time errors Occur when JSPs are translated into servlets Request-time errors Occur during request processing Methods jspInit and jspDestroy Container invokes them when initializing and terminating a JSP Defined in JSP declarations 11/13/2018
8
First JSP Example Simple JSP example (Fig. 27.1) Demonstrates
Fixed-template data (XHTML markup) Creating a Java object (java.util.Date) Automatic conversion of JSP expression to a String meta element to refresh Web page at specified interval First invocation of clock.jsp Notice the delay while: JSP container translates the JSP into a servlet JSP container compiles the servlet JSP container executes the servlet Subsequent invocations should not experience the same delay 11/13/2018
9
11/13/2018
10
JavaServer Pages are easier to implement than servlets when the response to a client request consists primarily of markup that remains constant between requests. 11/13/2018
11
Scripting Components JSP scripting components
Scriptlets (delimited by <% and %>) Comments JSP comments (delimited by <%-- and --%>) XHTML comments (delimited by <!-- and -->) Java’s comments (delimited by // and /* and */) Expressions (delimited by <%= and %>) Declarations (delimited by <%! and %>) Escape sequences 11/13/2018
12
11/13/2018
13
11/13/2018
14
11/13/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.