Jsp Introduction Tutorial
description
Transcript of Jsp Introduction Tutorial
![Page 1: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/1.jpg)
Java Server Page
• Deisigned By:• Shivendra Srivastwa• Java and Android consultant
![Page 2: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/2.jpg)
Java Server Page (JSP)JSP technology is used to create web
application just like Servlet technology. It can be thought of as an extension to the servlet because it provides more functionality than servlet.
A JSP page contains HTML code and JSP tags. The jsp pages are easier to maintain than servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tag etc.
![Page 3: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/3.jpg)
Advantage of JSP over ServletJSP is the extension to the servlet technology. We
can use all the features of Servlet in JSP. In addition to, we can use implicit objects,
predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.
JSP can be easily managed because we can easily separate our business logic with presentation logic. In servet, we mix our business logic with the presentation logic.
If JSP page is modified, we don't need to redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.
![Page 4: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/4.jpg)
Life cycle of a JSP PageCompilation of JSP PageClassloading (class file is loaded by the
classloader)Instantiation (Object of the Generated
Servlet is created).Initialization ( jspInit() method is
invoked by the container).Reqeust processing ( _jspService()
method is invoked by the container).Destroy ( jspDestroy() method is
invoked by the container)
![Page 5: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/5.jpg)
JSP page is translated into servlet by the help of JSP translator. The JSP translator is a part of webserver that is responsible to translate the JSP page into servlet. Afterthat Servlet page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that happens in servlet is performed on JSP later like initialization, commiting response to the browser and destroy.
![Page 6: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/6.jpg)
Directory structure of JSP
![Page 7: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/7.jpg)
The JSP API
The JSP API consists of two packages:
1. javax.servlet.jsp package2. javax.servlet.jsp.tagext
![Page 8: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/8.jpg)
javax.servlet.jsp package
The Interfaces are as follows: 1. JspPage 2.
HttpJspPageThe classes are as follows: 1. JspWriter2. PageContext3. JspFactory4.JspEngineInfo5.JspExceptionJspError
![Page 9: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/9.jpg)
The JspPage interface
According to the JSP specification, all the generated servlet classes must implement the JspPage interface. It extends the Servlet interface. It provides two life cycle methods.
![Page 10: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/10.jpg)
METHODS OF JSPPAGE INTERFACEpublic void jspInit()
It is invoked only once during the life cycle of the JSP when JSP page is requested firstly. It is used to perform initialization. It is same as the init() method of Servlet interface.
public void jspDestroy():
It is invoked only once during the life cycle of the JSP before the JSP page is destroyed. It can be used to perform some clean up operation.
![Page 11: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/11.jpg)
THE HTTPJSPPAGE INTERFACEpublic void _jspService():It is invoked each time when request for the JSP page comes to the container. It is used to process the request. The underscore _ signifies that you cannot override this method.
![Page 12: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/12.jpg)
JSP scripting ElementThe scripting elements provides
the ability to insert java code inside the jsp. There are three types of scripting elements:
Scripting elements:◦1. scriptlet tag◦2. expression tag◦3. declaration tag
![Page 13: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/13.jpg)
Scriptlet tagA scriptlet tag is used to execute
java source code in JSP. Syntax is as follows:
Syntax:<% java source code
%>
![Page 14: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/14.jpg)
JSP expression tagThe code placed within
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.
Syntax: <%= statement %>
![Page 15: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/15.jpg)
JSP declaration tagThe JSP declaration tag is used to
declare fields and methods. The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet. So it doesn't get memory at each request
Syntax: <%! statement %>
![Page 16: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/16.jpg)
Difference between the jsp scriptlet tag and jsp declaration tag The jsp scriptlet tag can only
declare variables not methods whereas jsp declaration tag can declare variables as well as methods.
The declaration of scriptlet tag is placed inside the _jspService() method whereas the declaration of jsp declaration tag is placed outside the _jspService() method.
![Page 17: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/17.jpg)
Implicit Object
There are 9 implicit objects available for the JSP page. The Auto Generated Servlet contains many objects like out, request, config, session, application etc.
![Page 18: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/18.jpg)
![Page 19: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/19.jpg)
out implicit objectFor writing any data to the buffer,
JSP provides an implicit object named out.
Syntax: out.print();
![Page 20: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/20.jpg)
request implicit objectIn JSP, request is an implicit
object of type HttpServletRequest. Let's see the simple example of request implicit object where we are printing the name of the user with welcome message.
eg: request.getParameter
![Page 21: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/21.jpg)
response implicit objectIn JSP, response is an implicit
object of type HttpServletResponse. Let's see the example of response implicit object where we are redirecting the request to the Google.
![Page 22: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/22.jpg)
config implicit objectIn JSP, config is an implicit object
of type ServletConfig. This object can be used to get configuration information for a particular JSP page.
This variable information can be used for one jsp page only.
![Page 23: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/23.jpg)
application implicit objectIn JSP, application is an implicit
object of type ServletContext.This object can be used to get configuration information from configuaration file(web.xml). This variable information can be used for all jsp pages.
![Page 24: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/24.jpg)
session implicit objectIn JSP, session is an implicit
object of type HttpSession.The Java developer can use this object to set,get or remove attribute or to get session information.
![Page 25: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/25.jpg)
pageContext implicit objectIn JSP, pageContext is an implicit
object of type PageContext class.The pageContext object can be used to set,get or remove attribute from one of the following scopes:
i. Page ii. RequestIii. Session iv. ApplicationNote: page scope is the default
scope
![Page 26: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/26.jpg)
exception implicit objectIn JSP, exception is an implicit
object of type java.lang.Throwable class. This object can be used to print the exception. But it can only be used in error pages.It is better to learn it after page directive. Let's see a simple example:
![Page 27: Jsp Introduction Tutorial](https://reader035.fdocuments.us/reader035/viewer/2022081721/54b666834a79595f448b456c/html5/thumbnails/27.jpg)
Happy Programming with JSP
Thanks…………