0% found this document useful (0 votes)
23 views29 pages

JSPTags

Uploaded by

Kripa Pattiwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views29 pages

JSPTags

Uploaded by

Kripa Pattiwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

JSP Syntax & Directives

Mr. V. M. Vasava
GPG, IT Dept. Surat

1
JavaServer Pages
Agenda

JSP Syntax
Implicit objects
JSP Directives

2
JSP Tag
1.scriptlet tag
2.Expression tag
3. Declarative tag

1.Scriptlet Tag
A scriptlet is a block of Java code that is executed during
the request-processing time.
Syntax: <% ….java code…. %>

3
DeclarationTag

A declaration is a block of code in a


JSP that is used to define class-wide
variables and methods in the
generated servlet.
Syntax:
<%! Dec var %>
Example:- <%! int var=10; %>

4
Example

5
Output

6
Expression tag
Expressions: sends a value of a Java
expression back to the client.
Syntax:
<%= expression%>
Example:- <% num1 = num1+num2 %>

7
Example

8
Output

9
HTML Comment
Generates a comment that is sent to the client.

 Syntax

<!-- comment [ <%= expression %> ] -->

 Example:

<!-- This page was loaded on


<%= (new java.util.Date()).toLocaleString() %>
-->

10
Predefined Variable – Implicit
Objects
request – Object of HttpServletRequest (request parameters, HTTP headers,
cookies

response – Object of HttpServletResponse

out - Object of PrintWriter buffered version JspWriter

session - Object of HttpSession associated with the request

application - Object of ServletContext shared by all servlets in the engine

config - Object of ServletConfig

pageContext - Object of PageContext in JSP for a single point of access

page – variable synonym for this object

11
Java Implicit Objects
Example
<html> <p>
<head> Storing a string to the application...<br>
<title>Implicit Objects</title> <% application.setAttribute("name", “vmv"); %>
</head> Retrieving the string from application...<br>
<body style="font-family:verdana;font-size:10pt"> <b>Name:</b>
<p> <%= application.getAttribute("name") %>
Using Request parameters...<br> </p>
<b>Name:</b> <%= request.getParameter("name") <p>
%>
Storing a string to the page context...<br>
</p>
<% pageContext.setAttribute("name", “vmv"); %>
<p>
Retrieving the string from page context...</br>
<% out.println("This is printed using the out implicit
variable"); %> <b>Name:</b>

</p> <%= pageContext.getAttribute("name") %>

<p> </p>

Storing a string to the session...<br> </body>

<% session.setAttribute("name", “Nivedita"); %> </html>

Retrieving the string from session...<br>


<b>Name:</b> <%= session.getAttribute("name") %>
</p>
Scriptlet
Contains a code fragment valid in the page scripting
language.

 Syntax
<% code fragment %>

<%
String var1 = request.getParameter("name");
out.println(var1);
%>

This code will be placed in the generated servlet method:


_jspService()
13
JSP Page Directive
The jsp directives are messages that tells the web container how to
translate a JSP page into the corresponding servlet.
Directives are messages to the JSP container and do not produce
output into the current output stream
 Syntax:
<%@ directive attribute=“value” %>
<%@ directive attribute1=“value1”
attribute1 =“value2” … %>

There are three types of directives:


1. page
2. include
3. Taglib
XML form:
<jsp:directive.directiveType attribute=“value” />
14
Page Directive
Defines attributes that apply to an entire JSP page.
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet
]" [ isErrorPage="true|false" ]
%>

15
Attributes of Page Directive
1)import
The import attribute is used to import class,interface or
all the members of a package.It is similar to import
keyword in java class or interface.
Example:
<%@page import="java.util.Date"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
Date date = new Date();
out.print( "Today date" +"<h2 align = \"center\">"
+date.toString()+"</h2>");
%>
16
Continued..

17
attributes
2)contentType
The contentType attribute defines the MIME(Multipurpose
Internet Mail Extension) type of the HTTP response.The
default value is "text/html;charset=ISO-8859-1".
3)extends
The extends attribute defines the parent class that will be
inherited by the generated servlet.It is rarely used.
4)info
This attribute simply sets the information of the JSP page
which is retrieved later by using getServletInfo() method of
Servlet interface.

18
Attributes
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle
output generated by the JSP page.The default size of the
buffer is 8Kb.
Example:
<%@ page buffer="16kb" %>
Today is: <%= new java.util.Date() %>

6)language
The language attribute specifies the scripting language used
in the JSP page. The default value is "java".
19
Output

20
Include Directive
Includes a static file in a JSP file, parsing the file's JSP elements.
 Syntax

<%@ include file="relativeURL" %>

The <%@ include %> directive inserts a file of text or code in a JSP file
at translation time, when the JSP file is compiled.
<%@ include %> process is static. A static include means that the text of
the included file is added to the JSP file.
The included file can be:
1. JSP file,
2. HTML file,
3. text file.

21
Taglib Directive
Defines a tag library and prefix for the custom
tags used in the JSP page.
 Syntax
<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix"
%>

<%@ taglib uri="http://thathost/tags" prefix="public"


%>
The <%@ taglib %> directive declares that the JSP file uses custom tags,
names the tag library that defines them, and specifies their tag prefix.

22
Example
Develop an application Control transfer from one JSP
page to Other.

In this example, the Login.jsp design UI using HTML


tag.

Regsuccess.jsp page retrieve the value from login page


through directive and getparameter() method.

23
Example
Login.jsp
<html><head></head>
<body bgcolor="orange">
<form method="post" action="regsuccess.jsp" name ="l"
onSubmit="return validateForm();">
<h1>Registration page</h1>
User:<input type="text" name="user"><br/>
Password:<input type="text" name="pass"><br/>
Email:<input type="text" name="eml"><br/>
Contact:<input type="text" name="cnt"><br/>
<input type="submit" value="Login">
</form>
</body>
</html>
24
Example

25
Continued..
regsuccess.jsp
<html>
<body bgcolor="orange">
<%
String user = request.getParameter("user");
String pwd = request.getParameter("pass");
String email = request.getParameter("eml");
String cont = request.getParameter("cnt");
%>
<center>
<h1>Your Registration is Successful</h1>
<h2>Your Account username: <td></td><%=user %><br><br>password: <td></td><%=
pwd %> <td> Email:</td><%=email %><br><br>Contact No: <td></td><%= cont
%><br></h2>
<p><tr> To login<td></td> <a href="login.jsp">click here</a> </tr></p>
</center>
</body>
</html>
26
Output

27
Expression Example
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
</HEAD>

<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
</UL>
</BODY>
</HTML>

28
Any Questions???

Copyright @ 2000 Jordan Anastasiade. All rights reserved. 29

You might also like