WEB TECH UNIT-3
WEB TECH UNIT-3
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.
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.
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
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.
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
</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
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.
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.