SlideShare a Scribd company logo
1 of 63
JSPs Lecture 8 cs193i – Internet Technologies Summer 2004 Stanford University
Administrative Stuff ,[object Object],[object Object],[object Object]
Why JSPs? ,[object Object],[object Object],[object Object],[object Object]
JSP/ASP/PHP vs CGI/Servlets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is JSP? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a JSP? <html> <body> <jsp:useBean.../> <jsp:getProperty.../> <jsp:getProperty.../> </body> </html>
Advantages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Model-View-Controller ,[object Object],[object Object],[object Object],[object Object]
Model-View-Controller Bean JSP Servlet Controller View Model
public class OrderServlet … { public void dogGet(…){ if(isOrderValid(req)){ saveOrder(req); … out.println(“<html><body>”); … } private void isOrderValid(…){ … } private void saveOrder(…){ … } } Public class OrderServlet … { public void doGet(…){ … if(bean.isOrderValid(…)){ bean.saveOrder(req); … forward(“conf.jsp”); } } <html> <body> <c:forEach items=“${order}”> … </c:forEach> </body> </html> isOrderValid() saveOrder() ------------------ private state Pure Servlet Servlet JSP Java Bean
JSP Big Picture Server w/ JSP Container GET /hello.jsp <html>Hello!</html> Hello.jsp HelloServlet.java HelloServlet.class
A JSP File
<%@ Directive %> ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<%-- JSPComment --> ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Template Text ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<%= expr > ,[object Object],[object Object],[object Object],[object Object],[object Object],WARNING: Old School JSP! The new way is introduced after break.... you may use either, but Old School JSP is used sparingly nowadays
<% code %> ,[object Object],[object Object]
<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%@ page import=&quot;java.util.*&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <% // Create an ArrayList with test data ArrayList list = new ArrayList( ); Map author1 = new HashMap( ); author1.put(&quot;name&quot;, &quot;John Irving&quot;); author1.put(&quot;id&quot;, new Integer(1)); list.add(author1); Map author2 = new HashMap( ); author2.put(&quot;name&quot;, &quot;William Gibson&quot;); author2.put(&quot;id&quot;, new Integer(2)); list.add(author2); Map author3 = new HashMap( ); author3.put(&quot;name&quot;, &quot;Douglas Adams&quot;); author3.put(&quot;id&quot;, new Integer(3)); list.add(author3); pageContext.setAttribute(&quot;authors&quot;, list); %> <html> <head> <title>Search result: Authors</title> </head> <body bgcolor=&quot;white&quot;> Here are all authors matching your search critera: <table> <th>Name</th> <th>Id</th> <c:forEach items=“${ authors }” var=“current”>
<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <html> <head> <title>Browser Check</title> </head> <body bgcolor=&quot;white&quot;> <% String userAgent = request.getHeader(&quot;User-Agent&quot;); if (userAgent.indexOf(&quot;MSIE&quot;) != -1) { %> You're using Internet Explorer. <% } else if (userAgent.indexOf(&quot;Mozilla&quot;) != -1) { %> You're probably using Netscape. <% } else { %> You're using a browser I don't know about. <% } %> </body> </html>
<%! decl > ,[object Object],[object Object],[object Object]
<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0; %> This counter never increases its value: <%= ++localCounter %> </body> </html> Declarations have serious multithreading issues!
<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0; %> This counter never increases its value:  <%= ++localCounter %> </body> </html> Not saved between requests...!
<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%@ page import=&quot;java.util.Date&quot; %> <%! int globalCounter = 0; java.util.Date startDate; public void  jspInit ( ) { startDate = new java.util.Date( ); } public void  jspDestroy ( ) { ServletContext context = getServletConfig().getServletContext( ); context.log(&quot;test.jsp was visited &quot; + globalCounter + &quot; times between &quot; + startDate + &quot; and &quot; + (new Date( ))); } %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited:  <%= ++globalCounter %>  times since  <%= startDate %> . </body> </html> No real need anymore, since we don't use instance variables!
<jsp:include …> ,[object Object]
Five Minute Break
Big Picture – Web Apps Database Legacy Applications Java Applications Web Service Other…? End User #1 End User #2 Web Server (Servlets/JSP)
Invoking Dynamic Code (from JSPs) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Invoking Dynamic Code (from JSPs) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Simple Application or Small Development Team Complex Application or Big Development Team
Servlets and JSPs ,[object Object]
Java Beans ,[object Object],[object Object],[object Object],[object Object]
Java Beans ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java Bean int getCount() void setCount(int c) String getS() void setS(String s) int[] getFoo() void setFoo(int[] f) int count; String s; int[] foo;
 
// MagicBean.java /* A simple bean that contains a single * &quot;magic&quot; string. */ public class MagicBean { private String magic; public MagicBean(String string) { magic = string; } public MagicBean() { magic = &quot;Woo Hoo&quot;; // default magic string } public String getMagic() { return(magic); } public void setMagic(String magic) { this.magic = magic; } }
Java Beans ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<!-- bean.jsp --> <hr> <h3>Bean JSP</h3> <p>Have all sorts of elaborate, tasteful HTML (&quot;presentation&quot;) surrounding the data we pull off the bean. <p> Behold -- I bring forth the magic property from the Magic Bean... <!-- bring in the bean under the name &quot;bean&quot; --> <jsp:usebean id=&quot;bean&quot; class=&quot;MagicBean&quot; /> <table border=1> <tr> <td bgcolor=green><font size=+2>Woo</font> Hoo</td> <td bgcolor=pink> <font size=+3> <td bgcolor=pink> <font size=+3> <!-- the following effectively does bean.getMagic() --> <jsp:getProperty name=&quot;bean&quot; property=&quot;magic&quot; /> </font> </td> <td bgcolor=yellow>Woo <font size=+2>Hoo</font></td> </tr> </table> <!-- pull in content from another page at request time with a relative URL ref to another page -->  <jsp:include page=&quot;trailer.html&quot; flush=&quot;true&quot; />
public class HelloBean extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(&quot;text/html&quot;); PrintWriter out = response.getWriter(); out.println(&quot;<html>&quot;); out.println(&quot;<head>&quot;); out.println(&quot;<html>&quot;); out.println(&quot;<head>&quot;); String title = &quot;Hello Bean&quot;; out.println(&quot;<title>&quot; + title + &quot;</title>&quot;); out.println(&quot;</head>&quot;); out.println(&quot;<body bgcolor=white>&quot;); out.println(&quot;<h1>&quot; + title + &quot;</h1>&quot;); out.println(&quot;<p>Let's see what Mr. JSP has to contribute...&quot;); request.setAttribute(&quot;foo&quot;, &quot;Binky&quot;); MagicBean bean = new MagicBean(&quot;Peanut butter sandwiches!&quot;); request.setAttribute(&quot;bean&quot;, bean); RequestDispatcher rd = getServletContext().getRequestDispatcher(&quot;/bean.jsp&quot;); rd.include(request, response); rd.include(request, response); out.println(&quot;<hr>&quot;); out.println(&quot;</body>&quot;); out.println(&quot;</html>&quot;); } // Override doPost() -- just have it call doGet() public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }
JSP Tags ,[object Object],[object Object],[object Object],[object Object],[object Object]
<%-- JSPComment --%> ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<%@ Directive %> ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Page Directive ,[object Object],[object Object],[object Object],[object Object]
Include Directive ,[object Object],[object Object],[object Object]
<jsp:include …> ,[object Object],[object Object],[object Object]
Scripting Element Tags ,[object Object],[object Object],[object Object]
Action Elements ,[object Object],[object Object],[object Object]
Standard Actions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Standard Actions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Custom Actions (Tag Libraries) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSTL Tags ,[object Object],[object Object],[object Object],[object Object]
JSP Standard Tag Library ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSTL Control Tags <%@ page contentType=&quot;text/html&quot; %> <%@ taglib prefix=&quot;c “uri=&quot;http://java.sun.com/jstl/core&quot; %> <c:if test=&quot;${2>0}&quot;> It's true that (2>0)! </c:if> <c:forEach items=&quot;${paramValues.food}&quot; var=&quot;current&quot;> <c:out value=&quot;${current}&quot; />&nbsp; </c:forEach>
Expression Language Motivation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Advantages of Expression Language (EL) ,[object Object],[object Object],[object Object]
Basic Arithmetic ,[object Object],[object Object],[object Object],[object Object]
Basic Comparisons ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implicit Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditionals ,[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
EL Examples ,[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshellLennart Schoors
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questionsSujata Regoti
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedBG Java EE Course
 
Using SVG with Ample SDK cross browser
Using SVG with Ample SDK cross browserUsing SVG with Ample SDK cross browser
Using SVG with Ample SDK cross browserSergey Ilinsky
 
JSF 2 and beyond: Keeping progress coming
JSF 2 and beyond: Keeping progress comingJSF 2 and beyond: Keeping progress coming
JSF 2 and beyond: Keeping progress comingAndy Schwartz
 
Component Framework Primer for JSF Users
Component Framework Primer for JSF UsersComponent Framework Primer for JSF Users
Component Framework Primer for JSF UsersAndy Schwartz
 
AK 3 web services using apache axis
AK 3   web services using apache axisAK 3   web services using apache axis
AK 3 web services using apache axisgauravashq
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSPFulvio Corno
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorialsunniboy
 
JavaScript Library Overview
JavaScript Library OverviewJavaScript Library Overview
JavaScript Library Overviewjeresig
 

What's hot (20)

Jsp 01
Jsp 01Jsp 01
Jsp 01
 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Open Social Summit Korea
Open Social Summit KoreaOpen Social Summit Korea
Open Social Summit Korea
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
 
Jsf Ajax
Jsf AjaxJsf Ajax
Jsf Ajax
 
JSP
JSPJSP
JSP
 
Using SVG with Ample SDK cross browser
Using SVG with Ample SDK cross browserUsing SVG with Ample SDK cross browser
Using SVG with Ample SDK cross browser
 
JSF 2 and beyond: Keeping progress coming
JSF 2 and beyond: Keeping progress comingJSF 2 and beyond: Keeping progress coming
JSF 2 and beyond: Keeping progress coming
 
Component Framework Primer for JSF Users
Component Framework Primer for JSF UsersComponent Framework Primer for JSF Users
Component Framework Primer for JSF Users
 
Java presentation
Java presentationJava presentation
Java presentation
 
AK 3 web services using apache axis
AK 3   web services using apache axisAK 3   web services using apache axis
AK 3 web services using apache axis
 
Jsp element
Jsp elementJsp element
Jsp element
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
JavaScript Library Overview
JavaScript Library OverviewJavaScript Library Overview
JavaScript Library Overview
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Web programming
Web programmingWeb programming
Web programming
 

Viewers also liked

Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicIMC Institute
 
Joomla!: phpMyAdmin for Beginners
Joomla!: phpMyAdmin for BeginnersJoomla!: phpMyAdmin for Beginners
Joomla!: phpMyAdmin for BeginnersYireo
 
Using XAMPP
Using XAMPPUsing XAMPP
Using XAMPPbutest
 
Lecture03 p1
Lecture03 p1Lecture03 p1
Lecture03 p1aa11bb11
 
phpMyAdmin con Xampp
phpMyAdmin con XamppphpMyAdmin con Xampp
phpMyAdmin con XamppLeccionesWeb
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course JavaEE Trainers
 
Web application using JSP
Web application using JSPWeb application using JSP
Web application using JSPKaml Sah
 
Introducing the MySQL Workbench CASE tool
Introducing the MySQL Workbench CASE toolIntroducing the MySQL Workbench CASE tool
Introducing the MySQL Workbench CASE toolAndrás Bögöly
 
Jdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.comJdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.comphanleson
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure courseMahmoud Alfarra
 

Viewers also liked (20)

Jsp
JspJsp
Jsp
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
xampp_server
xampp_serverxampp_server
xampp_server
 
4. jsp
4. jsp4. jsp
4. jsp
 
Joomla!: phpMyAdmin for Beginners
Joomla!: phpMyAdmin for BeginnersJoomla!: phpMyAdmin for Beginners
Joomla!: phpMyAdmin for Beginners
 
Using XAMPP
Using XAMPPUsing XAMPP
Using XAMPP
 
Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"
 
Lecture03 p1
Lecture03 p1Lecture03 p1
Lecture03 p1
 
phpMyAdmin con Xampp
phpMyAdmin con XamppphpMyAdmin con Xampp
phpMyAdmin con Xampp
 
Unit testing
Unit testingUnit testing
Unit testing
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Software quality assurance
Software quality assuranceSoftware quality assurance
Software quality assurance
 
Web application using JSP
Web application using JSPWeb application using JSP
Web application using JSP
 
Introducing the MySQL Workbench CASE tool
Introducing the MySQL Workbench CASE toolIntroducing the MySQL Workbench CASE tool
Introducing the MySQL Workbench CASE tool
 
Jdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.comJdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.com
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
jdbc
jdbcjdbc
jdbc
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure course
 
JDBC Driver Types
JDBC Driver TypesJDBC Driver Types
JDBC Driver Types
 

Similar to Jsp

ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentationipolevoy
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax componentsIgnacio Coloma
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran TochAdil Jafri
 
Javazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicJavazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicTimothy Perrett
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet Sagar Nakul
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet Sagar Nakul
 
Introduction to JSF
Introduction toJSFIntroduction toJSF
Introduction to JSFSoftServe
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentalsrspaike
 
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)JavaEE Trainers
 
Comparative Display Technologies
Comparative Display TechnologiesComparative Display Technologies
Comparative Display Technologiesjiali zhang
 
JSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLJSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLseleciii44
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSCarol McDonald
 
JSP diana y yo
JSP diana y yoJSP diana y yo
JSP diana y yomichael
 

Similar to Jsp (20)

C:\fakepath\jsp01
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
 
I Feel Pretty
I Feel PrettyI Feel Pretty
I Feel Pretty
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax components
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
 
29 Jsp
29 Jsp29 Jsp
29 Jsp
 
Javazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicJavazone 2010-lift-framework-public
Javazone 2010-lift-framework-public
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
 
Jsp
JspJsp
Jsp
 
Jsfsunum
JsfsunumJsfsunum
Jsfsunum
 
Introduction to JSF
Introduction toJSFIntroduction toJSF
Introduction to JSF
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
 
Os Leonard
Os LeonardOs Leonard
Os Leonard
 
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
 
Comparative Display Technologies
Comparative Display TechnologiesComparative Display Technologies
Comparative Display Technologies
 
JSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLJSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTL
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WS
 
JSP diana y yo
JSP diana y yoJSP diana y yo
JSP diana y yo
 

Recently uploaded

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Recently uploaded (20)

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

Jsp

  • 1. JSPs Lecture 8 cs193i – Internet Technologies Summer 2004 Stanford University
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. What is a JSP? <html> <body> <jsp:useBean.../> <jsp:getProperty.../> <jsp:getProperty.../> </body> </html>
  • 7.
  • 8.
  • 9. Model-View-Controller Bean JSP Servlet Controller View Model
  • 10. public class OrderServlet … { public void dogGet(…){ if(isOrderValid(req)){ saveOrder(req); … out.println(“<html><body>”); … } private void isOrderValid(…){ … } private void saveOrder(…){ … } } Public class OrderServlet … { public void doGet(…){ … if(bean.isOrderValid(…)){ bean.saveOrder(req); … forward(“conf.jsp”); } } <html> <body> <c:forEach items=“${order}”> … </c:forEach> </body> </html> isOrderValid() saveOrder() ------------------ private state Pure Servlet Servlet JSP Java Bean
  • 11. JSP Big Picture Server w/ JSP Container GET /hello.jsp <html>Hello!</html> Hello.jsp HelloServlet.java HelloServlet.class
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. <%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%@ page import=&quot;java.util.*&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <% // Create an ArrayList with test data ArrayList list = new ArrayList( ); Map author1 = new HashMap( ); author1.put(&quot;name&quot;, &quot;John Irving&quot;); author1.put(&quot;id&quot;, new Integer(1)); list.add(author1); Map author2 = new HashMap( ); author2.put(&quot;name&quot;, &quot;William Gibson&quot;); author2.put(&quot;id&quot;, new Integer(2)); list.add(author2); Map author3 = new HashMap( ); author3.put(&quot;name&quot;, &quot;Douglas Adams&quot;); author3.put(&quot;id&quot;, new Integer(3)); list.add(author3); pageContext.setAttribute(&quot;authors&quot;, list); %> <html> <head> <title>Search result: Authors</title> </head> <body bgcolor=&quot;white&quot;> Here are all authors matching your search critera: <table> <th>Name</th> <th>Id</th> <c:forEach items=“${ authors }” var=“current”>
  • 19. <%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <html> <head> <title>Browser Check</title> </head> <body bgcolor=&quot;white&quot;> <% String userAgent = request.getHeader(&quot;User-Agent&quot;); if (userAgent.indexOf(&quot;MSIE&quot;) != -1) { %> You're using Internet Explorer. <% } else if (userAgent.indexOf(&quot;Mozilla&quot;) != -1) { %> You're probably using Netscape. <% } else { %> You're using a browser I don't know about. <% } %> </body> </html>
  • 20.
  • 21. <%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0; %> This counter never increases its value: <%= ++localCounter %> </body> </html> Declarations have serious multithreading issues!
  • 22. <%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0; %> This counter never increases its value: <%= ++localCounter %> </body> </html> Not saved between requests...!
  • 23. <%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; %> <%@ page import=&quot;java.util.Date&quot; %> <%! int globalCounter = 0; java.util.Date startDate; public void jspInit ( ) { startDate = new java.util.Date( ); } public void jspDestroy ( ) { ServletContext context = getServletConfig().getServletContext( ); context.log(&quot;test.jsp was visited &quot; + globalCounter + &quot; times between &quot; + startDate + &quot; and &quot; + (new Date( ))); } %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor=&quot;white&quot;> This page has been visited: <%= ++globalCounter %> times since <%= startDate %> . </body> </html> No real need anymore, since we don't use instance variables!
  • 24.
  • 26. Big Picture – Web Apps Database Legacy Applications Java Applications Web Service Other…? End User #1 End User #2 Web Server (Servlets/JSP)
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. Java Bean int getCount() void setCount(int c) String getS() void setS(String s) int[] getFoo() void setFoo(int[] f) int count; String s; int[] foo;
  • 33.  
  • 34. // MagicBean.java /* A simple bean that contains a single * &quot;magic&quot; string. */ public class MagicBean { private String magic; public MagicBean(String string) { magic = string; } public MagicBean() { magic = &quot;Woo Hoo&quot;; // default magic string } public String getMagic() { return(magic); } public void setMagic(String magic) { this.magic = magic; } }
  • 35.
  • 36. <!-- bean.jsp --> <hr> <h3>Bean JSP</h3> <p>Have all sorts of elaborate, tasteful HTML (&quot;presentation&quot;) surrounding the data we pull off the bean. <p> Behold -- I bring forth the magic property from the Magic Bean... <!-- bring in the bean under the name &quot;bean&quot; --> <jsp:usebean id=&quot;bean&quot; class=&quot;MagicBean&quot; /> <table border=1> <tr> <td bgcolor=green><font size=+2>Woo</font> Hoo</td> <td bgcolor=pink> <font size=+3> <td bgcolor=pink> <font size=+3> <!-- the following effectively does bean.getMagic() --> <jsp:getProperty name=&quot;bean&quot; property=&quot;magic&quot; /> </font> </td> <td bgcolor=yellow>Woo <font size=+2>Hoo</font></td> </tr> </table> <!-- pull in content from another page at request time with a relative URL ref to another page --> <jsp:include page=&quot;trailer.html&quot; flush=&quot;true&quot; />
  • 37. public class HelloBean extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(&quot;text/html&quot;); PrintWriter out = response.getWriter(); out.println(&quot;<html>&quot;); out.println(&quot;<head>&quot;); out.println(&quot;<html>&quot;); out.println(&quot;<head>&quot;); String title = &quot;Hello Bean&quot;; out.println(&quot;<title>&quot; + title + &quot;</title>&quot;); out.println(&quot;</head>&quot;); out.println(&quot;<body bgcolor=white>&quot;); out.println(&quot;<h1>&quot; + title + &quot;</h1>&quot;); out.println(&quot;<p>Let's see what Mr. JSP has to contribute...&quot;); request.setAttribute(&quot;foo&quot;, &quot;Binky&quot;); MagicBean bean = new MagicBean(&quot;Peanut butter sandwiches!&quot;); request.setAttribute(&quot;bean&quot;, bean); RequestDispatcher rd = getServletContext().getRequestDispatcher(&quot;/bean.jsp&quot;); rd.include(request, response); rd.include(request, response); out.println(&quot;<hr>&quot;); out.println(&quot;</body>&quot;); out.println(&quot;</html>&quot;); } // Override doPost() -- just have it call doGet() public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51. JSTL Control Tags <%@ page contentType=&quot;text/html&quot; %> <%@ taglib prefix=&quot;c “uri=&quot;http://java.sun.com/jstl/core&quot; %> <c:if test=&quot;${2>0}&quot;> It's true that (2>0)! </c:if> <c:forEach items=&quot;${paramValues.food}&quot; var=&quot;current&quot;> <c:out value=&quot;${current}&quot; />&nbsp; </c:forEach>
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.