0% found this document useful (0 votes)
22 views

Day 16

The document outlines the curriculum for the Advanced Java course at the Domain Summer Winning Camp 2024, focusing on JSP (Java Server Pages) for creating dynamic web pages. It covers prerequisites, objectives, outcomes, and key concepts such as JSP tags, implicit objects, and exception handling. Additionally, it provides practical examples and coding syntax for implementing JSP in web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Day 16

The document outlines the curriculum for the Advanced Java course at the Domain Summer Winning Camp 2024, focusing on JSP (Java Server Pages) for creating dynamic web pages. It covers prerequisites, objectives, outcomes, and key concepts such as JSP tags, implicit objects, and exception handling. Additionally, it provides practical examples and coding syntax for implementing JSP in web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

DEPARTMENT OF COMPUTER SCIENCE

& ENGINEERING
Domain Summer Winning Camp 2024
Subject Name: Advanced Java
Day:16
Topics Covered: JSP tags, Creating dynamic web pages
using JSP
Handling Building web application using JSP

DISCOVER . LEARN . EMPOWER


1
These points related to Day wise topic should be included :

Prerequisites:
• Strong understanding of HTML, CSS, and JavaScript.
• Familiarity with Java programming language fundamentals.
• Basic knowledge of web development concepts such as HTTP, web
servers, and client-server architecture.
Objectives:
• Utilize JSP tags to embed Java code and dynamic content within
HTML pages.
• Create dynamic web pages using JSP to generate content based
on user input or database queries.
• Develop robust web applications using JSP for handling user
requests, managing session data, and interacting with databases.
• Follow best practices in JSP development to ensure code
readability, performance, and security.
Outcomes:
• Proficiency in using JSP tags to perform common tasks such as
variable interpolation, flow control, and including other resources.
• Capability to create dynamic web pages that respond to user
2
input and provide personalized content.
• Implementation of custom JSP tags to encapsulate complex logic
and promote code reusability.
What is JSP?

JSP stands for Java Server Pages, a technology invented by Sun


to allow the easy creation of server side HTML pages.

JSP lies in the presentation tier on the web server, with the main
responsibility of generating HTML content that needs to be
served to the browser. It also has the additional responsibility
of pass on the requests to the backend through the JavaBeans,
as and when required.
Why JSP?

1. High performance
2. Convenient to code and maintain as against servlets
3. Separates presentation from content
4. Powered by Java
– Has access to all Java APIs
– Has access to all J2EE APIsa
– Inherent Platform independence
Creating JSP in Eclipse IDE with Tomcat server

• Create a Dynamic web project


• create a jsp
• start tomcat server and deploy the project
The JSP API

• The JSP API consists of two packages:


1.javax.servlet.jsp
2.javax.servlet.jsp.tagext

• javax.servlet.jsp package
• The javax.servlet.jsp package has two interfaces and classes.
The two interfaces are as follows:
1.JspPage
2.HttpJspPage
The classes are as follows:
• JspWriter
• PageContext
• JspFactory
• JspEngineInfo
• JspException
• JspError
• The JspPage interface
• According to the JSP specification, all the generated servlet classes must
implement the JspPage interface. It extends the Servlet interface. It provides two
life cycle methods.

Methods of JspPage interface


• public void jspInit(): It is invoked only once during the life cycle of the JSP when
JSP page is requested firstly. It is used to perform initialization. It is same as the
init() method of Servlet interface.
• public void jspDestroy(): It is invoked only once during the life cycle of the JSP
before the JSP page is destroyed. It can be used to perform some clean up
operation.
• The HttpJspPage interface
• The HttpJspPage interface provides the one life cycle method of JSP. It extends
the JspPage interface.

Method of HttpJspPage interface:


• public void _jspService(): It is invoked each time when request for the JSP page
comes to the container. It is used to process the request. The underscore _
JSP Scriptlet tag (Scripting elements)

JSP Scripting elements


The scripting elements provides the ability to insert java code
inside the jsp.
There are three types of scripting elements:
• scriptlet tag
• expression tag
• declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java source code in JSP.
Syntax is as follows:
<% java source code %>
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
JSP expression tag

• The code placed within JSP expression


tag is written to the output stream of the response.
So you need not write out.print() to write data. It is
mainly used to print the values of variable or
method.
• Syntax of JSP expression tag
1.<%= statement %>
• Example of JSP expression tag
• In this example of jsp expression tag, we are simply
displaying a welcome message.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
• In this example, we are printing the username using the
expression tag. The index.html file gets the username and
sends the request to the welcome.jsp file, which displays the
username.
• File: index.jsp
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>
JSP Declaration Tag

The JSP declaration tag is used to declare fields and methods.


The code written inside the jsp declaration tag is placed outside the
service() method of auto generated servlet.
So it doesn't get memory at each request.

Syntax of JSP declaration tag


The syntax of the declaration tag is as follows:
<%! field or method declaration %>
• In this example of JSP declaration tag, we are declaring the field and printing the value of the declared field using
the jsp expression tag.
index.jsp
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
• Example of JSP declaration tag that declares method
• In this example of JSP declaration tag, we are defining the method which returns the cube of given number and calling
this method from the jsp expression tag. But we can also use jsp scriptlet tag to call the declared method.
index.jsp
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
JSP Implicit Objects
There are 9 jsp implicit objects. These objects are created by the web
container that are available to all the jsp pages.
The available implicit objects are out, request, config, session, application
etc.
A list of the 9 implicit objects is given below:

Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
JSP out implicit object

For writing any data to the buffer, JSP provides an implicit object named out. It is the
object of JspWriter. In case of servlet you need to write.
PrintWriter out=response.getWriter();
But in JSP, you don't need to write this code.
In this example we are simply displaying date and time.
index.jsp
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
JSP request implicit object
• The JSP request is an implicit object of type HttpServletRequest i.e.
created for each jsp request by the web container.
• It can be used to get request information such as parameter, header
information, remote address, server name, server port, content type,
character encoding etc.
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
• JSP response implicit object

• In JSP, response is an implicit object of type HttpServletResponse.


• The instance of HttpServletResponse is created by the web container for
each jsp request.
• It can be used to add or manipulate response such as redirect response to
another resource, send error etc.
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
response.sendRedirect("http://www.google.com");
%>
JSP config implicit object
• In JSP, config is an implicit object of type ServletConfig.
• This object can be used to get initialization parameter for a particular JSP page. The
config object is created by the web container for each jsp page.
Generally, it is used to get initialization parameter from the web.xml file.
• Example of config implicit object: web.xml file
index.html <web-app>
<servlet>
<form action="welcome"> <servlet-name>sonoojaiswal</servlet-
<input type="text" name="uname"> name>
<jsp-file>/welcome.jsp</jsp-file>
<input type="submit" value="go"><br/>
</form> <init-param>
<param-name>dname</param-
name>
welcome.jsp <param-
value>sun.jdbc.odbc.JdbcOdbcDriver</
<% param-value>
</init-param>
out.print("Welcome "+request.getParameter("uname"));

</servlet>
String driver=config.getInitParameter("dname");
<servlet-mapping>
out.print("driver name is="+driver); <servlet-name>sonoojaiswal</servlet-
%> name>
<url-pattern>/welcome</url-pattern>
• JSP application implicit object
• In JSP, application is an implicit object of type ServletContext.
• The instance of ServletContext is created only once by the web container when application
or project is deployed on the server.
• This object can be used to get initialization parameter from configuaration file (web.xml). It
can also be used to get, set or remove attribute from the application scope.
• This initialization parameter can be used by all jsp pages. web.xml file
<web-app>
index.html <servlet>
<servlet-name>sonoojaiswal</
<form action="welcome"> servlet-name>
<input type="text" name="uname"> <jsp-file>/welcome.jsp</jsp-
file>
<input type="submit" value="go"><br/> </servlet>
</form>
<servlet-mapping>
welcome.jsp <servlet-name>sonoojaiswal</
servlet-name>
<% <url-pattern>/welcome</url-
out.print("Welcome "+request.getParameter("uname")); pattern>
</servlet-mapping>

<context-param>
String driver=application.getInitParameter("dname"); <param-name>dname</
out.print("driver name is="+driver); param-name>
<param-
%> value>sun.jdbc.odbc.JdbcOdbcDri
ver</param-value>
</context-param>
session implicit object
In JSP, session is an implicit object of type HttpSession.
The Java developer can use this object to set, get or remove attribute or to get session
information.
Example of session implicit object

Index.html Welcome.jsp
<html> <html>
<body> <body>
<form action="welcome.jsp"> <%
<input type="text" name="uname"> String name=request.getParameter("uname");
<input type="submit" value="go"><br/>
</form> out.print("Welcome "+name);
</body> session.setAttribute("user",name);
</html> <a href="second.jsp">second jsp page</
a> %>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>
pageContext implicit object

In JSP, pageContext is an implicit object of type PageContext class.The pageContext object


can be used to set, get or remove attribute from one of the following scopes:
page
request
session
application
In JSP, page scope is the default scope.
Example of pageContext implicit object
index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
<a href="second.jsp">second jsp page</a>
%>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
out.print("Hello "+name);
%>
</body>
</html>
• page implicit object:

• In JSP, page is an implicit object of type Object


class. This object is assigned to the reference of
auto generated servlet class. It is written as:
• Object page=this;
• For using this object it must be cast to Servlet
type.For example:
<% (HttpServlet)page.log("message"); %>
• Since, it is of type Object it is less used because you
can use this object directly in jsp.For example:
<% this.log("message"); %>
• exception implicit object

In JSP, exception is an implicit object of type java.lang. Throwable


class. This object can be used to print the exception. But it can only
be used in error pages.
It is better to learn it after page directive. Let's see a simple example:

Example of exception implicit object:

error.jsp
<%@ page isErrorPage="true" %>
<html>
<body>

Sorry following exception occured:<%= exception %>

</body>
• In JSP, there are two ways to perform exception
handling:
1.By errorPage and isErrorPage attributes of page
directive
2.By <error-page> element in web.xml file
Example of exception handling in jsp by the elements of page
directive

In this case, you must define and create a page to handle the
exceptions, as in the error.jsp page. The pages where may occur
exception, define the errorPage attribute of page directive, as in the
process.jsp page.

There are 3 files:


index.jsp for input values
process.jsp for dividing the two numbers and displaying the result
error.jsp for handling the exception
index.jsp
<form action="process.jsp">
No1:<input type="text" name="n1" /><br/><br/>
No1:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/>
</form>

process.jsp
<%@ page errorPage="error.jsp" %>
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>

error.jsp
<%@ page isErrorPage="true" %>
<h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
• Example of exception handling in jsp by specifying
the error-page element in web.xml file

• This approach is better because you don't need to


specify the errorPage attribute in each jsp page.
Specifying the single entry in the web.xml file will
handle the exception. In this case, either specify
exception-type or error-code with the location
element. If you want to handle all the exception,
you will have to specify the java.lang.Exception in
the exception-type element. Let's see the simple
example:
• There are 4 files:
• web.xml file for specifying the error-page element
• index.jsp for input values
• process.jsp for dividing the two numbers and displaying
the result
• error.jsp for displaying the exception

• 1) web.xml file if you want to handle any exception


<web-app>

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>

</web-app>
• web.xml file if you want to handle the
exception for a specific error code
<web-app>

<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>

</web-app>
JSP directives

The jsp directives are messages that tells the web container how to translate a
JSP page into the corresponding servlet.

There are three types of directives:


• page directive
• include directive
• taglib directive

Syntax of JSP Directive


<%@ directive attribute="value" %>
JSP page directive
The page directive defines attributes that apply to an entire JSP page.

Syntax of JSP page directive


<%@ page attribute="value" %>

Attributes of JSP page directive


• import
• contentType
• buffer
• language
• isELIgnored
• pageEncoding
• errorPage
• isErrorPage
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 of import attribute
<html>
<body>

<%@ page import="java.util.Date" %>


Today is: <%= new Date() %>

</body>
</html>
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".

• Example of contentType attribute


<html>
<body>

<%@ page contentType=application/msword %>


Today is: <%= new java.util.Date() %>

</body>
</html>
extends
• The extends attribute defines the parent class that will be inherited by the generated
servlet. It is rarely used.
info
• This attribute simply sets the information of the JSP page which is retrieved later
by using getServletInfo() method of Servlet interface.

• Example of info attribute


<html>
<body>

<%@ page info="composed by xyz" %>


Today is: <%= new java.util.Date() %>

</body>
</html>

• The web container will create a method getServletInfo() in the resulting servlet.For
example:
public String getServletInfo() {
return "composed by xyz";
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 of buffer attribute


<html>
<body>

<%@ page buffer="16kb" %>


Today is: <%= new java.util.Date() %>

</body>
</html>

language

The language attribute specifies the scripting language used in the JSP page. The
default value is "java".
• isThreadSafe
• Servlet and JSP both are multithreaded.If you want
to control this behaviour of JSP page, you can use
isThreadSafe attribute of page directive.The value
of isThreadSafe value is true.If you make it false,
the web container will serialize the multiple
requests, i.e. it will wait until the JSP finishes
responding to a request before passing another
request to it.If you make the value of isThreadSafe
attribute like:
• <%@ page isThreadSafe="false" %>
errorPage
• The errorPage attribute is used to define the error page, if
exception occurs in the current page, it will be redirected to
the error page.

Example of errorPage attribute


//index.jsp
<html>
<body>

<%@ page errorPage="myerrorpage.jsp" %>

<%= 100/0 %>

</body>
</html>
isErrorPage

The isErrorPage attribute is used to declare that the current page is the error
page.

Note: The exception object can only be used in the error page.
Example of isErrorPage attribute
//myerrorpage.jsp
<html>
<body>

<%@ page isErrorPage="true" %>

Sorry an exception occured!<br/>


The exception is: <%= exception %>

</body>
</html>
Jsp Include Directive

The include directive is used to include the contents of any resource it may be
jsp file, html file or text file.
The include directive includes the original content of the included resource at
page translation time (the jsp page is translated only once so it will be better
to include static resource).

Advantage of Include directive


• Code Reusability

Syntax of include directive


<%@ include file="resourceName" %>
• Example of include directive
• In this example, we are including the content of
the header.html file. To run this example you
must create an header.html file.
<html>
<body>

<%@ include file="header.html" %>

Today is: <


%= java.util.Calendar.getInstance().getTime() %>

</body>
</html>
Taglib Directive
This directive basically allows user to use Custom tags in JSP. we shall discuss about
Custom tags in detail in coming JSP tutorials. Taglib directive helps you to
declare custom tags in JSP page.
Syntax of Taglib Directive:
• <%@taglib uri ="taglibURI" prefix="tag prefix"%>Where URI is uniform resource
locator, which is used to identify the location of custom tag and tag prefix is a
string which can identify the custom tag in the location identified by uri.

Example of Targlib:
<%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag" %>
<html>
<body>
<demotag:welcome/>
</body>
</html>
• As you can see that uri is having the location of custom tag library and prefix is
identifying the prefix of custom tag.
Note: In above example – <demotag: welcome> has a prefix demotag.
action Tags
• There are many JSP action tags or elements. Each JSP action tag
is used to perform some specific tasks.
• The action tags are used to control the flow between
pages and to use Java Bean. The Jsp action tags are given
below.
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.

jsp:include includes another resource.


jsp:useBean creates or locates bean object.
jsp:setPropert sets the value of property in bean object.
y
jsp:getPropert prints the value of property of the bean.
y
jsp:plugin embeds another components such as applet.

jsp:param sets the parameter value. It is used in forward and include


mostly.
jsp:fallback can be used to print the message if plugin is working. It is
jsp:forward action tag

jsp:forward action tag

The jsp:forward action tag is used to forward the request to another resource
it may be jsp, html or another resource.
Syntax of jsp:forward action tag without parameter
<jsp:forward page="relativeURL | <%= expression %>" />
Syntax of jsp:forward action tag with parameter
<jsp:forward page="relativeURL | <%= expression %>">
<jsp:param name="parametername" value="parametervalue
| <%=expression%>" />
</jsp:forward>
Example of jsp:forward action tag with parameter
In this example, we are forwarding the request to the printdate.jsp
file with parameter and printdate.jsp file prints the parameter value
with date and time.
index.jsp
<html>
<body>
<h2>this is index page</h2>

<jsp:forward page="printdate.jsp" >


<jsp:param name="name" value="javatpoint.com" />
</jsp:forward>

</body>
</html>

printdate.jsp
<html>
<body>

<
% out.print("Today is:"+java.util.Calendar.getInstance().getTime());
%>
<%= request.getParameter("name") %>
• jsp:include action tag
• The jsp:include action tag is used to
include the content of another resource it
may be jsp, html or servlet.
• The jsp include action tag includes the
resource at request time so it is better
for dynamic pages because there
might be changes in future.
• The jsp:include tag can be used to
include static as well as dynamic page
Difference between jsp include directive and
include action

JSP include directive JSP include action

includes resource at translation time. includes resource at request time.

better for static pages. better for dynamic pages.

includes the original content in the calls the include method.


generated servlet.
include action

Syntax of jsp:include action tag without parameter

<jsp:include page="relativeURL | <%= expression %>" />

Syntax of jsp:include action tag with parameter

<jsp:include page="relativeURL | <%= expression %>">


<jsp:param name="parametername" value="parametervalue | <%=expression%>" />
</jsp:include>
Example of jsp:include action tag without
parameter

In this example, index.jsp file includes the content of the printdate.jsp file.

File: index.jsp

<h2>this is index page</h2>

<jsp:include page="printdate.jsp" />

<h2>end section of index page</h2>

File: printdate.jsp

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>


• JavaBean
• A JavaBean is a Java class that should follow the following conventions:
• It should have a no-arg constructor.
• It should be Serializable.
• It should provide methods to set and get the values of the properties, known as getter
and setter methods.
• Why use JavaBean?
• According to Java white paper, it is a reusable software component. A bean encapsulates
many objects into one object so that we can access this object from multiple places.
Moreover, it provides easy maintenance.

• Simple example of JavaBean class


//Employee.java
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
• How to access the JavaBean class?
• To access the JavaBean class, we should
use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//
object is created
e.setName("Arjun");//
setting value to the object
System.out.println(e.getName());
}}
• JavaBean Properties

• A JavaBean property is a named feature that can be accessed by the user of the
object. The feature can be of any Java data type, containing the classes that you
define.
• A JavaBean property may be read, write, read-only, or write-only. JavaBean
features are accessed through two methods in the JavaBean's implementation
class:
1. getPropertyName ()
• For example, if the property name is firstName, the method name would be
getFirstName() to read that property. This method is called the accessor.
2. setPropertyName ()
• For example, if the property name is firstName, the method name would be
setFirstName() to write that property. This method is called the mutator.
• jsp:useBean action tag
• The jsp:useBean action tag is used to locate or instantiate a bean class. If bean
object of the Bean class is already created, it doesn't create the bean depending
on the scope. But if object of bean is not created, it instantiates the bean.
• Syntax of jsp:useBean action tag
1. <jsp:useBean id= "instanceName" scope= "page | request | session | application"

2. class= "packageName.className" type= "packageName.className"


3. beanName="packageName.className | <%= expression >" >
4. </jsp:useBean>
• Attributes and Usage of jsp:useBean action tag
1. id: is used to identify the bean in the specified scope.
2. scope: represents the scope of the bean. It may be page, request, session or
application. The default scope is page.
1. page: specifies that you can use this bean within the JSP page. The default scope is page.
2. request: specifies that you can use this bean from any JSP page that processes the same
request. It has wider scope than page.
3. session: specifies that you can use this bean from any JSP page in the same session
whether processes the same request or not. It has wider scope than request.
4. application: specifies that you can use this bean from any JSP page in the same
application. It has wider scope than session.
3. class: instantiates the specified bean class (i.e. creates an object of the bean
class) but it must have no-arg or no constructor and must not be abstract.
4. type: provides the bean a data type if the bean already exists in the scope. It is
mainly used with class or beanName attribute. If you use it without class or
beanName, no bean is instantiated.
5. beanName: instantiates the bean using the java.beans.Beans.instantiate()
method.
• Calculator.java (a simple Bean class)
package com.xyz;
public class Calculator{

public int cube(int n){return n*n*n;}

}
index.jsp file
<jsp:useBean id="obj" class="com.xyz.Calculator"/
>

<%
int m=obj.cube(5);
out.print("cube of 5 is "+m);
%>
• jsp:setProperty and jsp:getProperty
action tags
• The setProperty and getProperty action tags are
used for developing web application with Java
Bean. In web devlopment, bean class is mostly used
because it is a reusable software component that
represents data.

• The jsp:setProperty action tag sets a property value


or values in a bean using the setter method.
• Syntax of jsp:setProperty action tag
<jsp:setProperty name="instanceOfBean" property= "*" |
property="propertyName" param="parameterName" |
property="propertyName" value="{ string | <%= expression %>}"
/>
• Example of jsp:setProperty action tag if you have to set all the values of incoming
request in the bean
<jsp:setProperty name="bean" property="*" />
• Example of jsp:setProperty action tag if you have to set value of the incoming specific
property
<jsp:setProperty name="bean" property="username" />
• Example of jsp:setProperty action tag if you have to set a specific value in the property
<jsp:setProperty name="bean" property="username" value="Kumar" />
• jsp:getProperty action tag

• The jsp:getProperty action tag returns the value of the property.

• Syntax of jsp:getProperty action tag


<jsp:getProperty name="instanceOfBean" property="propertyName" />
Simple example of jsp:getProperty action tag
<jsp:getProperty name="obj" property="name" />
• Example of bean development in JSP
• In this example there are 3 pages:
index.html for input of values
welocme.jsp file that sets the incoming values to the bean object and prints the one value
User.java bean class that have setter and getter methods
index.html
<form action="process.jsp" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
Email:<input type="text" name="email"><br>
<input type="submit" value="register">
</form>
process.jsp
<jsp:useBean id="u" class="org.sssit.User"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>
Record:<br>
<jsp:getProperty property="name" name="u"/><br>
<jsp:getProperty property="password" name="u"/><br>
<jsp:getProperty property="email" name="u" /><br>
User.java
package org.sssit;
public class User {
private String name,password,email;
//setters and getters
}
• process.jsp
<jsp:useBean id="u" class="org.sssit.Use
r"></jsp:useBean>
<%
String name="arjun";
%>
<jsp:setProperty property="name" name=
"u" value="<%=name %>"/>

Record:<br>
<jsp:getProperty property="name" name=
"u"/><br>
• Displaying applet in JSP (jsp:plugin action tag)

• The jsp:plugin action tag is used to embed applet in the jsp file. The jsp:plugin action tag downloads plugin
at client side to execute an applet or bean.
Syntax of jsp:plugin action tag
<jsp:plugin type= "applet | bean" code= "nameOfClassFile"
codebase= "directoryNameOfClassFile"
</jsp:plugin>
• Example of displaying applet in JSP
• In this example, we are simply displaying applet in jsp using the jsp:plugin tag. You must have
MouseDrag.class file (an applet class file) in the current folder where jsp file resides. You may simply
download this program that contains index.jsp, MouseDrag.java and MouseDrag.class files to run this
application.
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mouse Drag</title>
</head>
<body bgcolor="khaki">
<h1>Mouse Drag Example</h1>

<jsp:plugin align="middle" height="500" width="500"


type="applet" code="MouseDrag.class" name="clock" codebase="."/>

</body>
</html>
Placement Related Questions:
• What are JSP tags, and how do they differ from HTML tags?
• Explain the difference between standard JSP tags and custom JSP tags.
Provide examples of each.
• How do you include another JSP file within a JSP page? What are the benefits
of using this approach?
• Describe the various types of JSP directives and their purposes.
• What is the purpose of Expression Language (EL) in JSP? How do you use
EL to access and manipulate data?
• How do you handle form submissions in a JSP page? Walk me through the
process of receiving form data and processing it in a JSP.
• Explain the concept of JSP Expression Language (EL) and its advantages over
using scriptlets in JSP pages.
• How do you use JSP standard actions to perform common tasks such as
including files, iterating over collections, and handling exceptions?
• Discuss the role of JSP custom tags in web development. Provide an example
of when you would use a custom tag and how you would implement it.
• What are the best practices for building web applications using JSP? How do
you ensure code maintainability and performance in your JSP-based projects?
Worksheets
• https://leetcode.com/problems/remove-k-digits/description/
• https://leetcode.com/problems/string-compression/description/
• https://leetcode.com/problems/occurrences-after-bigram/
• https://tests.mettl.com/authenticateKey/5106dfd
• https://www.hackerrank.com/challenges/pattern-syntax-
checker/problem
• https://leetcode.com/problems/trapping-rain-water/description/
• https://leetcode.com/problems/sort-colors/description/
• https://leetcode.com/problems/word-pattern/description/
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://www.youtube.com/watch?v=NQ7xKNULkTk

Reference Links:
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://docs.oracle.com/javaee/5/tutorial/doc/bnaou.html
https://beginnersbook.com/2013/05/jsp-tutorial-scriptlets/
https://www.javatpoint.com/jsp-scriptlet-tag
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://beginnersbook.com/2013/11/jsp-declaration-tag/
THANK YOU

You might also like