Java Server Pages Lec 34 What is JSP

  • Slides: 19
Download presentation
Java. Server Pages Lec - 34

Java. Server Pages Lec - 34

What is JSP page? • A text based document capable of returning both static

What is JSP page? • A text based document capable of returning both static & dynamic content to a client browser • Static content & dynamic content can be intermixed • Static content – HTML, XML, Text • Dynamic content – Java code – Displaying properties of Java. Beans – Invoking business logic defined in Custom tags

Web-Based Enterprise Applications in Java Figure shows a simplified view of one application and

Web-Based Enterprise Applications in Java Figure shows a simplified view of one application and its layers.

Java Web Application Technologies (Presentation/Web Tier)

Java Web Application Technologies (Presentation/Web Tier)

First run of a JSP

First run of a JSP

Benefits of JSP • Convenient: – We already know Java and HTML! – Provides

Benefits of JSP • Convenient: – We already know Java and HTML! – Provides an extensive infrastructure for: • • Tracking sessions. Managing cookies. Reading and sending HTML headers. Parsing and decoding HTML form data. • Efficient: – Every request for a JSP is handled by a simple Java thread. Hence, the time to execute a JSP document is not dominated by starting a process.

Benefits of JSP cont. • Portable – JSP follow a well standardized API. –

Benefits of JSP cont. • Portable – JSP follow a well standardized API. – The Java VM which is used to execute a JSP file is supported on many architectures and operating systems. • Inexpensive – There a number of free or inexpensive Web Servers that are good for commercial-quality websites.

JSP vs Servlet Writing Simple Program

JSP vs Servlet Writing Simple Program

JSP Ingredients • Besides HTML, a JSP may contain following elements – directive elements

JSP Ingredients • Besides HTML, a JSP may contain following elements – directive elements • Global control of JSP ……………. – scripting elements • JSP comments………………. • declarations ………………… – Instance variables and methods • expressions ……………. . – Java code fragment which returns String • scriptlets………………. . – Blocks of java code – action elements • special JSP tags ……………. ….

JSP Scripting Elements

JSP Scripting Elements

Scripting Elements: Comments • Two types of comments used in JSP 1. HTML comment:

Scripting Elements: Comments • Two types of comments used in JSP 1. HTML comment: comment is in browser <!-- comment text --> 2. JSP comment (Fully secret comment): Comment is not in browser <%-- comment text --%>

Scripting Elements: Expressions • Format – <%= Java Expression %> • Result – Expression

Scripting Elements: Expressions • Format – <%= Java Expression %> • Result – Expression evaluated, converted to String, and placed into HTML page at the place it occurred in JSP page • Examples – Time: <%= new java. util. Date() %> – Welcome: <%= request. get. Parameter(“name”) %>

Scripting Elements: Scriptlets • Format – <% Java Code %> • Result – Variables

Scripting Elements: Scriptlets • Format – <% Java Code %> • Result – Variables defined inside scriptlet are local • Example <% String n = request. get. Parameter(“name”); out. println(“Welcome” + n); %>

Scripting Elements: Declarations • Format – <%! Java Code %> • Result – Code

Scripting Elements: Declarations • Format – <%! Java Code %> • Result – Code is inserted verbatim into servlet's class definition, outside of any existing methods • Examples – <%! private int some. Field = 5; %> – <%! private void some. Method(. . . ) {. . . } %>

Example Code Sum of two numbers Using JSP scripting elements

Example Code Sum of two numbers Using JSP scripting elements

JSP Scripting Elements XML style • • Comment: No equivalent Declaration: <jsp: declaration> </jsp:

JSP Scripting Elements XML style • • Comment: No equivalent Declaration: <jsp: declaration> </jsp: declaration> Expression: <jsp: expression> </jsp: expression> Scriptlet: <jsp: scriptlet> </jsp: scriptlet>