Java Server Pages

BG Java EE Course
BG Java EE Course Manager, Technical Training at BG Java EE Course
JavaServer Pages (JSP) Svetlin Nakov Borislava Spasova Creating Dynamic Web Pages
Contents ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Contents (2) ,[object Object],[object Object],[object Object],[object Object]
Introduction to JSP Technology
What is JSP? ,[object Object],[object Object],[object Object],[object Object],[object Object]
JSP Technology ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Date JSP Page – Example ,[object Object],<html>  <head><title>Date JSP  example </title></head>  <body>  The date is:  <% out.println(new java.util.Date()); %>  </body>  </html> date.jsp
JSP Expressions ,[object Object],[object Object],[object Object],<%= Java  e xpression %>  The time is : <%= new java.util.Date() %> The square root of 2 is : <%=  Math.sqrt(2)  %> The value of PI is: <%= Math.PI %>
Predefined JSP Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JSP Expressions – More Examples ,[object Object],[object Object],[object Object],Your hostname: <%= request.getRemoteHost() %>  Session timeout :  <%= session.getMaxInactiveInterval() %>  Browser: <%=  request.getHeader(&quot;User-Agent&quot;)  %>
JSP Scriptlets ,[object Object],[object Object],<% Java  c ode %> <%  String queryData = request.getQueryString(); out.println(&quot;Attached GET data: &quot; + queryData); %>
JSP Scriptlets – Example ,[object Object],[object Object],<% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have an <B>interesting</B> day! <% } %> <% for (int i=0; i<10; i++) { %> <%= i %> * <%= i %> = <%= i*i %> <br> <% } %>
JSP Internals How JSP Pages Are Transformed to Servlets?
JSP Technology Internals ,[object Object],[object Object],[object Object],[object Object],JSP Page ( date .jsp ) Java servlet ( date .java ) Compiled Java servlet ( date . class ) JSP compiler javac
JSP Technology Internals ,[object Object],<html>  <head><title>Date JSP example</title></head> <body>  The date is: <% out.println(new java.util.Date()); %>  </body>  </html> date.jsp JSP compilation package org.apache.jsp; public final class date_jsp extends HttpJspBase implements JspSourceDependent { ... } date_jsp.java ebappsSP-Demosdate.jsp orkatalinaocalhostJSP-Demosrgpachesp
JSP Declarations and Directives
JSP Declarations ,[object Object],[object Object],[object Object],<%! Java  c ode  (fields and methods)  %> <%!  long counter = 0;  public void getCounter() { return counter; } %>
JSP Declarations ,[object Object],[object Object],[object Object],[object Object],<%! private  static  int accessCount = 0; %> This page has been accessed   <%= ++accessCount %>  times.
JSP Directives ,[object Object],[object Object],[object Object],<%@ directive attribute=&quot;value&quot; %> <%@ directive attribute1=&quot;value1&quot;  attribute2=&quot;value2&quot; ... attributeN=&quot;valueN&quot; %>
The JSP  @ page  Directive  ,[object Object],[object Object],[object Object],[object Object],import=&quot;package.class &quot;  or import=&quot;package.class1,   ...,   package.classN&quot;  <%@ page import=&quot;java.util.*&quot; %>
The JSP  @ page  Directive  (2) ,[object Object],[object Object],[object Object],contentType=&quot;MIME-Type&quot; or  contentType=&quot;MIME-Type; charset=Character-Set&quot; <%@ page contentType=&quot;text/plain&quot; %> <% response.setContentType(&quot;text/plain&quot;); %>
The JSP  @ page  Directive  (3) ,[object Object],[object Object],[object Object],session=&quot;true|false&quot; errorPage=&quot;url&quot; isErrorPage=&quot;true|false&quot;
The JSP  @ include  Directive  ,[object Object],[object Object],[object Object],[object Object],<%@ include file=&quot;relative url&quot; %> <%@ include file=&quot;/ include/ menu.jsp&quot; %>
Using  JSP  @ include  Directive ,[object Object],< html > < body > <%@ include file=&quot;/navbar.html&quot; %> <!-- Part specific to this page ... --> </ body > </ html >
Dynamic Include ,[object Object],[object Object],[object Object],<jsp:include page=&quot;header.jsp&quot;   />  <% String headerPage = &quot;header.jsp&quot; ;  %> <jsp:include page=&quot;<%= headerPage %>&quot; />
JSP Predefined Variables request ,  response ,  session , application ,  config ,  …
More About The JSP  Predefined Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More About The JSP  Predefined Variables  (2) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More About The JSP  Predefined Variables  (3) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More About The JSP  Predefined Variables  (4) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using The  application  Object ,[object Object],[object Object],[object Object],synchronized (application)  { Vector   items = (Vector) application.getAttribute (&quot;items&quot;); if (sharedItems == null) { sharedItems = new Vector (); application.setAttribute (&quot;items&quot;, items); } }
Using The  application  Object – Example <%@ page import=&quot;java.util.Vector&quot; %> <%// Get the global list of shared items Vector<String> sharedItems; synchronized (application)  { sharedItems = (Vector<String>) application.getAttribute (&quot;items&quot;); if (sharedItems == null) { sharedItems = new Vector<String>(); application.setAttribute (&quot;items&quot;, sharedItems); } } // Append the new item (if exists) String newItem = request.getParameter(&quot;item&quot;); if (newItem != null) sharedItems.addElement(newItem); %>
Using The  application  Object – Example (2) <html> <head><title>Global Shared List</title></head> <body> Available shared items: <ol> <% for (String item : sharedItems) {   %> <li><%= item %></li> <% }   %> </ol> <form method=&quot;POST&quot;  action=&quot;Global-Shared-List.jsp&quot;> <input type=&quot;text&quot; name=&quot;item&quot;> <input type=&quot;submit&quot; value=&quot;Add&quot;> </form> </body> </html>
Client and Server Redirections
Client Redirection to Another URL ,[object Object],[object Object],[object Object],[object Object],[object Object],response.sendRedirect(<url>); response.sendRedirect(&quot;date.jsp&quot;);
Server Redirection to Another Resource ,[object Object],[object Object],[object Object],[object Object],request.getRequestDispatcher(<url>). forward(request, response) request.getRequestDispatcher(&quot;date.jsp&quot;). forward(request, response);
<jsp:forward>  ,[object Object],[object Object],[object Object],<jsp:forward page= {&quot; relativeURL &quot; |   &quot;<%=  expression  %>&quot;} /> <jsp:forward  page={&quot; relativeURL &quot; |   &quot;<%=  expression  %>&quot;} >  <jsp:param name=&quot; parameterName &quot; value=&quot;{ parameterValue  |   <%=  expression  %>}&quot; />  </jsp:forward>
<jsp:forward>  – Example ,[object Object],[object Object],[object Object],<jsp:forward page=&quot;Global-Shared-List.jsp&quot;> <jsp:param name=&quot; item &quot; value= &quot;This item is added by JSP-forward.jsp&quot; /> </jsp:forward>
Escaping Problems And How to Avoid Them
Escaping Problems ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Escaping Problems – Example ,[object Object],[object Object],<html> You entered: <%= request.getParameter(&quot;something&quot;) %> <form> Enter something:<br> <input type=&quot;text&quot; name=&quot;something&quot;> <input type=&quot;submit&quot;> </form> </html> <script language=&quot;JavaScript&quot;>alert('Bug!');</script>
What To Escape? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Escape The HTML Special Characters ,[object Object],[object Object],&quot; &quot; Quotation Mark & &amp; Ampersand > &gt; Greater Than < &lt; Less Than Character HTML Entity Character  Name &nbsp;&nbsp;&nbsp; &nbsp; Tab <br> New Line &nbsp; Space Escaping Character  Name
HTML Escaping ,[object Object],[object Object],public static String htmlEscape(String text) { if (text == null) { return &quot;&quot;; } StringBuilder escapedText =  new StringBuilder(); for (int i=0; i<text.length(); i++) { char ch = text.charAt(i);
HTML Escaping (2) if (ch == '<') escapedText.append(&quot;&lt;&quot;); else if (ch == '>') escapedText.append(&quot;&gt;&quot;); else if (ch == '&') escapedText.append(&quot;&amp;&quot;); else if (ch == 'amp;quot;') escapedText.append(&quot;&quot;&quot;); else escapedText.append(ch); } String result = escapedText.toString(); return result; }
Problems ,[object Object],[object Object],[object Object]
Problems (2) ,[object Object],[object Object],[object Object]
Homework ,[object Object],[object Object],[object Object]
Homework (2) ,[object Object],[object Object],[object Object]
1 of 49

More Related Content

What's hot

Jsp chapter 1Jsp chapter 1
Jsp chapter 1kamal kotecha
10.9K views13 slides
Jsp sasidharJsp sasidhar
Jsp sasidharSasidhar Kothuru
1.7K views56 slides
Jsp elementJsp element
Jsp elementkamal kotecha
24.7K views77 slides

What's hot(20)

Jsp chapter 1Jsp chapter 1
Jsp chapter 1
kamal kotecha10.9K views
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
Chitrank Dixit3.6K views
Jsp sasidharJsp sasidhar
Jsp sasidhar
Sasidhar Kothuru1.7K views
java Servlet technologyjava Servlet technology
java Servlet technology
Tanmoy Barman8.6K views
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
chakrapani tripathi2.1K views
Jsp elementJsp element
Jsp element
kamal kotecha24.7K views
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap22.1K views
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
APSMIND TECHNOLOGY PVT LTD.46.2K views
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
Darshit Metaliya1.2K views
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha17.8K views
Data Access with JDBCData Access with JDBC
Data Access with JDBC
BG Java EE Course 3.3K views
JspJsp
Jsp
Priya Goyal1.5K views
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna6.3K views
Web Applications and DeploymentWeb Applications and Deployment
Web Applications and Deployment
BG Java EE Course 3.6K views
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
Vikas Jagtap1.3K views
Jdbc apiJdbc api
Jdbc api
kamal kotecha3.1K views
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
Manjunatha RK2.2K views
Java servletsJava servlets
Java servlets
Mukesh Tekwani10.8K views
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim4.4K views

Similar to Java Server Pages

Jsp 01Jsp 01
Jsp 01Subhasis Nayak
1K views36 slides
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01Subhasis Nayak
407 views36 slides
JspJsp
JspDSKUMAR G
1.4K views63 slides
29 Jsp29 Jsp
29 JspDSKUMAR G
806 views16 slides
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
157 views95 slides
Jsp1Jsp1
Jsp1Soham Sengupta
486 views54 slides

Similar to Java Server Pages(20)

Jsp 01Jsp 01
Jsp 01
Subhasis Nayak1K views
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
Subhasis Nayak407 views
JspJsp
Jsp
DSKUMAR G1.4K views
29 Jsp29 Jsp
29 Jsp
DSKUMAR G806 views
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
Yoga Raja157 views
Jsp1Jsp1
Jsp1
Soham Sengupta486 views
Jsp SlidesJsp Slides
Jsp Slides
DSKUMAR G2.4K views
JSP.pptxJSP.pptx
JSP.pptx
NishaRohit611 views
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli388 views
JSP diana y yoJSP diana y yo
JSP diana y yo
michael674 views
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
ManishaPatil9327235 views
JspJsp
Jsp
Roy Antony Arnold G807 views
I Feel PrettyI Feel Pretty
I Feel Pretty
John Quaglia576 views
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
Kalpana T72 views
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
shiva404612 views

More from BG Java EE Course

Rich facesRich faces
Rich facesBG Java EE Course
6.3K views80 slides
JSP Custom TagsJSP Custom Tags
JSP Custom TagsBG Java EE Course
5.3K views21 slides
JSTLJSTL
JSTLBG Java EE Course
1.3K views32 slides

More from BG Java EE Course (20)

Rich facesRich faces
Rich faces
BG Java EE Course 6.3K views
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
BG Java EE Course 5.3K views
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
BG Java EE Course 64.1K views
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
BG Java EE Course 12.3K views
JSTLJSTL
JSTL
BG Java EE Course 1.3K views
Unified Expression LanguageUnified Expression Language
Unified Expression Language
BG Java EE Course 4.2K views
Java ServletsJava Servlets
Java Servlets
BG Java EE Course 31K views
CSSCSS
CSS
BG Java EE Course 1.8K views
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
BG Java EE Course 32.7K views
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
BG Java EE Course 12.3K views
WWW and HTTPWWW and HTTP
WWW and HTTP
BG Java EE Course 1.2K views
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course 6K views
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
BG Java EE Course 4.6K views
Processing XML with JavaProcessing XML with Java
Processing XML with Java
BG Java EE Course 2K views
Introduction to XMLIntroduction to XML
Introduction to XML
BG Java EE Course 3.5K views
Introduction to-sqlIntroduction to-sql
Introduction to-sql
BG Java EE Course 7.7K views
Introduction to-RDBMS-systemsIntroduction to-RDBMS-systems
Introduction to-RDBMS-systems
BG Java EE Course 1.5K views
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course 5.3K views
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course 843 views
Strings v.1.1Strings v.1.1
Strings v.1.1
BG Java EE Course 2.5K views

Recently uploaded(20)

Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic Meetup
Rick Ossendrijver23 views
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation23 views
ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman152 views

Java Server Pages

  • 1. JavaServer Pages (JSP) Svetlin Nakov Borislava Spasova Creating Dynamic Web Pages
  • 2.
  • 3.
  • 4. Introduction to JSP Technology
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. JSP Internals How JSP Pages Are Transformed to Servlets?
  • 14.
  • 15.
  • 16. JSP Declarations and Directives
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. JSP Predefined Variables request , response , session , application , config , …
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. Using The application Object – Example <%@ page import=&quot;java.util.Vector&quot; %> <%// Get the global list of shared items Vector<String> sharedItems; synchronized (application) { sharedItems = (Vector<String>) application.getAttribute (&quot;items&quot;); if (sharedItems == null) { sharedItems = new Vector<String>(); application.setAttribute (&quot;items&quot;, sharedItems); } } // Append the new item (if exists) String newItem = request.getParameter(&quot;item&quot;); if (newItem != null) sharedItems.addElement(newItem); %>
  • 33. Using The application Object – Example (2) <html> <head><title>Global Shared List</title></head> <body> Available shared items: <ol> <% for (String item : sharedItems) { %> <li><%= item %></li> <% } %> </ol> <form method=&quot;POST&quot; action=&quot;Global-Shared-List.jsp&quot;> <input type=&quot;text&quot; name=&quot;item&quot;> <input type=&quot;submit&quot; value=&quot;Add&quot;> </form> </body> </html>
  • 34. Client and Server Redirections
  • 35.
  • 36.
  • 37.
  • 38.
  • 39. Escaping Problems And How to Avoid Them
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45. HTML Escaping (2) if (ch == '<') escapedText.append(&quot;&lt;&quot;); else if (ch == '>') escapedText.append(&quot;&gt;&quot;); else if (ch == '&') escapedText.append(&quot;&amp;&quot;); else if (ch == 'amp;quot;') escapedText.append(&quot;&quot;&quot;); else escapedText.append(ch); } String result = escapedText.toString(); return result; }
  • 46.
  • 47.
  • 48.
  • 49.

Editor's Notes

  1. ## * * 07/16/96
  2. ## * * 07/16/96 * Predefined JSP variables are also known as JSP implicit objects
  3. ## * * 07/16/96