SlideShare a Scribd company logo
1 of 19
Download to read offline
Java Server Pages


      Summer Internship – 2012
(Indian Institute of Technology Bombay)



                                       Rajavel D
                              (Clicker Software)
Java Server Pages (JSP)

–   JSP is dynamic web page
–   JSP is written as ordinary HTML, with a little Java
    mixed
–   The Java is enclosed in special tags, such as
    <% ... %>
–   JSP files must have the extension .jsp
–   JSP is translated into a Java servlet, which is then
    compiled



    IITB                                        - JSP
JSP Environment




IITB                     - JSP
JSP Tags

• <%= expression %>
     –   The expression is evaluated and the result is inserted
         into the HTML page
• <% code %>
     –   The code is inserted into the servlet's service method
     –   This construction is called a scriptlet
• <%! declarations %>
     –   The declarations are inserted into the servlet class, not
         into a method



    IITB                                                - JSP
Example JSP
 • <HTML>
   <BODY>
   Hello! The time is now <%= new java.util.Date() %>
   </BODY>
   </HTML>

• Notes:
   – The <%= ... %> tag is used, because we are computing a
     value and inserting it into the HTML
   – The fully qualified name (java.util.Date) is used, instead of
     the short name (Date), because we haven’t yet talked
     about how to do import declarations


     IITB                                                  - JSP
Scriptlets
• Scriptlets are enclosed in <% ... %> tags
   – Scriptlets are Java code that may write into the HTML

• Scriptlets are inserted into the servlet exactly as written

   – Example:
     <% if (Math.random() < 0.5) { %>
            Have a <B>nice</B> day!
     <% } else { %>
            Have a <B>good</B> day!
     <% } %>




   IITB                                                    - JSP
Declarations

• Use <%! ... %> tag for declarations
   – If declared with <% ... %>, variables are local

• Example:
     <%! int accessCount = 0; %>
                  :
     <%= ++accessCount %>

• You can use <%! ... %> to declare methods as easily as to
  declare variables




     IITB                                               - JSP
JSP Comments
• Different from HTML comments.
• HTML comments are visible to client.

    <!-- an HTML comment -->

• JSP comments are used for documenting JSP code.
• JSP comments are not visible client-side.

    <%-- a JSP comment --%>



    IITB                                    - JSP
Directives
• Directives affect the servlet class itself

• A directive has the form:
     <%@ directive attribute="value" %>

• The most useful directive is page, which lets you import
  packages
   – Example: <%@ page import="java.util.*" %>




   IITB                                                 - JSP
The include directive

• The include directive inserts another file into the file being
  parsed
• Syntax: <%@ include file="URL " %>
   – The URL is treated as relative to the JSP page
   – If the URL begins with a slash, it is treated as relative to the
     home directory of the Web server
• Example :
   <%@include file="/jsp/header.jsp"%>




     IITB                                                    - JSP
Actions tags
• Actions are XML-syntax tags used to control the servlet engine

• <jsp:include page="URL " />
   – Inserts the relative URL at execution time (not at compile
     time, like the include directive does)
   – This is great for rapidly changing data

• <jsp:forward page="URL" />
  <jsp:forward page="www.google.co.in" />
   – Forward the page to specified URL



     IITB                                                 - JSP
Actions
<jsp: forward page="ssParameters.jsp">
 <jsp: param name="myParam" value="Amar Patel"/>
 <jsp: param name="Age" value="15"/>
</jsp: forward>

Name: <%= request.getParameter("myParam") %>




    IITB                                           - JSP
JSP implicit objects
• JSP provides several implicit Object

  – request : The HttpServletRequest parameter
  – response : The HttpServletResponse parameter
  – session : The HttpSession associated with the request,
    or null if there is none
  – out : A JspWriter (like a PrintWriter) used to send
    output to the client
  – application : Exist through out the application
  – exception : Show the error information



    IITB                                          - JSP
Example (implicit Object)
Request :
<a href="queryString.jsp?id=22&name=guru">
request.getQueryString();
<input type="text" name="name">
request.getParameter("name");
<%=request.getRequestURI()%>

Response :
response.sendRedirect("http://www.google.co.in”);
response.setHeader("Cache-Control","no-cache");
response.setContentType("text/html");


    IITB                                            - JSP
Session in jsp
• In session management whenever a request comes for any
  resource, a unique token is generated by the server and
  transmitted to the client by the response object and stored
  on the client machine as a cookie.

Session management
(i) Session Object
(ii) Cookies
(iii) Hidden Form Fields
(iv) URL Rewriting




  IITB                                               - JSP
Session in jsp
Set Session Attribute
String svalue = request.getParameter("sesvalue_txt");
    if(svalue!=null)
    {

session.setAttribute("sesval",request.getParameter("sesvalue
_txt"));
    }




  IITB                                                  - JSP
Session in jsp
Using Session Attribute
<% if(session.getAttribute("sesval")==null){ %>
     <jsp:forward page = "CreateSessionValue.jsp"/>
     <% } %>
    <h1> Hello <%= (String)session.getAttribute("sesval") %>
</h1>


Remove Session Attribute
<%session.removeAttribute("sesval");%>


  IITB                                                - JSP
Application object in jsp
<% Integer hitsCount = (Integer)application.getAttribute("hitCounter");
  if( hitsCount ==null || hitsCount == 0 ){
     out.println("Welcome to my website!");
     HitsCount = 1;
  }else{
     out.println("Welcome back to my website!");
     hitsCount += 1;
 }    application.setAttribute("hitCounter", hitsCount); %>


<p>Total number of visits: <%= hitsCount%></p>



     IITB                                                          - JSP
References
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html

www.roseindia.net/jsp/jsp.htm

www.jsptutorial.net/

www.jsptut.com/

www.tutorialspoint.com/jsp/index.htm




     IITB                                             - JSP

More Related Content

What's hot

Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)Chitrank Dixit
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorialshiva404
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSINGAaqib Hussain
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance JavaDarshit Metaliya
 
Deploying java beans in jsp
Deploying java beans in jspDeploying java beans in jsp
Deploying java beans in jspPriyanka Pradhan
 
Sightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVASightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVAYash Mody
 
Ch. 9 jsp standard tag library
Ch. 9 jsp standard tag libraryCh. 9 jsp standard tag library
Ch. 9 jsp standard tag libraryManolis Vavalis
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharingvikram singh
 

What's hot (20)

Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Ch. 8 script free pages
Ch. 8 script free pagesCh. 8 script free pages
Ch. 8 script free pages
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Deploying java beans in jsp
Deploying java beans in jspDeploying java beans in jsp
Deploying java beans in jsp
 
Jsp tutorial (1)
Jsp tutorial (1)Jsp tutorial (1)
Jsp tutorial (1)
 
Jsp element
Jsp elementJsp element
Jsp element
 
JSP
JSPJSP
JSP
 
19servlets
19servlets19servlets
19servlets
 
Sightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVASightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVA
 
Ch. 9 jsp standard tag library
Ch. 9 jsp standard tag libraryCh. 9 jsp standard tag library
Ch. 9 jsp standard tag library
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
Jsp
JspJsp
Jsp
 
Spring Mvc Rest
Spring Mvc RestSpring Mvc Rest
Spring Mvc Rest
 
Ch. 7 beeing a jsp
Ch. 7 beeing a jsp     Ch. 7 beeing a jsp
Ch. 7 beeing a jsp
 
Being a jsp
Being a jsp     Being a jsp
Being a jsp
 

Viewers also liked

Viewers also liked (6)

Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Audio video class room
Audio video class roomAudio video class room
Audio video class room
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Java
JavaJava
Java
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similar to Jsp

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10Smita B Kumar
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Ayes Chinmay
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSbharathiv53
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...MathivananP4
 
Lecture 4: JavaServer Pages (JSP) & Expression Language (EL)
Lecture 4:  JavaServer Pages (JSP) & Expression Language (EL)Lecture 4:  JavaServer Pages (JSP) & Expression Language (EL)
Lecture 4: JavaServer Pages (JSP) & Expression Language (EL)Fahad Golra
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsKaml Sah
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGESKalpana T
 

Similar to Jsp (20)

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Jsp
JspJsp
Jsp
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
20jsp
20jsp20jsp
20jsp
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODS
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
Jsp
JspJsp
Jsp
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
J2EE jsp_01
J2EE jsp_01J2EE jsp_01
J2EE jsp_01
 
Lecture 4: JavaServer Pages (JSP) & Expression Language (EL)
Lecture 4:  JavaServer Pages (JSP) & Expression Language (EL)Lecture 4:  JavaServer Pages (JSP) & Expression Language (EL)
Lecture 4: JavaServer Pages (JSP) & Expression Language (EL)
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp basic
Jsp basicJsp basic
Jsp basic
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 

Recently uploaded

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Jsp

  • 1. Java Server Pages Summer Internship – 2012 (Indian Institute of Technology Bombay) Rajavel D (Clicker Software)
  • 2. Java Server Pages (JSP) – JSP is dynamic web page – JSP is written as ordinary HTML, with a little Java mixed – The Java is enclosed in special tags, such as <% ... %> – JSP files must have the extension .jsp – JSP is translated into a Java servlet, which is then compiled IITB - JSP
  • 4. JSP Tags • <%= expression %> – The expression is evaluated and the result is inserted into the HTML page • <% code %> – The code is inserted into the servlet's service method – This construction is called a scriptlet • <%! declarations %> – The declarations are inserted into the servlet class, not into a method IITB - JSP
  • 5. Example JSP • <HTML> <BODY> Hello! The time is now <%= new java.util.Date() %> </BODY> </HTML> • Notes: – The <%= ... %> tag is used, because we are computing a value and inserting it into the HTML – The fully qualified name (java.util.Date) is used, instead of the short name (Date), because we haven’t yet talked about how to do import declarations IITB - JSP
  • 6. Scriptlets • Scriptlets are enclosed in <% ... %> tags – Scriptlets are Java code that may write into the HTML • Scriptlets are inserted into the servlet exactly as written – Example: <% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>good</B> day! <% } %> IITB - JSP
  • 7. Declarations • Use <%! ... %> tag for declarations – If declared with <% ... %>, variables are local • Example: <%! int accessCount = 0; %> : <%= ++accessCount %> • You can use <%! ... %> to declare methods as easily as to declare variables IITB - JSP
  • 8. JSP Comments • Different from HTML comments. • HTML comments are visible to client. <!-- an HTML comment --> • JSP comments are used for documenting JSP code. • JSP comments are not visible client-side. <%-- a JSP comment --%> IITB - JSP
  • 9. Directives • Directives affect the servlet class itself • A directive has the form: <%@ directive attribute="value" %> • The most useful directive is page, which lets you import packages – Example: <%@ page import="java.util.*" %> IITB - JSP
  • 10. The include directive • The include directive inserts another file into the file being parsed • Syntax: <%@ include file="URL " %> – The URL is treated as relative to the JSP page – If the URL begins with a slash, it is treated as relative to the home directory of the Web server • Example : <%@include file="/jsp/header.jsp"%> IITB - JSP
  • 11. Actions tags • Actions are XML-syntax tags used to control the servlet engine • <jsp:include page="URL " /> – Inserts the relative URL at execution time (not at compile time, like the include directive does) – This is great for rapidly changing data • <jsp:forward page="URL" /> <jsp:forward page="www.google.co.in" /> – Forward the page to specified URL IITB - JSP
  • 12. Actions <jsp: forward page="ssParameters.jsp"> <jsp: param name="myParam" value="Amar Patel"/> <jsp: param name="Age" value="15"/> </jsp: forward> Name: <%= request.getParameter("myParam") %> IITB - JSP
  • 13. JSP implicit objects • JSP provides several implicit Object – request : The HttpServletRequest parameter – response : The HttpServletResponse parameter – session : The HttpSession associated with the request, or null if there is none – out : A JspWriter (like a PrintWriter) used to send output to the client – application : Exist through out the application – exception : Show the error information IITB - JSP
  • 14. Example (implicit Object) Request : <a href="queryString.jsp?id=22&name=guru"> request.getQueryString(); <input type="text" name="name"> request.getParameter("name"); <%=request.getRequestURI()%> Response : response.sendRedirect("http://www.google.co.in”); response.setHeader("Cache-Control","no-cache"); response.setContentType("text/html"); IITB - JSP
  • 15. Session in jsp • In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. Session management (i) Session Object (ii) Cookies (iii) Hidden Form Fields (iv) URL Rewriting IITB - JSP
  • 16. Session in jsp Set Session Attribute String svalue = request.getParameter("sesvalue_txt"); if(svalue!=null) { session.setAttribute("sesval",request.getParameter("sesvalue _txt")); } IITB - JSP
  • 17. Session in jsp Using Session Attribute <% if(session.getAttribute("sesval")==null){ %> <jsp:forward page = "CreateSessionValue.jsp"/> <% } %> <h1> Hello <%= (String)session.getAttribute("sesval") %> </h1> Remove Session Attribute <%session.removeAttribute("sesval");%> IITB - JSP
  • 18. Application object in jsp <% Integer hitsCount = (Integer)application.getAttribute("hitCounter"); if( hitsCount ==null || hitsCount == 0 ){ out.println("Welcome to my website!"); HitsCount = 1; }else{ out.println("Welcome back to my website!"); hitsCount += 1; } application.setAttribute("hitCounter", hitsCount); %> <p>Total number of visits: <%= hitsCount%></p> IITB - JSP