Introduction to Java Server Pages (JSP) in Web Development
Explore the fundamentals of JSP, a server-side technology for building dynamic web applications using Java code and HTML. Learn about JSP declaration, scriptlet, expression tags, and page directives to enhance web development capabilities efficiently.
Download Presentation
![](/assets/img/so-down.gif)
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Introduction to JSP Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction to JSP : Dept. of B.Voc SD&SA 1
JSP JSP (Java Server Pages) is server side technology to create dynamic java web application. JSP can be thought as an extension to servlet technology because it provides features to easily create user views. JSP Page consists of HTML code and provide option to include java code for dynamic content. Introduction to JSP : Dept. of B.Voc SD&SA 2
Since web applications contain a lot of user screens, JSPs are used a lot in web applications. To bridge the gap between java code and HTML in JSP, it provides additional features such as JSP Tags, Expression Language, Custom tags. This makes it easy to understand and helps a web developer to quickly develop JSP pages. Introduction to JSP : Dept. of B.Voc SD&SA 3
JSP Declaration Tag The JSP declaration tag is used to declare fields and methods. <html> <body> <%! int data=50; %> <%= "Value of the variable is:"+data %> </body> </html> Introduction to JSP : Dept. of B.Voc SD&SA 4
JSP Scriptlet tag In JSP, java code can be written inside the jsp page using the scriptlet tag. <html> <body> <% out.print("welcome to jsp"); %> </body> </html> JSP expression tag The code placed within JSP expression tag is written to the output stream of the response. <html> <body> <%= "welcome to jsp" %> </body> </html> Introduction to JSP : Dept. of B.Voc SD&SA 5
Page Directive There are several attributes, which are used along with Page Directives and these are import session isErrorPage errorPage ContentType isThreadSafe extends info language autoflush Introduction to JSP : Dept. of B.Voc SD&SA 6 Buffer
<%@page import="java.io.*%> <%@page import="java.lang.*%> <%--Comment: OR Below Statement: Both are Same--%> <%@page import="java.io.*, java.lang.*"%> Introduction to JSP : Dept. of B.Voc SD&SA 7
<%@ page errorPage="ExceptionHandling.jsp"%> This means if any exception occurs on the JSP page where this code has been placed, the ExceptionHandling.jsp (this page should have isErrorPage true) page needs to be called. Introduction to JSP : Dept. of B.Voc SD&SA 8
JSP implicit object Implicit Object Description request The HttpServletRequest object associated with the request. response The HttpServletRequest object associated with the response that is sent back to the browser. The JspWriter object associated with the output stream of the response. out session The HttpSession object associated with the session for the given user of request. application The ServletContext object for the web application. config The ServletConfig object associated with the servlet for current JSP page. pageContext The PageContext object that encapsulates the enviroment of a single request for this current JSP page page The page variable is equivalent to this variable of Java programming language. exception The exception object represents the Throwable object that was thrown by some other JSP page. Introduction to JSP : Dept. of B.Voc SD&SA 9
JSP Action JSP actions use the construct in XML syntax to control the behavior of the servlet engine. can dynamically insert a file, reuse the beans components, forward user to another page, etc. through JSP Actions like include and forward. Unlike directives, actions are re-evaluated each time the page is accessed. Syntax: <jsp:action_name attribute="value" /> Introduction to JSP : Dept. of B.Voc SD&SA 10
There are 11 types of action names as following: jsp:useBean jsp:include jsp:setProperty jsp:getProperty jsp:forward jsp:plugin jsp:attribute jsp:body jsp:text jsp:param jsp:attribute jsp:output Introduction to JSP : Dept. of B.Voc SD&SA 11
Action Tag jsp:forward Description forward the request to a new page Usage : <jsp:forward page="Relative URL" /> jsp:useBean instantiates a JavaBean Usage : <jsp:useBean id="beanId" /> jsp:getProperty retrieves a property from a JavaBean instance. Usage : <jsp:useBean id="beanId" ... /> ... <jsp:getProperty name="beanId" property="someProperty" .../> Where, beanName is the name of pre-defined bean whose property we want to access. Introduction to JSP : Dept. of B.Voc SD&SA 12
jsp:setProperty store data in property of any JavaBeans instance. Usage : <jsp:useBean id="beanId" ... /> ... <jsp:setProperty name="beanId" property="someProperty" value="some value"/> Where, beanName is the name of pre-defined bean whose property we want to access. jsp:include includes the runtime response of a JSP page into the current page. Generates client browser-specific construct that makes an OBJECT or EMBED tag for the Java Applets jsp:plugin Introduction to JSP : Dept. of B.Voc SD&SA 13
jsp:fallback Supplies alternate text if java plugin is unavailable on the client. You can print a message using this, if the included jsp plugin is not loaded. jsp:element Defines XML elements dynamically jsp:attribute defines dynamically defined XML element's attribute jsp:body Used within standard or custom tags to supply the tag body. jsp:param Adds parameters to the request object. jsp:text Used to write template text in JSP pages and documents. Usage : <jsp:text>Template data</jsp:text> Introduction to JSP : Dept. of B.Voc SD&SA 14
welcome.jsp <html> <head> <title>Welcome Page</title> </head> <body> <%@ include file="header.jsp" %> Welcome, User </body> </html> header.jsp <html> <body> <img src="header.jpg" alt="This is Header image" / > </body> </html> Introduction to JSP : Dept. of B.Voc SD&SA 15