java server pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •a way to create...

23
Java Server Pages JSP

Upload: nguyennhan

Post on 19-Apr-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Java Server PagesJSP

Page 2: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

• Introduction

• JSP Architecture

• Scripting Elements

• Directives

• Implicit Objects

2

Agenda

Page 3: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

• A way to create dynamic web pages

• Separates the graphical design from the dynamic content

• A JSP page is a text document that contains:– Static data (such as HTML, WML, and XML)

– JSP elements, which construct dynamic content.

3

Introduction

Page 4: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

First Look at Code<html>

<body>

I am HTML code. <br>

<b><% out.println("I am JSP Code"); %></b><br>

<b><% out.println("Today date & time is: "+java.time.LocalDateTime.now()); %></b>

</body>

</html>

4

Page 5: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Processing

5SRC:https://www.tutorialspoint.com/jsp/jsp_architecture.htm

Page 6: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Life Cycle

6SRC: https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm

Page 7: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

JSP/Servlet CorrespondenceOriginal JSP<%= printme() %>

<% callme(); %>

Possible resulting servlet codepublic void _jspService(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

request.setContentType("text/html");

HttpSession session = request.getSession(true);

JspWriter out = response.getWriter();

out.println(printme());

callme();

...

}7

Page 8: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

API• Packages

– javax.servlet.jsp

– javax.servlet.jsp.tagext

• javax.servlet.jsp package– Interfaces

– JspPage

– HttpJspPage

– Classes– JspWriter

– PageContext

– JspFactory

– JspEngineInfo

– JspException

– JspError

8

Page 9: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Components

• Scripting Elements

• Directives

• Implicit Objects

• Actions

9

Page 10: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Scripting Elements• Provides the ability to insert java code inside the jsp

• Three forms:

– Expressions of the form <%= expression %> that are evaluated and inserted into the output

– Scriptlets of the form <% code %> that are inserted into the servlet's service method

– Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.

10

Page 11: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Expression• <% = expression %>

• <jsp:expression>

expression

</jsp:expression>

Example:

11

<%= java.time.LocalDate.now() %>

Page 12: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Scriptlet• <% code fragment %>

• <jsp:scriptlet>

code fragment

</jsp:scriptlet>

Example:

12

<% out.println("Your IP address is " + request.getRemoteAddr());

%>

Page 13: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Declaration• <% ! declaration; %>

• <jsp:declaration>

• code fragment

• </jsp:declaration>

13

<%! int i = 0; %>

<%! int i; %>

<%! Student student = new Student(); %>

<%! int area(int a, int b){

return a*b;

}

%>

Page 14: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Example: Math.html

14

<form method="post" action="Math.jsp">

<input type="radio" name=“operator" value="add" checked>Addition<br>

<input type="radio" name=“operator" value=“subtract" >Subtraction<br>

<input type="radio" name=“operator" value="multiply" >Multiplication<br>

<input type="radio" name="operator" value="divide" >Division<br>

<br><br>

Enter first Value <input type="text" name=“input1" value=""><br>

Enter second Value <input type="text" name=“input2" value=""><br>

<input type="submit" name="result">

</form>

Page 15: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Example: Math.jsp

15

<body><%!int number1, number2, result;%><H1> Result for <%=request.getParameter("operator")%></H1><%

number1 = Integer.parseInt(request.getParameter("input1"));number2 = Integer.parseInt(request.getParameter("input2"));result = 0;String str = request.getParameter("operator");

if (str.equals("add")) {result = number1 + number2;

}if (str.equals("subtract")) {

result = number1 - number2;}if (str.equals("multiply")) {

result = number1 * number2;}

if (str.equals("divide")) {result = number1 / number2;

}

%>Result is <%=result%>

</body>

Page 16: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Directives • Affects the overall structure of the servlet class

• Tells the web container how to translate a JSP page into the corresponding servlet.

• <%@ directive attribute="value" %>

16

<%@ page ... %> Defines attributes that apply to an entire JSP page.

<%@ include ... %> Includes a file during the translation phase.

<%@ taglib ... %> Declares a tag library, containing custom actions, used in the page

Page 17: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Page Directive• <%@ page attribute="value" %>

• <jsp:directive.page attribute="value" />

• Example:

17

<%@ page extends=“myClass”

import=‘java.util.*”

contentType=“text/html”

errorPage=“error.jsp”

%>

Page 18: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Page directive

18

Directives Values Description

Language scriptingLanguage The default value is "java".

Extends classname

Import importList java.lang.*, javax.servlet.*, javax.servlet.jsp.*,

and javax.servlet.http.* are imported

implicitily

Session true|false

Buffer none|sizekb Default 8 kb

autoFlush true|false

isThreadSafe true|false

Info info_text retrieved later by using getServletInfo()

method

errorPage error_URL

isErrorPage true|false

contentType contentInfo

pageEncoding pageEncodingInfo

Page 19: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

errorPage & isErrorPage• Authenticate.html

• Authenticate.jsp

• LoginErrorPage.jsp

19

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

<%@ page isErrorPage="true" %>

Page 20: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Include Directive• Includes a static file

<%@ include file="relative url" >

<jsp:directive.include file="relative url" />

Example:

main.jsp:

date.jsp:

20

Current date and time is: <%@include file=“date.jsp”

<%@page import =“java.util.*” %><% =(new java.util.Date()).toLocaleString() %>

Page 21: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Implicit Objects

21

Object Description

request This is the HttpServletRequest object associated with the request.

response This is the HttpServletResponse object associated with the response to the client.

out This is the PrintWriter object used to send output to the client.

session This is the HttpSession object associated with the request.

application This is the ServletContext object associated with application context.

config This is the ServletConfig object associated with the page.

pageContext This encapsulates use of server-specific features like higher performance JspWriters.

page This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.

Exception The Exception object allows the exception data to be accessed by designated JSP.

Page 22: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

References

• https://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html

• https://www.javatpoint.com/jsp-tutorial

22

Page 23: Java Server Pages - syedimtiyazhasan.files.wordpress.com file09/03/2018 · •A way to create dynamic web pages •Separates the graphical design from the dynamic content •A JSP

Thank you

23