🕸️
Web Development
 
UNIT 1
UNIT 2
UNIT 3
XML
J2EE
  • J2EE : Java 2 Platform Enterprise Edition
  • It's basically a wrapper over Java Standard Edition
  • Provides platform for developers with enterprise features such as Distributed Computing and Web Services.
  • J2EE applications are usually run on microservices
  • It is used for developing, building and deploying Web-based enterprise applications online.
  • The J2EE platform consists of a set of services, APIs, and protocols that provide the functionality for developing multitiered, Web-based applications.
 
 
J2EE Architectures
Excerpt from Book
Sir's class slides
J2EE Features
Java™ Database Connectivity (JDBC™) API [ Next topic ]
  • The standard way to connect to a database from a J2EE application or module is through a JDBC driver.
Transaction Service
  • The purpose of a transaction is to ensure that data is updated in an all-or-nothing fashion in order to preserve data integrity.
  • The transaction service provides transactional resource managers for the JDBC API, the JMS API, and resource adapters (connector modules)
Java Naming and Directory Interface™ (JNDI) API
  • The JNDI API allows application components and clients to look up distributed resources, services, and EJB™ components. The J2EE resources described in this guide are made available through the JNDI API
Java™ Message Service (JMS) API
  • The JMS API provides a common way for J2EE applications and modules to create, send, receive, and read messages in a distributed environment.
JavaMail™ API
  • The JavaMail API allows J2EE applications to create, send, receive, and read mail messages. The JavaMail API includes support for the IMAP4, POP3, and SMTP mail protocols. JavaMail sessions are made available through the JNDI API.
 
Web Server Vs Application Server, what's the difference ?
 
Web Server
  • Web server contains only web or servlet container. It can be used for servlet, jsp, struts, jsf etc. It can't be used for EJB.
  • It is a computer where the web content can be stored. In general web server can be used to host the web sites but there also used some other web servers also such as FTP, email, storage, gaming etc.
  • Examples of Web Servers are: Apache Tomcat and Resin.
 
It can respond to the client request in either of the following two possible ways:
  • Generating response by using the script and communicating with database.
  • Sending file to the client associated with the requested URL.
 
The block diagram representation of Web Server is shown below:
 
notion image
Important points
  • If the requested web page at the client side is not found, then web server will sends the HTTP response: Error 404 Not found.
  • When the web server searching the requested page if requested page is found then it will send to the client with an HTTP response.
  • If the client requests some other resources then web server will contact to application server and data is store for constructing the HTTP response.
Application Server
  • Application server contains Web and EJB containers. It can be used for servlet, jsp, struts, jsf, ejb etc.
  • It is a component based product that lies in the middle-tier of a server centric architecture.
  • It provides the middleware services for state maintenance and security, along with persistence and data access.
  • It is a type of server designed to install, operate and host associated services and applications for the IT services, end users and organizations.
 
notion image
Examples
The Example of Application Servers are:
  1. JBoss: Open-source server from JBoss community.
  1. Glassfish: Provided by Sun Microsystem. Now acquired by Oracle.
  1. Weblogic: Provided by Oracle. It more secured.
  1. Websphere: Provided by IBM.
 
notion image
 
 
 
notion image
Resources
 
Database Programming with JDBC
 
  • JDBC stands for Java Database Connectivity
  • It's a JAVA API to connect and execute the query with a database.
  • It's a part of the JAVA SE (Standard Edition)
  • JDBC API uses JDBC Drivers to connect with the database
Types of JDBC Drivers
  • JDBC-ODBC Bridge Driver,
  • Native Driver,
  • Network Protocol Driver, and
  • Thin Driver
  • with the help of JDBC API, we can save, update, delete and fetch data from the database
 
 
notion image
 
  • The java.sql package contains classes and interfaces for JDBC API
Popular Interfaces of JDBC API present in the java.sql package
  • Driver interface
  • Connection interface
  • Statement interface
  • PreparedStatement interface
  • CallableStatement interface
  • ResultSet interface
  • ResultSetMetaData interface
  • DatabaseMetaData interface
  • RowSet interface
Popular Classes of JDBC API present in the java.sql package
  • DriverManager class
  • Blob class
  • Clob class
  • Types class
Why Use JDBC ?
  • Previously ODBC API (which uses ODBC Drivers) written in C was used
  • So Java made its own API written in obviously Java to make it cohesive with the ecosystem
 
JDBC Drivers Deep Dive
  • JDBC Driver is a software component that enables java application to interact with the database
 
Types of JDBC Drivers
JDBC-ODBC Bridge Driver,

1) JDBC-ODBC bridge driver

notion image

In Java 8, the JDBC-ODBC Bridge has been removed.

Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that you use JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.

Advantages:

  • easy to use.
  • can be easily connected to any database.

Disadvantages:

  • Performance degraded because JDBC method call is converted into the ODBC function calls.
  • The ODBC driver needs to be installed on the client machine.

Native Driver,

2) Native-API driver

notion image

Advantage:

  • performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:

  • The Native driver needs to be installed on each client machine.
  • The Vendor client library needs to be installed on client machine.
Network Protocol Driver, and

3) Network Protocol driver

The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.
notion image

Advantage:

  • No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.

Disadvantages:

  • Network support is required on client machine.
  • Requires database-specific coding to be done in the middle tier.
  • Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier.
Thin Driver

4) Thin driver

notion image

Advantage:

  • Better performance than all other drivers.
  • No software is required at client side or server side.

Disadvantage:

  • Drivers depend on the Database.
 
Sir's Slides
Resources
UNIT 4
Servlets
Terminology you need to understand
Web Server
  • Web server contains only web/servlet container.
  • It can be used for servlet, jsp, struts, jsf etc. It can't be used for EJB (Enterprise Java Bean needs application server).
  • It is a computer where the web content can be stored and installed with web server software like apache.
  • In general web server can be used to host the web sites but there also used as some other web servers also such as FTP, email, storage, gaming etc.
  • Types
    • Shared Web Server (Many websites hosted typically unrelated)
    • Dedicated Web Server ( opposite of shared )
Examples of Web Servers are: Apache Tomcat and Resin.
Application Server
  • An application server is a server that hosts applications.
  • It's a server application that implements the Java EE platform APIs and provides the standard Java EE services
  • Application server contains Web and EJB containers. It can be used for servlet, jsp, struts, jsf, ejb etc. It is a component based product that lies in the middle-tier of a server centric architecture.
  • It provides the middleware services for state maintenance and security, along with persistence and data access. It is a type of server designed to install, operate and host associated services and applications for the IT services, end users and organizations.
  • The block diagram representation of Application Server is shown below:
notion image
Web Container
  • A web container (also known as a servlet container) is the component of a web server that interacts with Servlets.
  • It's responsible for
    • Managing the lifecycle of servlets
    • Mapping a URL to a particular servlet
    • Ensuring that the URL requester has the correct access-rights.
  • It handles requests to servlets, Jakarta Server Pages (JSP) files, and other types of files that include server-side code.
  • It creates servlet instances, loads and unloads servlets, creates and manages request and response objects, and performs other servlet-management tasks.
  • It implements the web component contract of the Jakarta EE architecture.
  • This architecture specifies a runtime environment for additional web components, including security, concurrency, lifecycle management, transaction, deployment, and other services.
Servlets
  • A servlet is a Java program that runs on a Web server. It is similar to an applet, but is processed on the server rather than a client's machine.
  • A Servlet is a Java software component that extends the capabilities of a server.
  • Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web applications on web servers and thus qualify as a server-side servlet web API
 
Picture representing relationship of all the above components
🗒️
Application Server is not needed for basic use but if specific Java EE Components have to be used, in that case applications servers are needed.
notion image
 
Web Server Vs Application Server
  • Web Container + HTTP request handling = Web Server
  • Web Server + EJB + (Messaging + Transactions+ etc) = Application Server
 
notion image
 
Features
  • In Servlets, when multiple requests are made, only one copy of servlet is loaded in Java Virtual Machine
  • In Servlets, for each request a thread is created rather than new process
  • Creating threads saves memory and response time
  • Servlet programs are persistent
Servlet API
 

Two java packages are used to implement Servlet API

 
javax.servlet
  • The javax.servlet package contains many interfaces and classes that are used by the servlet or web container.
  • These are not specific to any protocol.
Classes
  1. GenericServlet
  1. ServletInputStream
  1. ServletOutputStream
  1. ServletRequestWrapper
  1. ServletResponseWrapper
  1. ServletRequestEvent
  1. ServletContextEvent
  1. ServletRequestAttributeEvent
  1. ServletContextAttributeEvent
  1. ServletException
  1. UnavailableException
Interfaces
  1. Servlet
  1. ServletRequest
  1. ServletResponse
  1. RequestDispatcher
  1. ServletConfig
  1. ServletContext
  1. SingleThreadModel
  1. Filter
  1. FilterConfig
  1. FilterChain
  1. ServletRequestListener
  1. ServletRequestAttributeListener
  1. ServletContextListener
  1. ServletContextAttributeListener
javax.servlet.http
  • The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
Classes
  1. HttpServlet
  1. Cookie
  1. HttpServletRequestWrapper
  1. HttpServletResponseWrapper
  1. HttpSessionEvent
  1. HttpSessionBindingEvent
  1. HttpUtils (deprecated now)
Interfaces
  1. HttpServletRequest
  1. HttpServletResponse
  1. HttpSession
  1. HttpSessionListener
  1. HttpSessionAttributeListener
  1. HttpSessionBindingListener
  1. HttpSessionActivationListener
  1. HttpSessionContext (deprecated now)
 
 
Servlet LifeCycle

Stages of the Servlet Life Cycle

  • Loading a Servlet.
  • Initializing the Servlet.
  • Request handling.
  • Destroying the Servlet.

Servlet Life Cycle Methods

  • init()
  • service()
  • destroy()
Diagram of methods
notion image
 
Pic 1
notion image
Pic 2
notion image
 
Further Reading
 
Sample Servlet
 
 
Session Tracking Mechanism
  • Session simply means a particular interval of time.
  • Session Tracking is a way to maintain state (data) of an user.
  • It is also known as session management in servlet.
  • Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.
  • HTTP is stateless that means each request is considered as the new request.
 

Session Tracking Techniques

Cookies
  • A cookie is a small piece of information that is persisted between the multiple client requests.
  • A cookie has a name, a single value, and optional attributes such as a comment, path
  • By default, each request is considered as a new request. In cookies technique, we add cookie with response from the servlet.
  • So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user.
notion image

There are two types of cookies

  • Non-persistant Cookies (Valid for single session)
  • Persistant Cookies (Valid for multiple Sessions)
Advantages
  1. Simplest technique of maintaining the state.
  1. Cookies are maintained at client side.
Disadvantages
  1. It will not work if cookie is disabled from the browser.
  1. Only textual information can be set in Cookie object.
 

Further Reading

Hidden Form Field
  • A hidden (invisible) textfield is used for maintaining the state of an user.
  • In such case, we store the information in the hidden field and get it from another servlet. This approach is better if we have to submit form in all the pages and we don't want to depend on the browser.
<input type="hidden" name="uname" value="Vimal Jaiswal">
  • Here, uname is the hidden field name and Vimal Jaiswal is the hidden field value.
  • It is widely used in comment form of a website. In such case, we store page id or page name in the hidden field so that each page can be uniquely identified.
Advantages
It will always work whether cookie is disabled or not.
Disadvantages
  1. It is maintained at server side.
  1. Extra form submission is required on each pages.
  1. Only textual information can be used.
notion image

Further Reading

URL Rewriting
  • In URL rewriting, we append a token or identifier to the URL of the next Servlet or the next resource. We can send parameter name/value pairs using the following format:
  • url?name1=value1&name2=value2&??
  • A name and a value is separated using an equal = sign, a parameter name/value pair is separated from another parameter using the ampersand(&). When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server.
  • From a Servlet, we can use getParameter(<name of element in form>) method to obtain a parameter value.

Further Reading

HttpSession
 
Sir's PPT
Sir's PPT for this unit
UNIT 5
JSP
Okay, What the hell is JSP ?
  • Java Server Pages (JSP) is a technology which is used to develop web pages by inserting Java code into the HTML/XML pages by making special JSP tags. The JSP tags which allow java code to be included into it are <% ----java code----%>.
  • It can consist of either HTML or XML (combination of both is also possible) with JSP actions and commands.
  • It can be used as HTML page, which can be used in forms and registration pages with the dynamic content into it.
  • Dynamic content includes some fields like dropdown, checkboxes, etc. whose value will be fetched from the database.
  • This can also be used to access JavaBeans objects.
  • We can share information across pages using request and response objects.
  • JSP can be used for separation of the view layer with the business logic in the web application.
Why use JSP?
  • In Java server pages JSP, the execution is much faster compared to other dynamic languages.
  • It is much better than the previously used Common Gateway Interface (CGI).
  • Java server pages JSP are always compiled before its processed by the server as it reduces the effort of the server to create process.
  • Java server pages JSP are built over Java Servlets API. Hence, it has access to all Java APIs, even it has access to JNDI, JDBC EJB and other components of java.
  • JSP are used in MVC architecture (which will be covered in MVC architecture topic) as view layer.
  • The request is processed by a view layer which is JSP and then to servlet layer which is java servlet and then finally to a model layer class which interacts with the database.
  • JSP is an important part of Java EE, which is a platform for enterprise level applications.
Advantages
  • The advantage of JSP is that the programming language used is JAVA, which is a dynamic language and easily portable to other operating systems.
  • It is very much convenient to modify the regular HTML. We can write the servlet code into the JSP.
  • It is only intended for simple inclusions which can use form data and make connections.
  • JSP can also include the database connections into it. It can contain all type of java objects.
  • It is very easy to maintain
  • Performance and scalability of JSP are very good because JSP allows embedding of dynamic elements in HTML pages.
  • As it is built on Java technology, hence it is platform independent and not depending on any operating systems.
  • Also, it includes the feature of multithreading of java into it.
  • We can also make use of exception handling of java into JSP.
  • It enables to separate presentation layer with the business logic layer in the web application.
  • It is easy for developers to show as well as process the information.
Life Cycle of JSP Page
JSP Elements
JSP Declaration
  • <%! %>
  • A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method.
  • We can declare a static member, an instance variable (can declare a number or string) and methods inside the declaration tag.
<%!  Dec var  %>
Here Dec var is the method or a variable inside the declaration tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Declaration Tag</title>
</head>
<body>
<%! int count =10; %>
<% out.println("The Number is " +count); %>
</body>
</html>
JSP Scriptlet
  • <% %>
  • Scriptlet tag allows to write Java code into JSP file.
  • JSP container moves statements in _jspservice() method while generating servlet from jsp.
  • For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for every request.
  • A Scriptlet contains java code that is executed every time JSP is invoked.
Syntax of Scriptlet tag:
<% java code %>
Here <%%> tags are scriplets tag and within it, we can place java code.
JSP Expression
  • <%= %>
  • Expression tag evaluates the expression placed in it.
  • It accesses the data stored in stored application.
  • It allows create expressions like arithmetic and logical.
  • It produces scriptless JSP page.
Syntax:
<%= expression %>
Example:
Here the expression is the arithmetic or logical expression.
In this example, we are using expression tag
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Guru Expression</title>
	</head>
	<body>
		<% out.println("The expression number is "); %>
		<% int num1=10; int num2=10; int num3 = 20; %>
		<%= num1*num2+num3 %>
	</body>
</html>
JSP Comments
These tags are used to comment in JSP and ignored by the JSP container.
  • <%-- comment --%>
  • Comments are the one when JSP container wants to ignore certain texts and statements.
  • When we want to hide certain content, then we can add that to the comments section.
Syntax:
<% -- JSP Comments %>
This is HTML comment which is ignored by browser
Example:
In this example, we are using JSP comments
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Guru Comments</title>
	</head>
	<body>
		<%-- Guru Comments section --%>
		<% out.println("This is comments example"); %>
	</body>
</html>
Resource
JSP Directives
  • <%@ %>.
What are they?
  • JSP directives are the messages to JSP container. They provide global information about an entire JSP page.
  • JSP directives are used to give special instruction to a container for translation of JSP to servlet code.
  • In JSP life cycle phase, JSP has to be converted to a servlet which is the translation phase.
  • They give instructions to the container on how to handle certain aspects of JSP processing
  • Directives can have many attributes by comma separated as key-value pairs.
  • In JSP, directive is described in <%@ %> tags.
<%@ directive attribute="" %>

There are 3 Types of directives

Page Directive
  • <%@ page attribute="value" %>
  • It provides attributes that get applied to entire JSP page.
  • It defines page dependent attributes, such as scripting language, error page, and buffering requirements.
  • It is used to provide instructions to a container that pertains to current JSP page.

Following are its list of attributes associated with page directive:

To see detail about these attributes head over to the resource toggle below
  1. Language
  1. Extends
  1. Import
  1. contentType
  1. info
  1. session
  1. isThreadSafe
  1. autoflush
  1. buffer
  1. IsErrorPage
  1. pageEncoding
  1. errorPage
  1. isELIgonored
Example
<%@ page language="java" %>
 
Include Directive
  • <%@ include….%>
  • JSP "include directive" is used to include one file to the another file
  • This included file can be HTML, JSP, text files, etc.
  • It is also useful in creating templates with the user views and break the pages into header&footer and sidebar actions.
  • It includes file during translation phase
Example Syntax of include directive:
<%@page import="java.util.Date" %>
Taglib Directive
  • <%@ taglib uri="uri" prefix="myTag"%>
  • JSP taglib directive is used to define the tag library with "taglib" as the prefix, which we can use in JSP.
  • Basically if you have tags which are custom made for your project you can use that with this directive
  • JSP taglib directive is used in the JSP pages using the JSP standard tag libraries
  • It uses a set of custom tags, identifies the location of the library and provides means of identifying custom tags in JSP page.
Syntax of taglib directive:
<%-- Initialize custom tag like this %-->

<%@ taglib uri="uri" prefix="value"%>

<%-- Use the tag like this %-->

<mytag:currentDate/>
Here "uri" attribute is a unique identifier in tag library descriptor and "prefix" attribute is a tag name.
 
Resource
JSP Implicit Objects
  • JSP implicit objects are created during the translation phase of JSP to the servlet.
  • These objects can be directly used in scriplets that goes in the service method.
  • They are created by the container automatically, and they can be accessed using objects.

How many Implicit Objects are available in JSP?

There are 9 types of implicit objects available in the container:
To see detail about these implicit objects click here and go to the resource website
out
  • Out is one of the implicit objects to write the data to the buffer and send output to the client in response
  • Out object allows us to access the servlet's output stream
  • Out is object of javax.servlet.jsp.jspWriter class
  • While working with servlet, we need printwriter object
Example where JSP page converted into servlet
Notice the PrintWriter class
notion image
request
  • The request object is an instance of java.servlet.http.HttpServletRequest and it is one of the argument of service method
  • It will be created by container for every request.
  • It will be used to request the information like parameter, header information , server name, etc.
  • It uses getParameter() to access the request parameter.
response
  • "Response" is an instance of class which implements HttpServletResponse interface
  • Container generates this object and passes to _jspservice() method as parameter
  • "Response object" will be created by the container for each request.
  • It represents the response that can be given to the client
  • The response implicit object is used to content type, add cookie and redirect to response page
config
  • "Config" is of the type java.servlet.servletConfig
  • It is created by the container for each jsp page
  • It is used to get the initialization parameter in web.xml
application
  • Application object is an instance of javax.servlet.ServletContext and it is used to get the context information and attributes in JSP.
  • Application object is created by container one per application, when the application gets deployed.
  • Servletcontext object contains a set of methods which are used to interact with the servlet container.We can find information about the servlet container
session
  • The session is holding "httpsession" object(code line 10).
  • Session object is used to get, set and remove attributes to session scope and also used to get session information
pageContext
  • This object is of the type of pagecontext.
  • It is used to get, set and remove the attributes from a particular scope

There are 4 types of Scopes

  • Page
  • Request
  • Session
  • Application
<% pageContext.setAttribute("student","gurustudent",pageContext.PAGE_SCOPE); %>
setAttribute has 3 parameters :
  • Key
  • Value
  • Scope
page
  • Page implicit variable holds the currently executed servlet object for the corresponding jsp.
  • Acts as this object for current jsp page.
<% String pageName = page.toString(); %>
 
exception
  • Exception is the implicit object of the throwable class.
  • It is used for exception handling in JSP.
  • The exception object can be only used in error pages
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" 
🗒️
Explanation : It has an array of numbers, i.e., num1 with four elements. In the output, we are trying to print the fifth element of the array from num1, which is not declared in the array list. So it is used to get or show exception object of the jsp.

Output

notion image
Resource
JSP Standard Action Tags

What is JSP Action?

  • JSP actions use the construct in XML syntax to control the behavior of the servlet engine.
  • We can dynamically insert a file, reuse the beans components, forward user to another page, etc. through JSP Actions like include and forward.
  • Unlike directives, actions are re-evaluated each time the page is accessed.
Syntax:
<jsp:action_name attribute="value" />
How many Action Tags are available in JSP ?
There are 11 types of Standard Action Tags as following:
To see detail about these implicit objects click here
  • jsp:useBean
  • jsp:include
  • jsp:setProperty
  • jsp:getProperty
jsp:forward
  • It is used to forward the request to another jsp or any static page.
  • Here the request can be forwarded with no parameters or with parameters.
Syntax:
<jsp:forward page="value">
  • Here value represents where the request has to be forwarded.
Example:
Action_jsp41.jsp
	<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<html>
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
			<title>Guru Action JSP1</title>
		</head>
		<body>
			<jsp:forward page="jsp_action_42.jsp" />  --- [ <-- LINE 10 ] ---
		</body>
	</html>
Jsp_action_42.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Guru Action JSP2</title>
</head>
<body>
	<a>This is after forward page</a>
</body>
</html>
Explanation of the code

Action_jsp41.jsp

Code Line 10: Here we are using forward JSP Action to forward the request to the page mentioned in the attribute, i.e., jsp_action_42.jsp
Jsp_action_42.jsp
Code Line 10: Once we call action_jsp41.jsp, the request gets forwarded to this page, and we get the output as "This is after forward page."
When we execute the above code, we get the following output
notion image
Output:
We call action_jsp41.jsp but the request gets forwarded to jsp_action_42.jsp, and we get the output from that page as "This is after forward page".
 
  • jsp:plugin
  • jsp:attribute
  • jsp:body
  • jsp:text
  • jsp:param
  • jsp:attribute
  • jsp:output
 
Resource
JSP Expression Language

What is Expression Language (EL)?

  • ( Enables you to directly execute code in the JSP file )
  • Expression Language (EL) is mechanism that simplifies the accessibility of the data stored in Java bean component and other object like request, session and application, etc.
  • There are many operators in JSP that are used in EL like arithmetic and logical operators to perform an expression. It was introduced in JSP 2.0
In this tutorial, you will learn-
JSP Syntax of Expression Language (EL)
Syntax of EL :$(expression)
  • In JSP, whatever present in the braces gets evaluated at runtime sent to the output stream.
  • The expression is a valid EL expression and it can be mixed with a static text and can be combined with other expression to form larger expression.
To get a better idea, on how expression works in JSP, we will see below example.
In this example, we will see how EL is used as an operator to add two numbers (1+2) and get the output respectively.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Guru JSP1</title>
	</head>
	<body>
		<a>Expression is:</a>
		${1+2};
	</body>
</html>
Explanation of Code:
  • Code Line 11: Expression Language (EL) is set where we are adding two numbers 1+2, hence it will give output as 3.
When you execute the above code, you will have the following Output.
Output:
notion image
  • Expression is: 3 (As numbers 1+2 will be added and serve as an output)
Flow - Control Statements
Decision-Making Statements
JSP If-else
"If else" statement is basic of all control flow statements, and it tells the program to execute the certain section of code only if the particular test evaluates to true.
This condition is used to test for more than one condition whether they are true or false.
  • If the first condition is true then "if block" is executed and
  • if it is false then "else block" is executed
Syntax for if – else statement:
If(test condition)
{
	 //Block of statements
}
else
{
    //Block of statements
JSP Switch
The body of the switch statement is called as a "switch block".
  • The switch case is used to check the number of possible execution paths.
  • A switch can be used with all data types
  • The switch statements contain more than one cases and one default case
  • It evaluates the expression then executes all the statements following the matching case
Syntax for switch statement:
switch (operator)
{
   Case 1:
       Block of statements
break;
   Case 2:
      Block of statements
break;


case n:
    Block of statements
break;
default:
  Block of statements
break;
}
  • Switch block begins with one parameter, which is the operator that needs to be passed and
  • Then there are different cases which provides condition and whichever matches with the operator that case is executed.
Loop Statements
JSP For loop
It is used for iterating the elements for a certain condition, and it has three parameters.
  • Variable counter is initialized
  • Condition till the loop has to be executed
  • Counter has to be incremented
For loop Syntax:
For(inti=0;i<n;i++)
{
           //block of statements
}
JSP While loop
It is used to iterate the elements wherein it has one parameter of the condition.
Syntax:
While(i<n)
{
      //Block of statements
}
JSP Operators
  • JSP Operators supports most of its arithmetic and logical operators which are supported by java within expression language (EL) tags.
notion image
 
JSP Custom Tags
Directory Structure of JSP
  • In directory structure, there is a root folder which has folder WEB-INF, which has all configuration files and library files.
  • JSP files are outside WEB-INF folder
notion image
JSP vs Java Servlet
 
Acessing database from Servlet and JSP
Architecture of JSP Page
Resources
 
 
Sir's PPTs
1
2