KEMBAR78
JSP Notes | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
8 views35 pages

JSP Notes

JSP (Java Server Pages) is a technology used for creating web applications, extending Servlet technology by offering features like expression language and JSTL. It allows for easier maintenance, faster development without recompilation, and requires less code compared to Servlets. The document also outlines the JSP processing lifecycle, implicit objects, scripting elements, and provides examples of using JSP for web applications.

Uploaded by

3832amangarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views35 pages

JSP Notes

JSP (Java Server Pages) is a technology used for creating web applications, extending Servlet technology by offering features like expression language and JSTL. It allows for easier maintenance, faster development without recompilation, and requires less code compared to Servlets. The document also outlines the JSP processing lifecycle, implicit objects, scripting elements, and provides examples of using JSP for web applications.

Uploaded by

3832amangarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

JSP

JSP (Java Server Pages)


• JSP technology is used to create web application just like Servlet
technology.

• It can be thought of as an extension to Servlet because it provides


more functionality than servlet such as expression language, JSTL
(JavaServer Pages Standard Tag Library), etc.
Cont…
• A JSP page consists of HTML tags and JSP tags.
Advantages of JSP over Servlet
(1) Extension to Servlet

• JSP technology is the extension to Servlet technology.

• We can use all the features of the Servlet in JSP.


Cont…
• In addition to, we can use predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.

(2) Easy to maintain


• JSP can be easily managed because we can easily separate our
business logic with presentation logic.
Cont…
• In Servlet technology, we mix our business logic with the presentation
logic.

(3) Fast Development: No need to recompile and redeploy


• If JSP page is modified, we don't need to recompile and redeploy the
project.
Cont…
• The Servlet code needs to be updated and recompiled if we have to
change the look and feel of the application.

(4) Less code than Servlet


• In JSP, we can use many tags such as action tags, JSTL, custom tags,
etc. that reduces the code. Moreover, we can use EL, implicit objects,
etc.
JSP Processing
• The following steps explain how the web server creates the Webpage using
JSP −
1. As with a normal page, your browser sends an HTTP request to the web
server.
2. The web server recognizes that the HTTP request is for a JSP page and
forwards it to a JSP engine.
3. The JSP engine loads the JSP page from disk and converts it into a servlet
content.
4. The JSP engine compiles the servlet into an executable class and forwards
the original request to a servlet engine.
5. A part of the web server called the servlet engine loads the Servlet class and
executes it.
6. The web server forwards the HTTP response to your browser.
JSP
Processing
JSP Processing
The Lifecycle of a JSP Page
• The JSP pages follow these phases:
• Translation of JSP Page
• Compilation of JSP Page
• Classloading (the classloader loads class file)
• Instantiation (Object of the Servlet is created).
• Initialization ( the container invokes jspInit() method).
• Request processing ( the container invokes _jspService() method).
• Destroy ( the container invokes jspDestroy() method).
Cont…
• As depicted in the above diagram, JSP page is translated into Servlet
by the help of JSP translator.

• The JSP translator is a part of the web server which is responsible for
translating the JSP page into Servlet.
Cont…
• After that, Servlet page is compiled by the compiler and gets
converted into the class file.

• Moreover, all the processes that happen in Servlet are performed on


JSP later like initialization, committing response to the browser and
destroy.
JSP Implicit Objects
• There are 9 jsp implicit objects.

• These objects are created by the web container that are available to all
the jsp pages.
Cont…
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
JSP Scripting elements

• The scripting elements provides the ability to insert java code inside
the jsp.

• There are three types of scripting elements:


• scriptlet tag
• expression tag
• declaration tag
1. JSP scriptlet tag
• A scriptlet tag is used to execute java source code in JSP.

• Syntax
<% java source code %>
Q: WAP to print any message
in a JSP.
Ans:

<% out.println("welcome to jsp"); %>


2. JSP expression tag
• The code placed within JSP expression tag is written to the output
stream of the response.

• So you need not write out.print() to write data.

• It is mainly used to print the values of variable or method.


Cont…
• Syntax
<%= statement %>

Q: WAP to print any message in JSP.


Ans: <%= "welcome to jsp" %>

Note: Do not end your statement with semicolon in case of expression


tag.
Cont…
Q: WAP to print current date and Time in JSP.
Ans.

<%@ page import="java.util.Date"%>


Today is: <%= new Date() %>
3. JSP Declaration Tag
• The JSP declaration tag is used to declare variable and method.

• Syntax
<%! variable or method declaration %>
Example
Q: WAP to declare any variable and print the value of that variable.

Ans:
<%! int data=50; %>
<%= data %>
JSP request implicit object (The JSP Request
Class)
• The JSP request is an implicit object of type HttpServletRequest.

• It is created by the web container.

• It can be used to get request information such as parameter etc.


Method of HttpServletRequest
Interface
• public String getMethod(): This method returns the name of the
HTTP method with which this request was made, for example, GET,
POST, or PUT etc.

• public String getParameter(String name): This function returns the


value of a request parameter as a String , or null if the parameter does
not exist
Example (Accessing a JSP using
an HTML page)
• In this example, we have created two files index.html and
welcome.jsp. The index.html file gets the username from the user and
the welcome.jsp file prints the username with the welcome message.

Index.html welcome.jsp (JSP)


Index.html
<html>
<body>
<form action="welcome.jsp">
User Name: <input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
welcome.jsp

<%
String name=request.getParameter("name");
out.print("Welcome "+name);
%>
JSP response implicit object (The JSP Response
Class)
• The response is an implicit object of type HttpServletResponse.

• It is created by the web container.

• It can be used to add or manipulate response such send error etc.


Method of HttpServletResponse Interface
• public void setContentType(String str): By using this method the
Servlet container informs to the client browser of what type of data is
being sent now.

• void sendError(int sc, String msg): sends an error response to the


client using the specified status
Example
• In this example, we have created two files index.html and
welcome1.jsp where we are redirecting the response to the Google.

Index.html welcome1.jsp (JSP)


Index.html
<html>
<body>
<form action="welcome1.jsp">
User Name: <input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
welcome1.jsp

<%
response.sendRedirect("http://www.google.com");
%>
Write a program to print table
using JSP.
<%
int i, n=5;
for(i=1;i<=10;i++)
{
out.print(n*i);
%>
<br>
<%
}
%>

You might also like