Learning jsp

24
Java server pages

Transcript of Learning jsp

Java server pages

Basics of Servlet and Jsp

Read the explicit data sent by client.

Read the implicit request data sent by browser.

Generate the result

Send the explicit data to client

Send the implicit HTTP response data

Introduction

JSP or Java Server Pages is a server side technology developed by Sun

Microsystems for coding the presentation layer of an enterprise

application.

A JSP page is a text based document containing static HTML and

dynamic actions which describe how to process a response to the client in a

more powerful and flexible manner

• Can be used to code presentation logic

• Can be used to generate dynamic pages

• Is as simple as HTML, even web designers can use it easily

Servlet v/s JspServletBits of HTML embedded in Java code

Suitable for coding the business layer of an enterprise application

Created and maintained by Java programmers

JSPBits of Java code embedded in HTML

Suitable for coding the presentation layer of an enterprise application

Created and maintained by web designers

How compilation occurs

Client request

Jsp

Compile

Servlet

Is valid?

request

yes

no

class

response

text

Jsp Life CyclejspInit() method.

The code that should be executed only once when the JSP is invoked for the first time

_jspService() method.

This method contains the actual logic to be implemented

jspDestroy() method.

Typically this method contains code to cleanup the resources used by the JSP

Jsp in various scenarios

Implicit objectsA JSP developer can use some implicit objects

out - JSPWriter

request - HttpServletRequest

response- HttpServletResponse

session - HttpSession

application - ServletContext

config - ServletConfig

exception - Throwable / JspException

pageContext- PageContext

page - Object

Jsp TagsTags in JSP can be categorized as

Comments

Scripting elements

Directive elements

Action elements

Template data

– Any thing other than the above mentioned four categories

fall under template data

This will include all HTML tags and text

Scripting elementsSCRIPTING TAGS are of three types

Scriptlet tags:

The Java statements that appear between <% and %> are called scriptlets

Expression tags:

<%=expression%>

Declaration tags:

Variables declared within <%! and %>

Ex:AddSubstract.jsp

Reading request Information <%= request.getMethod() %>

<%= request.getRequestURI() %>

<%= request.getProtocol() %>

<%= request.getQueryString() %>

<%= request.getContentLength() %>

<%= request.getContentType() %>

<%= request.getServerName() %>

<%= request.getServerPort() %>

Example:ReadRequestInfo.jsp

Directive elementsThere are three types of directives:

Page directive: <%@ page attributes %>

Some important attributes:

importEx:<%@ page import = “java.io.*,java.net.Socket” %>

Session<%@ page session = “false” %>(Ex:SessionForm.jsp)

contentType<%@ page contentType = “text/plain” %>

errorPage<%@ page errorPage = “Error.jsp” %>

isErrorPage<%@ page isErrorPage = “true” %>

Ex:ErrorTestPage.jsp

Include directive: This can be used to include contents of some other file in a jsp.

Ex:<%@ include file = "../Header.html" %>

Action tagsSome common standard actions are

jsp:forward<jsp:forward page = “Relative url” />

The url can also be computed at request time

jsp:include<jsp:include page = “Relative url” />

This tag acts at runtime.

jsp:param

Beans: jsp:useBean jsp:setProperty jsp:getProperty

Bean laws public no-arguments constructor

public getter and setter methods. “get” and “set” followed by same word.

property name is derived by changing first character to lowercase.

Setter argument type and getter return type must be identical

For use with JSPs property type should be String or primitive. If it isn’t

you can’t rely on standard actions and you might have to use scripting

Why java beans in Jsp we can define our business logic within a JavaBean and then consistently

use that logic in seperate applications.

Minimizing Java code in a JSP will enable even a web designer to maintain

it.

To instantiate a bean in jsp use <jsp:useBean> tag.

jsp:useBeanThe important attributes of <jsp:useBean> tag are:

Id

Class

Scope

1.)Page

2.)Request

3.)Session

4.)Application

jsp:setPropertyIt is used to sets values to properties of beans in two ways

1. use it after, but outside of a jsp:useBean element, which is executed

regardless of whether a new bean was instantiated or an existing bean was

found.

Appears inside the body of a jsp:useBean element, which is executed only

if a new object was instantiated, not if an existing one was found.

The associated attributes are:

name

property

param

value

<jsp : setProperty> <jsp:useBean id=“person” class=“packagename.classname”>

<jsp:setProperty name=”person” property=”name” param=”username” />

</jsp:useBean>

<input type=”text” name=”name”>

<jsp:useBean id=“person” class=“packagename.classname”>

<jsp:setProperty name=”person” property=”name” />

</jsp:useBean>

If ALL the request parameter names match with the bean property names then

<jsp:useBean id=“person” class=“packagename.classname”>

<jsp:setProperty name=”person” property=”*” />

</jsp:useBean>

<jsp:useBean id=“person” class=“packagename.classname”>

<jsp:setProperty name=“person” property=“wish” value=“Welcome” />

</jsp:useBean>

jsp:getProperty

This element retrieves the value of a bean property, converts it to a string, and inserts it into the output.

The two required attributes are name of a bean, and property whose value should be inserted.

Ex:<jsp:getProperty name ="myWishBean" property = "wish" />

Example:Employee.jsp

Benefits of jsp JSP is a technology for coding the presentation logic of an enterprise application

Web designers without programming skills can create and maintain JSP

Use JSP technology without having to learn the Java language: You can use JSP

technology without learning how to write Java scriplets

JSP is more convenient, not more powerful.

You can use standard Web-site development tools. For example,we use

Macromedia Dreamweaver for most of the JSP pages in the book.