fbpx
Select Page

Introduction to Java Server-side Programming (JSP)

JSP (Java Server Pages) is server-side technology to create dynamic java web pages. JSP is an extension to servlet technology because it has the facility to create user views easily. JSP Page consists of HTML code and when required has an option to include java code for dynamic content. Web applications contain a lot of user screens, JSPs are used a lot in creating web applications. To balance the gap between java code and HTML in JSP, it provides extra features such as JSP Tags, Expression Language, Custom tags. This makes it easier for a web developer to quickly develop JSP pages.

Advantages of JSP over Servlet:

  • We can write HTML response using servlets also but the process is tedious and error-prone when it comes to writing a complex HTML response, writing in a servlet is difficult. JSP provides an option to write a normal HTML page and include our java code only where it’s required.
  • JSP provides extra additional features such as tag libraries, expression language, custom tags that helps in faster development of user views.
  • JSP pages are easier to deploy, we need to change the modified page in the server and the container takes care of the deployment. For servlets execution, we need to restart, compile the server and deploy the whole project again.

The Servlet and JSPs complement each other. We should implement Servlet as a server-side controller to communicate with model classes whereas JSPs should be implemented as a presentation layer.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

JSP Life Cycle:

JSP’s life cycle can be grouped into the following steps.

  • JSP Page Translation:

When a JSP Page is loaded into memory, the container converts a JSP file into a servlet file.

i.e. java servlet file is generated from the JSP source file. 

  • JSP Page Compilation:

After the translation from .jsp to .java servlet file,. java file is then converted into .class file during compilation phase.

  • Initialization:

jspInit () method is called after the instance of the .class file is created. jspInit () method is designed to be called only once during the JSP life cycle.

  • _jspService () execution:

This method is designed to be called for every request received by the web container. 

It processes the request and generates the response. It passes the request and the response objects. _jspService () cannot be overridden.

  • jspDestroy () execution:

This method is called at the end of the lifecycle. In this step, the JSP page is destroyed

and unloaded from the memory before the objects go into garbage collector.jspDestroy() is called only once and not call again for each request.

JSP Scripting Elements:

JSP Scriptlets:

Scriptlet tags are used in a JSP page to include java code. A scriptlet tag starts with <% and ends with %>. Any code written inside the scriptlet tags is considered as java code. All the code inside scriplet tag goes into the _jspService () method.

JSP Expression:

JSP Expression tag starts with <%= and ends with %>. Whenever we want to print some text on the browser, we print dynamic data on the JSP page using out.print () method, there is a shortcut to do this through JSP Expressions. <% out. print (“Welcome”); %>

It can be written using JSP Expression as

<%= “Welcome” %>

JSP Directives:

JSP directives start with <%@ and end with %> Anything was written using this tag is taken as 

a direction to the page. JSP Directives are used to give special instructions to the container when a JSP page is getting translated to servlet source code:

JSP Declaration:

JSP Declarations starts with:

<%! and ends with %>

Whenever we need to declare any member methods and variables of the servlet class, we have to declare it using-declaration tag.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

JSP Implicit Objects

 

  • Out Object:

JSP out implicit object is a part of javax.servlet.jsp.JspWriter implementation.

Example of out implicit object:

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

out.print(“Welcome”);

%>

</body>

</html>

2. Request Object:

JSP request implicit object is part of javax.servlet.http.HttpServletRequest

 Example of JSP request implicit object:

 Input.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<form action=“Output.jsp”>

<input type=“text” name=“uname”>

<input type=“submit” value=“go”><br/>

</form>

</body>

</html>

 Output.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String name=request.getParameter(“uname”);

out.print(“welcome “+name);

%>

</body>

</html>

3. Response Object:

JSP response implicit object is part of javax.servlet.http.HttpServletResponse. 

Redirect.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

response.sendRedirect(http://www.google.com);

%>

</body>

</html> 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

4) config Object:

JSP config implicit object is part of javax.servlet.ServletConfig.It is used to pass configuration information to one particular Jsp Page.

Example of config implicit object:

Jsp file:InitParameter.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String name=config.getInitParameter(“name”);

out.print(“Institute name is = “+name);

%>

</body>

</html>

web.xml file

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=http://java.sun.com/xml/ns/javaee

xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/webapp_

3_0.xsd” version=“3.0”>

<display-name>jsp_practice</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>Home</servlet-name>

<jsp-file>/config.jsp</jsp-file>

<init-param>

<param-name>name</param-name>

<param-value>Coder technologies</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>Home</servlet-name>

<url-pattern>/config.jsp</url-pattern>

</servlet-mapping>

</web-app>

5. Application Object:

JSP application implicit object is part of javax.servlet.ServletContext.It is used to pass global parameters to all the Jsp Pages.

Example of Application implicit object:

Jsp file 1: Context.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<form action=“Application.jsp”>

<input type=“submit” value=“click to check context value”><br/>

</form>

</body>

</html>

Jsp file 2: Application.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String info=application.getInitParameter(“info”);

out.print(“Program info = “+info); %>

</body>

</html>

web.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=http://java.sun.com/xml/ns/javaee

xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/webapp_

3_0.xsd” version=“3.0”>

<display-name>jsp_practice</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>One</servlet-name>

<jsp-file>/Application.jsp</jsp-file>

</servlet>

<servlet-mapping>

<servlet-name>One</servlet-name>

<url-pattern>/Application.jsp</url-pattern>

</servlet-mapping>

<context-param>

<param-name>info</param-name>

<param-value>Example of application object</param-value>

</context-param>

</web-app>

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

6. Session Object:

JSP session implicit object is part of javax.servlet.http.HttpSession.It is used to manage the session in a Jsp page.

Example of Session implicit object:

Input.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<form action=“session1.jsp”>

<input type=“text” name=“uname”>

<input type=“submit” value=“go”><br/>

</form>

</body>

</html>

session1.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String name=request.getParameter(“uname”);

out.print(“Welcome “+name);

session.setAttribute(“user”,name);

%>

<a href=“session2.jsp”>second jsp page</a>

</body>

</html>

session2.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String name=(String)session.getAttribute(“user”);

out.print(“Hello “+name);

%>

</body>

</html>

  1. pageContext Object:

JSP pageContext implicit object is part of javax.servlet.jsp.PageContext.

The pageContext object can be used to set the values, retrieve or remove the attribute from the following scopes.

  • page
  • request
  • session
  • application

Example of pageContext implicit object

Input.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String name=“SevenMentors”;

pageContext.setAttribute(“user”,name,pageContext.SESSION_SCOPE);

pageContext.setAttribute(“user”,name,pageContext.PAGE_SCOPE);

pageContext.setAttribute(“user”,name,pageContext.REQUEST_SCOPE);

pageContext.setAttribute(“user”,name,pageContext.APPLICATION_SCOPE);

String sname=(String)pageContext.getAttribute(“user”,pageContext.SESSION_SCOPE);

String pname=(String)pageContext.getAttribute(“user”,pageContext.PAGE_SCOPE);

String rname=(String)pageContext.getAttribute(“user”,pageContext.REQUEST_SCOPE);

String aname=(String)pageContext.getAttribute(“user”,pageContext.APPLICATION_SCOPE);

%>

<b>Value in Session scope &nbsp;<%=sname%></b><br>

<b>Value in Request scope &nbsp;<%=rname%></b><br>

<b>Value in Page scope &nbsp;<%=pname%></b><br>

<b>Value in Application scope &nbsp;<%=aname%></b><br>

<a href=“pageContext.jsp”>One jsp page</a>

</body>

</html>

pageContext.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String sname=(String)pageContext.getAttribute(“user”,pageContext.SESSION_SCOPE);

String pname=(String)pageContext.getAttribute(“user”,pageContext.PAGE_SCOPE);

String rname=(String)pageContext.getAttribute(“user”,pageContext.REQUEST_SCOPE);

String aname=(String)pageContext.getAttribute(“user”,pageContext.APPLICATION_SCOPE);

%>

<b>Value in Session scope &nbsp;<%=sname%></b><br>

<b>Value in Request scope &nbsp;<%=rname%></b><br>

<b>Value in Page scope &nbsp;<%=pname%></b><br>

<b>Value in Application scope &nbsp;<%=aname%></b><br>

</body>

</html>

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

  1. page Object:

JSP page implicit object is part of java.lang.Object class and represents the current JSP page. 

This object is not used frequently.

  1. exception Object:

JSP exception implicit object is part of java.lang.Throwable class and used to provide exception details in JSP error pages. While declaring this object, we should set the Jsp Page as an error page as it will not work in normal JSP pages. To handle the errors in Jsp this object is most suitable in all the cases.

Input.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8” isErrorPage=“true”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<form action=“operation.jsp”>

<table>

<tr><td>Enter first Number:</td><td><input type=“text” name=“firsrnum”></td></tr>

<tr><td>Enter second Number:</td><td><input type=“text” name=“secondnum”></td></tr>

<tr><td><input type=“submit” value=“Divide”></td></tr>

</table><br><br>

Result: &nbsp;&nbsp; <input type=“text” value=<%=session.getAttribute(“result”)%>>

</form>

</body>

</html>

operation.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8” isErrorPage=“true”

errorPage=“exception.jsp”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

<%

String num1=request.getParameter(“firsrnum”);

String num2=request.getParameter(“secondnum”);

int v1= Integer.parseInt(num1);

int v2= Integer.parseInt(num2);

int res= v1/v2;

session.setAttribute(“result”, res);

response.sendRedirect(“calculation.jsp”);

%>

</body>

</html>

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

exception.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8” isErrorPage=“true”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Insert title here</title>

</head>

<body>

Got this Exception: <%= exception %> <br><br>

<b>Please enter correct value</b>

</body></html>

Don’t forget to read my previous blog Servlet Interview Questions and Answers.

 

Author:-
Pallavi Kadam Software Trainer
SevenMentor Pvt Ltd.

 

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

Pin It on Pinterest

× How can I help you?