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

Post on 19-Apr-2019

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java Server PagesJSP

• Introduction

• JSP Architecture

• Scripting Elements

• Directives

• Implicit Objects

2

Agenda

• 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

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

Processing

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

Life Cycle

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

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

API• Packages

– javax.servlet.jsp

– javax.servlet.jsp.tagext

• javax.servlet.jsp package– Interfaces

– JspPage

– HttpJspPage

– Classes– JspWriter

– PageContext

– JspFactory

– JspEngineInfo

– JspException

– JspError

8

Components

• Scripting Elements

• Directives

• Implicit Objects

• Actions

9

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

Expression• <% = expression %>

• <jsp:expression>

expression

</jsp:expression>

Example:

11

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

Scriptlet• <% code fragment %>

• <jsp:scriptlet>

code fragment

</jsp:scriptlet>

Example:

12

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

%>

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;

}

%>

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>

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>

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 Directive• <%@ page attribute="value" %>

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

• Example:

17

<%@ page extends=“myClass”

import=‘java.util.*”

contentType=“text/html”

errorPage=“error.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

errorPage & isErrorPage• Authenticate.html

• Authenticate.jsp

• LoginErrorPage.jsp

19

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

<%@ page isErrorPage="true" %>

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() %>

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.

References

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

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

22

Thank you

23

top related