Unit 3 - Chp2
Unit 3 - Chp2
1
Introduction to JavaServer Pages (JSP)
JavaServer Pages (JSP) is a technology for building interactive web pages. It was created by Sun Microsystems and is
an advanced version of Java servlets. JSP makes it easier to develop web pages and is useful for many applications.
Like most server-side technologies, JSP separates the business logic (how the website works) from the presentation
layer (how the website looks).
JSP pages are just like normal HTML pages but with Java code inside them. To use JSP, developers need a special
engine (JSP engine) connected to a web server. The JSP engine turns the JSP page into a servlet, which the servlet
engine processes. This process is called "translation." The servlet then runs, creating dynamic HTML content that
gets sent to the browser. The JSP page is converted to a servlet the first time it is requested, and it stays that way
unless changes are made to the JSP file. JSP can also work with Java DataBase Connectivity (JDBC) to create dynamic,
database-driven websites.
Extension of Servlet Technology: JSP builds on servlet technology. It can use all the features of servlets and
adds more, like implicit objects, predefined tags, expression language (EL), and custom tags, making JSP
easier to develop.
Easy to Maintain: In JSP, the business logic and presentation (how the website looks) are separate. This
makes it easier to manage compared to servlets, where both are mixed together.
Fast Development: If you modify a JSP page, there's no need to recompile and redeploy the project. In
contrast, with servlets, you need to update and recompile the code whenever you want to change the look
of the application.
Less Code: JSP allows you to use tags like action tags, JavaServer Pages Standard Tag Library (JSTL), custom
tags, and implicit objects, which reduces the amount of code you need to write. It also uses Java's "write
once, run anywhere" advantage.
Simplified Syntax: JSP uses a simpler scripting language to embed HTML, making it easier to work with.
Q.2
Compared to Active Server Pages (ASP): ASP, created by Microsoft, is also a tag-based programming
language, but ASP code isn't portable. This means you can't easily move it to different platforms. On the
other hand, JSP is portable because it's written in Java, which works across different platforms. With JSP,
you're not tied to any specific server or IIS (Internet Information Services).
Compared to PHP: PHP is an open-source scripting language, just like Java. It is similar to JSP and ASP but
isn't ideal for large-scale applications or banking apps that involve financial transactions due to security
concerns. Also, PHP requires learning a new language, while JSP lets you work with Java, which you may
already know.
Compared to JavaScript: JavaScript has no connection to Java programming. It's mostly used to create
dynamic HTML on the client side (in the browser). However, JavaScript is not suitable for tasks like network
programming or accessing server resources. Java (used in JSP) is much more powerful, flexible, reliable, and
portable than JavaScript.
Compared to HTML: Regular HTML is static, meaning it can't react to user input or handle dynamic content.
It also doesn't interact with server-side resources. JSP can handle both static (HTML) and dynamic content,
making it more versatile than plain HTML.
Q.3
Q.4
A JSP page doesn't directly send content to the browser. Instead, it goes through several steps on the server. The JSP
page is first translated into a Java class (called a servlet) which then handles all requests made to the JSP.
2. JSP Page Compilation: After translation, the Java servlet file is compiled into a servlet class (Java bytecode).
This step can happen either when the JSP page is first deployed or when a client requests the page for the
first time.
3. Class Loading: The servlet class created from the JSP file is loaded into the server's memory.
4. Execution Phase: The server creates one or more instances of the servlet class when a request is made to
the JSP page. Two important methods (jspInit() and jspDestroy()) are defined in the interface called JspPage,
which helps manage the JSP life cycle. For pages handling HTTP requests, the jspService() method handles
the requests.
5. Initialization: The jspInit() method is called right after the servlet instance is created. This happens only once
in the JSP life cycle.
6. jspService() Execution: This method is called every time a request is made to the JSP page during its life
cycle. It passes the request and response objects to handle the request. You cannot override the jspService()
method.
7. jspDestroy() Execution: This method is called when the JSP page is about to be destroyed. After this, the
servlet completes its job and gets cleaned up by the garbage collector, marking the end of the JSP life cycle.
Q.5
To run JSP pages, the web server requires a JSP engine (also called a container). This container is responsible for
handling all requests made for JSP pages.
The JSP container works with the web server to provide the necessary environment and services for JSP to run. It
understands and processes the special JSP elements (like tags and Java code) embedded in the page.
When it encounters a JSP page, the web server passes the request to the JSP handler.
The JSP handler processes the JSP tags and Java code in the page.
The Java compiler converts this code into a Java class (a servlet).
The Java runtime then runs this servlet, which generates the final HTML content to be sent back to the
user’s browser.
In short, the JSP container ensures that JSP pages are properly processed and converted into dynamic web content
that can be displayed in the browser.
1. Browser Request: Your browser sends an HTTP request to the web server, just like it does for a regular
webpage.
2. Web Server Detection: The web server sees that the request is for a JSP page (because the file ends with .jsp
instead of .html) and sends the request to the JSP engine.
3. JSP to Servlet Conversion: The JSP engine loads the JSP page from the server’s disk and converts it into a
servlet. This process is simple: any regular text (like HTML) is turned into println() statements, and JSP
elements (Java code) are transformed into Java code that handles the dynamic content of the page.
4. Servlet Compilation: The JSP engine compiles this Java code into an executable servlet class and sends the
request to the servlet engine.
5. Servlet Execution: The servlet engine, a part of the web server, loads the compiled servlet class and runs it.
During this process, the servlet generates HTML output.
6. Sending HTML to Browser: The servlet engine sends the generated HTML back to the web server, which
forwards it to your browser as an HTTP response.
7. Browser Display: The browser receives this dynamically generated HTML and displays it, just like a normal
webpage.
Additionally, the JSP engine checks whether a servlet for the JSP page has already been created. If the JSP file hasn’t
changed, the engine reuses the existing servlet, making the process more efficient and faster than some other
scripting languages like PHP
Chapter 14
Syntax:
<JSP:action_name attribute="value" />
2. **Scope attribute**: This controls how long the action's data is available. It works with the id attribute to decide
the lifespan of the object. The scope can be:
- **Page**: Data is available only on the current page.
- **Request**: Data lasts for the entire request, even if it moves to another page.
- **Session**: Data is kept for the user's whole visit.
- **Application**: Data is available for all users across the entire application.
How It Works: When this tag is used, it stops processing the current page and transfers the request to the
specified resource.
The page attribute specifies the relative URL of the another resource (another JSP, static page, or servlet).
EXAMPLE:
Let's say we have two files:
Passing Parameters: The <jsp:param> tag allows sending extra information, such as parameters, to another
file using an action tag like <jsp:forward>.
Attributes:
Use in Forwarding: This tag is often placed inside <jsp:forward> to send parameters along with the
forwarded request.
Multiple Parameters: You can include multiple <jsp:param> tags inside <jsp:forward> if you need to
send more than one parameter.
Retrieving Parameters: The target file can access these parameters using
request.getParameter("parameterName").
Example:
1. Use <jsp:useBean> Tag: The <jsp:useBean> action is used to add or create an instance of a
JavaBean in a JSP page.
2. Attributes: Common attributes are id (to reference the bean), scope (lifecycle of the bean: page,
request, session, or application), and class (Java class used to create the bean).
3. Bean Creation: If the bean instance doesn’t exist in the specified scope, the JSP container will
create a new one.
4. Example:
1. Use <jsp:setProperty> Tag: The <jsp:setProperty> action is used to set the value of a bean
property.
2. Attributes: name (identifies the bean), property (the property to set), and value (the value to
assign).
3. Usage Outside and Inside <jsp:useBean>: It can be used outside <jsp:useBean> for existing
beans, or inside to set properties when a new bean is instantiated.
4. Example:
1. Use <jsp:getProperty> Tag: The <jsp:getProperty> action retrieves the value of a specified
bean property.
2. Attributes: name (identifies the bean) and property (the property to access).
3. Output the Property Value: The value is converted to a string and inserted into the JSP page's
output.
4. Example:
1. The <jsp:setProperty> action is used to set the properties of a previously defined Bean.
2. It can be used outside of <jsp:useBean>, meaning it will run whether a new Bean was created or an
existing one was found:
jsp
Copy code
<jsp:useBean id="myBean" ... />
<jsp:setProperty name="myBean" property="someProperty" ... />
3. If <jsp:setProperty> is inside the <jsp:useBean> element, it will only run if a new Bean is
instantiated:
jsp
Copy code
<jsp:useBean id="myBean" ...>
<jsp:setProperty name="myBean" property="someProperty" ... />
</jsp:useBean>
4. The name attribute specifies the Bean to update, and the property attribute identifies which property
to set. Using "*" for property sets all matching request parameters.
5. The value attribute assigns a specific value to the property, while the param attribute assigns a
request parameter's value.
6. If the specified parameter does not exist or is null, <jsp:setProperty> does nothing.
1. The <jsp:getProperty> action retrieves the value of a Bean's property and outputs it as a string.
2. The name attribute specifies which Bean to retrieve the property from, and the property attribute
specifies which property to access.
3. Here’s an example setup:
5. Make sure the compiled TestClass.class file is accessible in the web application's classpath.
6. In a JSP file:
<html>
<head><title>Using JavaBeans in JSP</title></head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
<jsp:useBean id="test" class="testAction.TestClass" />
<jsp:setProperty name="test" property="msg" />
<p>Here we got the message</p>
<jsp:getProperty name="test" property="msg" />
</center>
</body>
</html>
These actions are used to create XML elements dynamically. The term "dynamically" is important because it means
the XML elements can be generated during the request, rather than being fixed at the time compiling.