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

WEB TECH UNIT-3

JSP technology simplifies the creation of dynamic web applications by allowing developers to embed Java code within HTML, making it easier to maintain than Servlets. JSP pages are converted into Servlets by the Web Container, enabling efficient separation of presentation and business logic. Key advantages of JSP include ease of coding, high performance, and platform independence, along with various implicit objects that facilitate data handling and session management.

Uploaded by

ishavkashyap786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

WEB TECH UNIT-3

JSP technology simplifies the creation of dynamic web applications by allowing developers to embed Java code within HTML, making it easier to maintain than Servlets. JSP pages are converted into Servlets by the Web Container, enabling efficient separation of presentation and business logic. Key advantages of JSP include ease of coding, high performance, and platform independence, along with various implicit objects that facilitate data handling and session management.

Uploaded by

ishavkashyap786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JSP technology is used to create dynamic web applications.

JSP pages are easier to maintain


then a Servlet. JSP pages are opposite of Servlets as a servlet adds HTML code inside Java code,
while JSP adds Java code inside HTML using JSP tags. Everything a Servlet can do, a JSP page
can also do it.

JSP enables us to write HTML pages containing tags, inside which we can include powerful Java
programs. Using JSP, one can easily separate Presentation and Business logic as a web
designer can design and update JSP pages creating the presentation layer and java developer can
write server side complex computational code without concerning the web design. And both the
layers can easily interact over HTTP requests.

In the end a JSP becomes a Servlet

JSP pages are converted into Servlet by the Web Container. The Container translates a JSP page
into servlet class source(.java) file and then compiles into a Java Servlet class.

Why JSP is preffered over servlets?

 JSP provides an easier way to code dynamic web pages.

 JSP does not require additional files like, java class files, web.xml etc

 Any change in the JSP code is handled by Web Container(Application server like
tomcat), and doesn't require re-compilation.

 JSP pages can be directly accessed, and web.xml mapping is not required like in servlets.

Advantage of JSP

 Easy to maintain and code.

 High Performance and Scalability.

 JSP is built on Java technology, so it is platform independent.

JSP OBJECTS:

JSP Implicit Objects are Java objects automatically made available to developers by the JSP
Container on each page without any explicit declaration from developers. This is an essential
component of the JSP application.

Types of Implicit objects in JSP


Request
It is inbuilt in javax.servlet.http.HttpServletRequest. It has the sole purpose of getting the
data entered on a JSP page from the previous JSP page. We often ask users for their details
while dealing with JSP forms. This object is used to extract that information and pass it to
another JSP page (action page) for validation.
Intro.html
<!DOCTYPE html>
<html>
<head>
<title>JSP implicit objects</title>
</head>
<body>
<form action="new.jsp">
Enter your name: <input type="text" name="name" /> <br />

<input type="submit" value="confirm" />


</form>
</body>
</html>
new.jsp
<%@ page import = " java.util.* " %>
<%
String name = request.getParameter("name");
out.print("Hey!!!"+name+" have a good day.");
%>
Output

When we load the intro.html file in the browser it will ask for input, after that new.jsp file
will get loaded according to the input.

It shows the user a Hello message, and getParameter() returns the Parameter value as a name
from the user.
Response
An object from HttpServletResponse is used to modify or handle a response sent back to the
client (browser) after processing a request.
intro.html
<!DOCTYPE html>
<html>
<head>
<title>implicit object</title>
</head>
<body>
<form action="new.jsp">
OPEN CODING NINJA'S

<input type="submit" value="OPEN" /><br />


</form>

</html>
detail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1" />
<title>JSP response</title>
</head>
<body>
<% response.sendRedirect("https://www.codingninjas.com/"); %>
</body>
</html>
Output

When we load the intro.html file in the browser it will show a button to open the details.jsp
file.

This example shows how to use a response and its method sendRedirect. After clicking on
open, users are redirected to https://www.codingninjas.com/.
PageContext
The javax.servlet.jsp uses this object as an implicit object when accessing the attributes for a
page, request, application, or session.
intro.html
<!DOCTYPE html>
<html>
<head>
<title>implicit object</title>
</head>
<body>
<form action="new.jsp">Enter your name: <input type="text" name="name" /> <input
type="submit" value="Enter" /><br /></form>
</body>
</html>
new.jsp
<!DOCTYPE html>
<html>
<head>
<title>PageContext</title>
</head>
<body>
<% String name = request.getParameter("name"); out.print("Hey!!!" + name);
pageContext.setAttribute("User", name, PageContext.SESSION_SCOPE); %>
<a href="next.jsp">next jsp page</a>
</body>
</html>
next.jsp
<!DOCTYPE html>
<html>
<head>
<title>page context</title>
</head>
<body>
<% String name = (String) pageContext.getAttribute("User",
PageContext.SESSION_SCOPE); out.print("Hey!!!" + name); %>
</body>
</html>
Output

When we load the intro.html file in the browser it will ask for the input after entering the
value and pressing the Enter button then new.jsp file will get loaded.
Now, when we click on the next jsp page it will load the next.jsp file.

As shown in the example above, new.jsp sets the attribute client in the Session scope and
passes it to the next.jsp, which retrieves the attribute and displays the result.
Session
The session object is implicit under the javax.servlet.http.HttpSession package. This object is
used to store user data used by other JSP pages as long as the user is still logged in to the
page.
intro.html
<!DOCTYPE html>
<html>
<head>
<title>JSP implicit object</title>
</head>
<body>
<form action="new.jsp">Enter your name:<input type="text" name="name" /> <input
type="submit" value="Enter" /><br /></form>
</body>
</html>
new.jsp
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<% String name = request.getParameter("name"); out.print("Hey!!! " + name);
session.setAttribute("Individual", name); %>
<a href="next.jsp">nextjsp page</a>
</body>
</html>
next.jsp
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<% String name = (String) session.getAttribute("Individual"); out.print("Hey!!!" +
name); %>
</body>
</html>
Output

When we load the intro.html file in the browser it will ask for the input after entering the
value and pressing the Enter button then new.jsp file will get loaded.

Now, when we click on the next jsp page it will load the next.jsp file.

Using this code, a session will be generated. Once the form is submitted, the form is returned
to new.jsp, which is a session page. If we click the following JSP page link, we get to the
next.jsp, which says Hey! Student.
Application
JSP uses the implicit Application object to obtain initialization parameters and share
attributes across all pages. JSP is an implicit object of the ServletContext implementation of
javax.servlet.ServletContext. All the JSP pages would have access to any attributes added by
the application implicit object.
Application-wide initialization parameters and user data are obtained and maintained through
this method.
new.jsp
<%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html>
<html>
<head>
<title>Application Implicit</title>
</head>
<body>
<% Integer count= (Integer)application.getAttribute("clicks"); if( count ==null ||
count== 0 ){ count = 1; } else{ count = count+ 1; } application.setAttribute("clicks", count);
%>
<h3>Number of page refresh: <%= count %></h3>
</body>
</html>
Output

An application and the method getAttribute are the objects used in the example. A given
attribute name returns the object associated with that attribute.
Config
The object is of the type javax.servlet.ServletConfig. This service configuration object allows
you to obtain servlet configuration information, like servlet context, servlet name, or
configuration parameters.
next.jsp
<%
String servletname=config.getServletName();
out.print("Name of servlet is: "+servletname);
%>
new.xml
<web-app>
<servlet>
<servlet-name>jsp</servlet-name>
<jsp-file>/next.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/next</url-pattern>
</servlet-mapping>
</web-app>
Output

Here, the servlet-mapping is performed by new.xml, and the servlet name gets printed in the
output.
Out
This is the most commonly used implicit object provided by java.servlet.jsp.JspWriter. It is
used to write content to the client (browser). You can use various methods to format output
messages for display to a browser and deal with buffers.
new.jsp
<html>
<head>
<title>OUT OBJECT</title>
</head>
<body>
<% out.print( "Hey Ninjas's" ); out.println( "How are you doing?" ); %>
</body>
</html>
Output

Here is an example that uses the method out.println() to demonstrate out implicit objects. It
prints the output on the screen.
Page
An implicit object under the java.lang.Object.class.Page class represents a current Servlet
instance (a JSP page converted into a Servlet). The implicit object can be used instead of the
actual one. This implicit object is rarely used and is useless when building JSP applications.
next.jsp
<html>
<head>
<title>Page object</title>
</head>
<body>
<% String pageName = page.toString(); out.println("Page Name is " +pageName);%>
</body>
</html>
Output

As a result of a successful compilation, a reference page is generated.


Exception
In exception handling, java.lang.Throwable.Exception implicit object is used to display error
messages. Only pages that have set isErrorPage to true have access to this object.
intro.html
<html>
<body>
<form action="new.jsp">
Enter Ist number:<input type="number" name="val1"/><br/>
<br/>
Enter IInd number:<input type="number" name="val2" /><br/>
<br/>
<input type="submit" value="result" />
</form>
</body>
</html>
new.jsp
<%@ page errorPage="next.jsp" %>
<%
String var1 = request.getParameter("one");
String var2 = request.getParameter("two");
int value1= Integer.parseInt(var1);
int value2= Integer.parseInt(var2);
int result= value1 / value2;
out.print("Division is: "+ result);
%>
next.jsp
<%@ page isErrorPage="true" %>
<h3>An exception occured please enter again</h3>
Exception is: <%= exception %>
Output

When we load the intro.html file in the browser it will ask for the input after entering the
values and pressing the Divide button then new.jsp file will get loaded.

If there is no error, then the output will be displayed in the browser.


If we enter some invalid inputs, as shown below:

Then it will display an error message.

The HTML page in this example opens and requests input. After entering these numbers,
new.jsp performs calculations, and if the page throws an error, next.jsp is invoked and throws
java.langNumberFormatException and checks the data.

You might also like