Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)

3,689 views

Published on

Introduction to JavaServer Pages (JSP)

Published in: Technology, Education
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,689
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
0
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide
  • <iframe src="http://rcm.amazon.com/e/cm?t=marcelblog-20&o=1&p=8&l=as1&asins=1932394389&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
  • Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)

    1. 1. Chaper 2 Servlets / JSP Course Introduction to JavaServer Pages
    2. 2. Servlet / JSP course topics <ul><li>Chapter 0 Introduction to Java Web Development </li></ul><ul><li>Chapter 1 Introduction to servlets </li></ul><ul><li>Chapter 2 Introduction to JavaServer Pages </li></ul><ul><li>Chapter 3 How to use the MVC pattern in a Java Web Application </li></ul><ul><li>Chapter 4 How to share information in servlets and JSPs </li></ul><ul><li>Chapter 5 Advanced JSP concepts </li></ul><ul><li>Chapter 6 How to use JavaBeans with JSP </li></ul><ul><li>Chapter 7 How to use the JSP Expression Language (EL) </li></ul><ul><li>Chapter 8 How to use the JSP Standard Tag Library (JSTL) </li></ul><ul><li>Chapter 9 How to use custom JSP tags </li></ul><ul><li>Chapter 10 How to access databases in java web applications </li></ul><ul><li>Chapter 11 How to use JavaMail to send email </li></ul><ul><li>Chapter 12 How to secure java web applications </li></ul><ul><li>Chapter 13 How to download files with Servlets </li></ul><ul><li>Chapter 14 How to work with listeners </li></ul><ul><li>Chapter 15 How to work with filters </li></ul>
    3. 3. Introduction to JavaServer Pages
    4. 4. Intorduction to JavaServer Pages <ul><li>JavaServer Pages definition </li></ul><ul><li>JavaServer Pages life cycle </li></ul><ul><li>Exercise </li></ul><ul><li>Exercise explanation </li></ul>
    5. 5. What Is a JSP Page? <ul><li>JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. </li></ul><ul><li>JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. </li></ul><ul><li>The main features of JSP technology are as follows: </li></ul><ul><ul><li>A language for developing JSP pages, which are text-based documents that describe how to process a request and construct a response </li></ul></ul><ul><ul><li>An expression language for accessing server-side objects </li></ul></ul><ul><ul><li>Mechanisms for defining extensions to the JSP language </li></ul></ul>
    6. 6. What Is a JSP Page? <ul><li>A JSP page is a text document that contains two types of text: </li></ul><ul><ul><li>static data, which can be expressed in any text-based format (such as HTML, SVG, WML, and XML), and </li></ul></ul><ul><ul><li>JSP elements, which construct dynamic content. </li></ul></ul><ul><li>The recommended file extension for the source file of a JSP page is .jsp. The page can be composed of a top file that includes other files that contain either a complete JSP page or a fragment of a JSP page. </li></ul><ul><li>The recommended extension for the source file of a fragment of a JSP page is .jspf. </li></ul><ul><li>The JSP elements in a JSP page can be expressed in two syntaxes, standard and XML, though any given file can use only one syntax. </li></ul><ul><li>A JSP page in XML syntax is an XML document and can be manipulated by tools and APIs for XML documents. </li></ul>
    7. 7. The Life Cycle of a JSP Page <ul><li>The Life Cycle of a JSP Page is similar to that of a servlet with one difference. </li></ul><ul><li>When a Web container receives a request for a JSP file, it passes the request to the JSP processor . </li></ul><ul><li>If this is the first time the JSP file has been requested or if the compiled copy of the JSP file is not found, the JSP compiler generates and compiles a Java source file for the JSP file. </li></ul><ul><li>Then the JSP processor puts the Java source and class file in the JSP processor directory. </li></ul><ul><li>After the JSP processor places the servlet class file in the JSP processor directory, the Web container creates an instance of the servlet and calls the servlet service() method in response to the request. All subsequent requests for the JSP are handled by that instance of the servlet. </li></ul>
    8. 8. The Life Cycle of a JSP Page
    9. 9. The Life Cycle of executing a JSP Page
    10. 10. Exercise 1 <ul><li>Develope the first java web application that uses JSPs </li></ul><ul><li>Download the file: </li></ul><ul><ul><li>jspservlet-02.zip </li></ul></ul><ul><li>Unzip it </li></ul><ul><li>Import from Eclipse </li></ul><ul><li>Run it </li></ul>
    11. 11. Exercise 1 Results <ul><li>You should get same result than chapter before but with JSPs </li></ul>
    12. 12. Exercise 1 Results
    13. 13. Exercise 1 Results <ul><li>The web client: join_email_list.html </li></ul>< form action = &quot; display_email_entry.jsp &quot; method = &quot;post&quot; > < table cellspacing = &quot;5&quot; border = &quot;0&quot; > < tr > < td align = &quot;right&quot; > First name: </ td > < td >< input type = &quot;text&quot; name = &quot;firstName&quot; ></ td > </ tr > < tr > < td align = &quot;right&quot; > Last name: </ td > < td >< input type = &quot;text&quot; name = &quot;lastName&quot; ></ td > </ tr > < tr > < td align = &quot;right&quot; > Email address: </ td > < td >< input type = &quot;text&quot; name = &quot;emailAddress&quot; ></ td > </ tr > < tr > < td ></ td > < td >< br >< input type = &quot;submit&quot; value = &quot;Submit&quot; ></ td > </ tr > </ table > </ form > The action is mapped to a JSP
    14. 14. Exercise 1 Results <ul><li>The web component: display_email_entry.jsp </li></ul>. . . <!-- import packages and classes needed by the scripts --> <%@ page import = &quot;business.*, data.*, java.util.Date&quot; %> <% // get parameters from the request String firstName = request .getParameter( &quot;firstName&quot; ); String lastName = request .getParameter( &quot;lastName&quot; ); String emailAddress = request .getParameter( &quot;emailAddress&quot; ); // get a relative file name ServletContext sc = this .getServletContext(); String path = sc.getRealPath( &quot;/WEB-INF/EmailList.txt&quot; ); // use regular Java objects User user = new User(firstName, lastName, emailAddress); UserIO.add(user, path); %> . . . The request object Accesed by variables
    15. 15. Exercise 1 Results <ul><li>The web component: display_email_entry.jsp </li></ul>. . . < p > Here is the information that you entered: </ p > < table cellspacing = &quot;5&quot; cellpadding = &quot;5&quot; border = &quot;1&quot; > < tr > < td align = &quot;right&quot; > First name: </ td > < td > <%= user.getFirstName() %> </ td > </ tr > < tr > < td align = &quot;right&quot; > Last name: </ td > < td > <%= user.getLastName() %> </ td > </ tr > < tr > < td align = &quot;right&quot; > Email address: </ td > < td > <%= user.getEmailAddress() %> </ td > </ tr > </ table > . . . The response object Is implicit
    16. 16. Exercise 1 Results <ul><li>Result to the response </li></ul>
    17. 17. Exercise 1 Results <ul><li>Compare the code of previous chapter with this chapter </li></ul>. . . // send response to browser response.setContentType( &quot;text/html;charset=UTF-8&quot; ); PrintWriter out = response.getWriter(); out.println( . . . + &quot; <table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot; border=&quot;1&quot;> &quot; + &quot; <tr><td align=&quot;right&quot;>First name:</td> &quot; + &quot; <td>&quot; + firstName + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; <tr><td align=&quot;right&quot;>Last name:</td> &quot; + &quot; <td>&quot; + lastName + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; <tr><td align=&quot;right&quot;>Email address:</td> &quot; + &quot; <td>&quot; + emailAddress + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; </table> &quot; . . . out.close(); . . .
    18. 18. JSP syntax
    19. 19. JSP syntax
    20. 20. Resources To download example code for this chapter go to: http://www.jeetrainers.com

    ×