SlideShare ist ein Scribd-Unternehmen logo
1 von 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]

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Kürzlich hochgeladen (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

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.