Docsity

JavaServer Pages (JSP) in ICE 4203: Web Engineering, Slides of Web Design and Development

Partial preview of the text

Download JavaServer Pages (JSP) in ICE 4203: Web Engineering and more Slides Web Design and Development in PDF only on Docsity!

ICE 4203: Web Engineering

Lecture 4: Java Server Pages (JSP)

July – December 2020 Hasan Mahmud Department of Information and Communication Technology (ICT) Bangladesh University of Professionals (BUP)

Contents

  • Introduction to JSP
  • JSP Life Cycle
  • JSP nuts and bolts
  • Scriptlets, Expression, Declaration, Directives

Simple.jsp

<% out.println(“Hello World”); %>

JSP Lifecycle

JSP changed? JSP page (.jsp) Servlet (.java) Servlet Instance Servlet (.class) Yes No JSP Translation JSP Compilation HTTP response Servlet loaded and Initialized HTTP request Web Container

JSP nuts and bolts

Syntactic elements: <%@ directives %>  Interaction with the CONTAINER <%! declarations %>  In the initialization of the JSP <% scriptlets %>  In the service method <%= expressions %>  In the service method

Scriptlets

  • A scriptlet is a block of Java code executed during the request- processing time.
  • In Tomcat all the scriptlets gets put into the service() method of the servlet. They are therefore processed for every request that the servlet receives.

Expressions

• An expression is a shorthand notation that

sends the evaluated Java expression back to

the client (in the form of a String).

• Examples :

<%= getName() %>

<%@ page import=java.util.* %>

Today is: <%= new Date().toString(); %>

Expressions

<%! String name=“hasan” %> <%! public String getName() { return name; } %>

Good Morning <%= getName() %>

Directives

  • A directive is used as a message mechanism to:
    • pass information from the JSP code to the container
  • Main directives:
    • page
    • include (for including other STATIC resources at compilation time)
    • taglib (for including custom tag libraries)

<%@ DIRECTIVE {attribute=value} %> main attributes: <%@ page language=java session=true %> <%@ page import=java.awt.,java.util. %> <%@ page isThreadSafe=false %> <%@ page errorPage=URL %> <%@ page isErrorPage=true %> Directives

What is a Java bean?

  • A bean is a Java class that:
    • Provides a public no-argument constructor
    • Implements java.io.Serializable
    • Follows JavaBeans design patterns
      • Has Set/get methods for properties
      • Has Add/remove methods for events
  • See http://java.sun.com/developer/onlineTraining/Beans/JBeansAPI/shortcourse.html public class SimpleBean implements Serializable { private int counter; SimpleBean() {counter= 0 ;} int getCounter() {return counter;} void setCounter(int c) {counter=c;} }

Standard actions involving beans

<%-- Comment --%> or ?

  • When should I use JSP-style comments instead of HTML-style comments?
  • Always use JSP-style comments unless you specifically want the comments to appear in the HTML that results from serving a JSP page.
  • JSP-style comments are converted by the JSP page engine into Java comments in the source code of the servlet that implements the JSP page. Therefore, JSP-style comments don't appear in the output produced by the JSP page when it runs. HTML-style comments pass through the JSP page engine unmodified. They appear in the HTML source passed to the requesting client.
  • JSP-style comments do not increase the size of the document that results from the JSP page, but are useful to enhance the readability of the JSP page source, and to simplify debugging the servlet generated from the JSP page.
  • taken from: http://java.sun.com/blueprints/qanda/web_tier/index.html#comments

request

<%@ page errorPage="errorpage.jsp" %>

UseRequest

<% // Get the User's Name from the request out.println("Hello: " + request.getParameter("user") + ""); %>