Advanced Java: Mca 4 Sem Unit Iv Part 1st

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

ADVANCED JAVA

MCA 4th sem


UNIT IV
Part 1st
 JSP APPLICATION DEVELOPMENT
 GENERATING DYNAMIC CONTENT:
 Dynamic contents means the contents that get changed based on the
user inputs or states of external system or on some runtime
conditions. JSP helps in handling such conditions. There are various
ways by which JSP handles the dynamic contents such as use of:
 Directive elements
 Java Beans
 Scripting elements
 Standard tag libraries
 Standard and custom actions
 Expression language.
 Directive Elements:
 Directive elements are used to specify the information about the page.
 Syntax:
 <%@ directivename attr1=”value1” attr2=”value2” %> The directive

names and attribute names are case sensitive.


 Examples of some directives are:

◦ Page
◦ Include
◦ Taglib
◦ Attribute
◦ Tag
◦ Variable
 Page directive:
 This directive can only be used in JSP pages, not tag files. It
defines page dependent attributes, such as scripting language,
error page, buffer requirements.
 Syntax:
 <%@ Page [autoflush=”true/false”]
[buffer=”skb/NNkb/None] [ContentType=”MIMEType”]
[errorPage=”page or ContextRelativePath”]
[extends=”classname”] [import=”packagelist”] [info=”info” ]
[isErrorPage=”true/false”] [isThreadSafe=”true/false”]
[language=”java/language”] [pageEncoding=”encoding”]
[session=”true/false”] %>
 Example:
 <%@ page language=”java” ContentType=”text/html” %>
 Include directive:
 
 It includes a static file, merging its content with the including

page before the combined results is converted to JSP page


implementation class.

Syntax:
 
 <%@ include file=”page or contextRelativePath” %> Example:
 <%@ include file=”home.html” %>
 
 Taglib directive:
 Declares a tag library, containing custom actions that are used in the page.
 Syntax: <%@
 
 taglib prefix=”prefix” [Uri=”tagliburi/ tagdir =”contextRelativePath”]
 
 %>
 
 Example:
 
 <%@ taglib prefix=”ora” Uri=”orataglib” %>
 
 <%@ taglib prefix=”mylib” tagdir=”/WEB-INF/tags/mylib” %>
 Attribute directive:
  
 This directive can only be used in tag files. It declares the
attributes that tag file supports.
  
 Syntax:
  
 <%@ attribute name=”attrname” [description=”desc”]
[required=”true/false”]
[fragment=”true/false”/type=”attrDataType”] %>
 Example:
  
 <%@ attribute name=”date” type=”java.util.Date” %>
 Tag directive:
 This directive can only be used in tag files. Syntax:
 <%@ tag [body-

content=”empty/scriptless/tagdependent”]
[description=”desc”] [display- name=”displayname”]
[dynamic-attributes=”attrcolvar”]
[import=”packagelist”] [language=”java/language”]
[page-encoding=”encoding”] %>
 Example:
 <%@ tag body-content=”empty” %>
 Variable directive:
 It is similar to variable declarations.

Syntax:
 <%@ variable name-given=”attrname”/name-from-
attribute=”attrname” alias=”varname”
 %>
Sample program for directive elements: Scripting.jsp file:
<%@ page import="java.util.Date"%>
 <html>
 <head><title>Scripting elements example</title></head>
 <body>
 <%! int count=0; %>
 <% count++;
 out.println("You are accessing this page for "+count+ "times");

out.println(new Date().toString());
%> 
<%= count %>
 </body>
 </html> Web.xml file:
<web-app>
 </web-app>
Output:
Java Beans

Java beans are reusable components. We can use simple java beans in a JSP. This helps us
in keeping the business logic separate from presentation logic. Beans are used in the JSP
pages as instance of a class. We must specify the scope of the bean in the JSP page. Here
scope of the bean means the range time span of the bean for its existence in the page.
There are various scopes using which the bean can be used in the JSP page.
Page scope: The bean object gets disappeared as soon as the current page gets discarded.

The default scope for a bean in the JSP page is page scope.
Request scope: the bean object remains in the existence as long as the request object is

present.
Session scope: A session can be defined as the specific period of time the user spends in

browsing the site. Then the time spent by the user in starting and quitting the site is one
session.
Application scope: during application scope the bean will gets stored to ServletContext.

Hence particular bean is available to all the servlets in the same web application.
Application scope is the broadest scope provided by the JSP.
Object Scopes

 Before we look at JSP syntax and semantics, it is


important to understand the scope or visibility of Java
objects within JSP pages that are processing a request.

Objects may be created implicitly using JSP directives,


explicitly through actions,or, in rare cases, directly using
scripting code. The instantiated objects can be associated
with a scope attribute defining where there is a reference
to the object and when that reference is removed. The
following diagram indicates the various scopes that can be
associated with a newly created object:
Getname.html file:
 
<HTML>
 
<BODY>
 
<FORM METHOD=POST ACTION="SaveName.jsp">
 
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's your e-mail
address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR> What's your age? <INPUT
TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
 
</FORM>
 
</BODY>
 
</HTML>
 Nextpage.jsp file:
 
 <jsp:useBean id="user" class="user.UserData" scope="session"/>
 
 <HTML>
 
 <BODY>
 
 You entered<BR>
 
 Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR>
Age: <%= user.getAge() %><BR>
 </BODY>
 
 </HTML>
 Savename.jsp file:
  <jsp:useBean id="user" class="user.UserData"
scope="session"/>
 <jsp:setProperty name="user" property="*"/>
  <HTML>
  <BODY>
  <A HREF="NextPage.jsp">Continue</A>
  </BODY>
  </HTML>
  Web.xml file:
  <web-app>
  </web-app>
 Output:
 Scripting elements:
 Scripting allows us to embed java code in a JSP page and scripting

elements (scriptlets, declaration and expressions) allows us to do scripting.


Scripting elements are executed at request processing time and are used for
a variety of purposes including manipulation of objects, perform
calculation on runtime variables values, etc.
 Standard tag libraries:
 JSTL stands for JSP standard tag libraries. This tag library is useful for

performing some common task such as condition execution, loop


execution, data processing and so on. JSTL allows the programmer to
embed the logic in JSP page without using java code. The tag library
makes use of some standard set of tags. To install JSTL in tomcat just copy
the jstl.jar and standard.jar files in the lib folder of WEB-INF directory to
your tomcat. Some tags are : <c:out>, <c:set>, <c:if>, <c:choose> etc.
 Standard and custom actions:
 The standard actions are those actions that can be defined by
the JSP specification itself. Following are some of the
standard actions:
Action element Description

<jsp:usebean> This tag is used to instantiate the object of java bean

<jsp:setProperty> This tag is used to set the property value for java bean

<jsp:getProperty> This tag is used to get the property value from java bean

<jsp:include> This tag works as a sub-routine. It includes the response from


the servlet or JSP when the request is being processed.

<jsp:param> For adding the specific parameter to the request this tag is used.
<jsp:forward> This tag helps in forwarding the current request to
servlet or to JSP page.

<jsp:plugin> This tag is used to generate the HTML code and to embed
the applet into
it.

<jsp:attribute> This tag sets the value of action attribute.

<jsp:element> This tag generates the XML elements dynamically.

<jsp:text> This tag is used to handle template text. When JSP pages are
written as
XML documents then this tag is used.

<jsp:body> This tag is used to set the body element.

Custom actions allow us to create user- defined tags.


 Expression language: 
 Expression language is all about generating dynamic content,
without indulging in the complexity of a programming
language. Hence it is used to write script-free pages. It is used
by the JSP programmer in order to avoid the usage of java
code for accessing data. The EL statements are always used
within {…..} along with the prefix $.
IMPLICIT JSP OBJECTS

 
 The objects which we access in our JSP page, without
any explicit declaration are called as implicit objects.
Implicit objects are exposed by the JSP container and can
be seen in the generated servlet of a JSP page.
 Implicit objects are advantageous because, they don’t
require the JSP authors to explicitly declare and initialize
a few of the servlet objects, which is a difficult task.
Object Class/Interface Description

application javax.servlet.ServletConfig Represents the context for the


JSP page servlet, in which
session javax.servlet.http.HttpSession This variable is used to access
current client’s session.
request javax.servlet.http.HttpServletRequest It provides the method for
accessing the information made
by current request.

Response javax.servlet.http.HttpServletResponse It provides the methods related


to adding cookies, session, setting
headers.

PageContent javax.servlet.jsp.PageContent It provides access to several JSP


attributes.
Page Java.lang.object This variable is assigned to
instance of JSP implementation
class.

Out Javax.Servlet.jsp.JspWriter It provides methods related to I/O.

Exception Java.lang.Throwable This object is used for handling error


pages and contain information about
runtime
errors.

config Javax.servlet.ServletConfig It helps in passing the


information to the servlet or JSP page
during initialization.

You might also like