JSPTags
JSPTags
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
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
Example:
10
Predefined Variable – Implicit
Objects
request – Object of HttpServletRequest (request parameters, HTTP headers,
cookies
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> </p>
Syntax
<% code fragment %>
<%
String var1 = request.getParameter("name");
out.println(var1);
%>
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
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"
%>
22
Example
Develop an application Control transfer from one JSP
page to Other.
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???