ch 6 Notes
ch 6 Notes
PROGRAMME: BCA
Web Technology-I
5.0 Objective
Output:
ii) The HTML tags like Head, Title and Body are frequently used.
The content within these tags will be delivered in a similar way to
client (Web browser).
iii) Scriptlets are the JSP elements that contain Java codes. <
%out.print (“Sample JSP code”) ;%> is an example of Scriptlet. Syntax
of scriptlet is: <%Executable java code%>. As the java code ends with
semicolon (;), so, the Scriptlets. out.print (“Sample JSP code”) is a
java statement, which prints“Sample JSP code ”.
Servlets
1. A Servlet is a Java program.
2. Servlets supports HTML tags.
3. It is used for developing the business layer of an enterprise
application.
4. Java developers create and maintain the servlets.
JSP
1. JSP program is a HTML code.
2. It supports the java statements by embedding it into HTML
using JSP tags.
3. Differentiate the presentation layer and business layer. So,
both can be developed separately easily of an enterprise
application
4. Web developers uses JSP frequently used for designing
websites.
5.3.4.1 Translation: In the initial step, the JSP file is converted the
web container converts the into a Java source file containing servlet
class definition by the web container. The JSP pages and tag files are
validated by the web container.
5.3.4.3 JSP Page Class Loading: The servlet class byte code is stacked
into the web container’s JVM software using class loader in this step.
5.3.4.6 JSP Page Service: The requests are now serviced by the
initialized servlet. The user request is processed by calling _jspService
() method by the web container. This method is called for each
request during its life cycle. The method takes HttpServletRequest
and HttpServletResponse parameters.
The time and date of the day is: <%= new java.util.Date () %>
5.3.5.4 The Comment Tag
This tag is used to ignore or hide some parts of the code. The syntax
is as follows:
<% - -comment -- %>
Output:
Total=10
Output:
Explanation
The “new java.util.Date ()” has processed the actual date and time of
the HTML on the browser. Few other examples:
Examples
• This example shows expression for converting lower case string
to upper case. Here is the code:
The Expression: Following expression is used for converting a string
to uppercase.
<%= new String(“Hello World”).to Uppercase () %>. A “string” object
with value “Hello World” is created. After that, a Java function
“.toUpperCase” is called to convert the string to upper case.
The HTML: Converting a string to uppercase: HELLO WORLD
• Mathematical expressions can also be used in JSP.
The Expression: Is 79 less than 65? <%= 79 <65 %>. The return type is
always string either going to be “true” or “false” of these Boolean
expressions.
The HTML: Is 79 less than 65? False
Output:
I love counting: 1
I love counting: 2
I love counting: 3
I love counting: 4
I love counting: 5
Explanation
In the above example, a basic h2 heading has been set up. Then, we
have a “for loop” in the scriptlet. Don’t put in a lot of code in a
scriptlet in JSP. It will be easily manageable and readable. Try
refactoring this code into different Java classes and make use of MVC
to keep it under control if we can’t find any other option.
In each iteration of the loop we are printing the “I really love
counting” and appending it with the integer value of the “i” printed
through the HTML.
Code:
<%!
String makeItLower(String data)
{
returndata.toLowerCase();
}
%>
Example:
Output:
Explanation
In the method declaration we are having our standard java method
with the return type of a String. We take a string as an argument and
return it converted to a lower case. Later we call this function
through a JSP Expression.
Attribute Description
Flush Whether or not the included asset
has its buffer removed is decided
by the boolean attribute before it is
included.
Page The predefined page which is to be
incorporated its relative URL is
found.
Table 5.2: Properties Associated with
<include> Action Example:
Give us a chance to characterize the two records main.jsp and
date.jsp which are as shown below. Following is the substance of the
record for date.jsp:
<html>
<head>
<title>Example of include action</title>
</head>
<body>
<center>
<h3> Example of “include action” </h3>
<jsp:include page = "date.jsp" flush = "true" />
</center>
</body>
</html>
Output:
Once any bean class to be loaded into the page has been loaded, we
can jsp: getProperty and use jsp: setProperty actions for retrieving
and modifying the properties of the bean. The table below lists the
attributes of useBean action
Attribute Description
Example
On testing the bean class:
/* File:
TestBean.java */
package action;
public class
TestBean {
private String message = "No message
specified"; public String getMessage() {
return(msg);
}
public void setMessage(String msg) {
this.message = msg;
}
}
Example:
On reusing the files date.jsp and main.jsp.The content of the date.jsp
file is:
Example:
<jsp:plugin type = "applet" codebase = "dir" code =
"Applet.class" width = "80" height = "100">
<jsp:param name = "fontcolor" value = "blue" />
<jsp:param name = "background" value = "red" />
<jsp:fallback>
Unable to initialize the Java Plugin
</jsp:fallback>
</jsp:plugin>
We try using some applet for this action. A new <fallback> element,
to specify a message can be used error string to be sent to the user
in case the component fails.
Output:
We can code the page directives at anyplace in our JSP page. The
guidelines to the container relating to JSP current page is provided by
the page directive. The directives of page are coded at top of the JSP
page by convention. The basic syntax of page directive is as follows:
The attributes associated with the page directive are listed below:
Attribute Purpose
Buffer A buffering model is specified for
the output stream.
autoFlush Servlet output buffer behavior is
controlled by this.
contentType It characterizes the character
encoding scheme.
errorPage This characterizes the URL of
another JSP that reports on Java
unchecked runtime exceptions
(special cases).
isErrorPage It shows if this JSP page is a URL
indicated by another JSP page's
errorPage characteristic.
Extends Determines a superclass that the
produced servlet must extend.
Import Indicates a bundle of packages or
classes for use in the JSP as the
Java import statement does for
Java classes.
Info Characterizes a string that can be
accessed with the servlet's
getServletInfo() method.
isThreadSafe Characterizes the threading model
for the generated servlet.
Language Characterizes the programming
language used in the JSP page.
Session Specifies whether or not the JSP
page participates in HTTP sessions.
isELIgnored Characterizes whether or not the
EL expression within the JSP page
will be ignored.
isScriptingEnabled Characterizes if the scripting
elements are allowed for use.
Table 5.4 Attributes of Page Directive
Some of the above attributes are being explained below:
Include Directive
Example
Including a typical header and footer with multiple pages of content
is a good example of include directive. Following three files have
been defined below: (a)header.jps,(b)footer.jsp, and (c)main.jsp.
header.jsp content is as follows:
<%!
int pageCount =
0; void
addCount() {
pageCount++;
}
%>
<% addCount(); %>
<html>
<head>
<title>An example of Include Directive </title>
</head>
<body>
<center>
<h2> An example of Include Directive </h2>
<p>You have visited this site <%= pageCount %>
times.</p> </center>
<br/><br/>
<br/><br/>
<center>
<p>Copyright © 2015</p>
</center>
</body>
</html>
main.jsp content is
as follows:
<%@ include file = "header.jsp" %>
<center>
<p>Thank you for visiting this page.</p>
</center>
<%@ include file = "footer.jsp" %>
Output:
An example of Include
Directive You have visited
this site 1 times.
Thank you for visiting this page