Uploaded on

Presentation on JSP

Presentation on JSP

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
185
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
16
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Java Server Pages Team Emertxe
  • 2. Java Server Pages
  • 3. Java Server Pages ✔ JSP Stands for Java Server Pages, which allows easy creation and maintenance of server side HTML pages. ✔ JSP can be easily managed because we can easily separate our business logic with presentation logic. ✔ In servlet technology, we combine our business logic with the presentation logic. ✔ JSP consists of HTML tags and JSP tags. ✔ Each and every JSP will be translated into the corresponding servlet.
  • 4. client HTTP Request Web Server Servlet Engine JSP Engine JVM Web Server Software Other Server Extensions Java runtime Architecture
  • 5. Life cycle of JSP The JSP pages follows these phases: ✔ Translation of JSP Page ✔ Compilation of JSP Page ✔ Classloading (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).
  • 6. Life cycle of JSP jspInit() : The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance. _jspservice(): The container calls this method for each request, passing it the request and response object. jspDestroy() : The container calls this when it decides and take the instance out of service. It is the last method called in the servlet instance.
  • 7. The JSP API The JSP API consists of two packages: ✔ javax.servlet.jsp ✔ javax.servlet.jsp.tagext javax.servlet.jsp : This package consists of two interfaces and classes. The two interfaces are as follows. ✔ JspPage ✔ HttpJspPage ✔ The classes are as follows ✔ JspWriter ✔ PageContext ✔ JspFactory ✔ JspEngineInfo ✔ JspException ✔ JspError
  • 8. 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 ✔ public void jspInit() ✔ public void jspDestroy() The HttpJspPage interface : The HttpJspPage interface provides the one life cycle method of JSP. It extends the JspPage interface. This package consists of folowing method. ✔ public void _jspService(): The JSP API
  • 9. Scripting Elements ✔ Scripting elements allows you to insert Java code into servlet that will be generated from JSP current page. ✔ There are three types of Scripting elements ✔ Expressions ✔ Scriptlets ✔ Declarations
  • 10. Expressions ✔ A JSP Expression is used to insert java values directly into the output. ✔ So, you need not write out.print() to print data. ✔ It is mainly used to print the values of variable or method. Syntax: <%= Java Expression%> Example <html> <head> JSP example </head> <body> <br> <%="Welcome to India" %> </body> </html>
  • 11. Scriptlets ✔ Scriplets are defined as any block of valid java code that resides between <% and %> tags. ✔ Code that is defined within a scriplet can access any variable that have been declared. Syntax: <% Java Code %> <html> <head> JSP scriptlet example </head> <body> <%out.println(new Date().toString()); %> </body> </html>
  • 12. Declarations ✔ A JSP declaration allows you to declare methods or fields that get inserted in to the main body of the servlet class. ✔ The code written inside the JSP declaration tag is placed outside the service() method of servlet. Syntax : <%! Java Code %> <html> <body> <%! int a=15,b=30; %> <%="a+b = "+(a+b) %> </body> </html>
  • 13. Implicit Objects ✔ Implicit objects are created by the web container that are available to all JSP pages.These are used to simplify the expressions. ✔ There are 9 implicit objects. They are as follows. Object Type out JspWriter request HttpServletRequest response HttpServletResponse session HttpSession application ServletContext config ServetConfig pageContext PageContext page Object exception Throwable
  • 14. Implicit Objects ✔ out : It is used to print data and it is of type JspWriter. In Servlet we have to write : PrintWriter out= response.getWriter(); But in jsp, we can write like this : <%out.println(new Date().toString()); %> ✔ request : It is of type HTTP Request <%String name=request.getParameter("username");   out.print("welcome "+name); %> ✔ response : It is of type HttpServletResponse <% response.sendRedirect("www.emertxe.com"); %> ✔ session: It is of type HttpSession. session.setAttribute("city",name);   
  • 15. Directive Elements✔ A JSp directive effects the over all structure of servlet class. ✔ It is a message that tells the web container how to translate a JSP page into corresponding servlet. Ther are 3 types of directive elements. ✔ page directive. ✔ include directive. ✔ taglib directive. Syntax<%@ directive attribute=”value”%>
  • 16. page directive helps you to customize the servlet super class. It defines attributes that apply to entire JSP page. Attributes of JSP page are ✔ buffer ✔ autoFlush ✔ import ✔ contentType ✔ language ✔ isErrorPage ✔ errorPage ✔ extends ✔ isThreadSafe ✔ session ✔ pageEncoding ✔ info Note: Using page directive we can import classes, handle error messages, define if JSP is thread-safe, define is session object is available, set the page content type. page directive <%@ page buffer=”56kb” %> <%@ page import=” java.sql.*” %> <%@ page contentType=application %> <%@ page info=”created by emertxe” %> <%@ page errorPage=”myerrorpage.jsp” %> <%@ page isThreadSafe= “true” %> <%@ page isErrorPage=”true” %>
  • 17. Include and taglib directive ✔ The include directive is used to insert the contents of another file to main JSP file, where the directive is located. ✔ The included file can be a JSP file, HTML file text file or Java code file. ✔ It is useful for including copyright information, scripting language files. ✔ The taglib directive is used to define tag library that define many tags. ✔ TLD is used to define the tags. <%@ include file="Hello.html" %> <%@ taglib uri="uri" prefix="prefixof tag" >
  • 18. Exception Handling in JSP ✔ Exception Handling is used to handle runtime errors. ✔ It may occur at part of code in our web application. It is safe to handle exceptions in web applications. Ther are two ways to handle exceptions in JSP. ✔ errorPage and isErrorPage attributes of page directive ✔ <error-page> element in web.xml file. For example we read two numbers from Html page into num1 ,num2. <html> <form action=”sample.jsp”> Number1 :<input type=”text” name=”num1”> Number2 : <input type=”text name= “num2”> <input type=”submit” value=”click”> </form> </html>
  • 19. //Sample.jsp <%@ page errorPage="error.jsp" %> <% Int m=Integer.parseInt(request.getParameter("num1")); int n=Integer.parseInt(request.getParameter("num2")); Int p=m/n; out.print(" division of numbers is: " + p); %> If num2 is equal to Zero(=0), it goes to error.jsp file display //error.jsp <%@ page isErrorPage="true" %> <h3>Division by Zero Excpetion occured.</h3> Exception is: <%= exception %> Exception Handling in JSP
  • 20. JSP Action tags Action tags are used to control the behaviour of servlet engine. We can dynamically insert a file, forward the user to another page, or generate HTML. JSP Action tags are as follows ✔ jsp:include - It is used to include a file at the time the page is requested. ✔ jsp:useBean - It is used to find or instantiate a JavaBean. ✔ Jsp:setProperty - It is used to set the property of a JavaBean. ✔ Jsp:getProperty - It is used to get the property to output. ✔ Jsp:forward - is used to forward the request to a new page. ✔ Jsp:plugin - It is used to generate browser-specific code that makes an OBJECT or EMBED tag for the Java plug-in. Java Bean: It is a Java class with following properties. ✔ Beans must supply a Zero-argument Constructor. ✔ Beans must provide getter and setter methods.
  • 21. Applet in JSP ✔ To embedd applet in jsp file we have to use jsp:plugin action tag. ✔ At client side jsp:plugin action tag downloads a plugin to run applet. Syntax: <jsp:plugin type= "applet | bean" code= "name of class file" codebase= "directory name of class file" </jsp:plugin>
  • 22. public class ButtonDemo extends Applet implements ActionListener { String msg=" "; Button yes; public void init() { yes=new Button("yes"); add(yes); yes.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("yes")) { msg="You Pressed Yes"; } } public void paint(Graphics g) { g.drawString(msg, 50, 100); } } //Button.jsp <html> <body> <jsp:plugin align="middle" height="300" width="400" type="applet" code="ButtonDemo.class" name="click" codebase="."/> </body> </html> Applet in JSP
  • 23. ✔ EL is language that enables JSP developers to access application data stored in Java Bean components. ✔ EL expressions are enclosed between “${“ and “}” Types of EL Expressions : ✔ Immediate and deferred expressions ✔ Value expressions ✔ Method expressions Expression Language(EL) in JSP Syntax: ${ expression }
  • 24. Immediate and Deferred Expressions ${expr} : It is used for expressions that need to be evaluated immediately. This is called Immediate expression. ✔ #{expr} : It is used for expressions that are evaluated at a later time. This is called deferred expression. Value expressions : This is used to refer to objects such as Java Beans, collection, enumerations, and implicit objects and their properties. Example : public enum mobiles{samsung, nokia, apple, htc } ${mobiles.nokia} Method expressions are used to call public methods , which return a value or object. These are usually called as deferred expressions. Expression Language(EL) in JSP
  • 25. EL operators ✔ Arithmetic operators : All types of arithmetic operators(+, -, *, / , %) are used in EL expressions. ✔ Relational operators : The empty operator : This operator is used to check for empty values, which differ according to data type of operand. > gt >= ge < lt <= le == eq != ne
  • 26. Implicit objects in EL API ✔ pageContext ✔ pageScope ✔ requestScope ✔ sessionScope ✔ applicationScope ✔ param ✔ paramValues ✔ header ✔ headerValues ✔ cookie ✔ InitParam //sample.jsp <h1>Emertxe</h1>   <% session.setAttribute(“name",“ implicitobject");   %>     <a href=“show.jsp">visit</a>   //show.jsp Value is ${ sessionScope.name }  
  • 27. MVC in JSP Browser Servlet JSP Java Bean DB Client request response Create Java Bean Retrieve data from database Use Java Bean Invoke JSP Application Server
  • 28. JSTL(JSP Standard Tag Library) ✔ The JSTL is a collection of custom tag libraries, which provide core functionality used for JSP documents. ✔ JSTL reduces the use of scriptlets in a JSP page. ✔ It helps developers to use predefined tags instead of writing the Java code. There are four types of tag libraries that can be used with JSP pages. ✔ The Core JSTL : Used to process a JSP page in an application. ✔ The XML tag library : Used for parsing, selecting, and transforming XML data in a JSP page. ✔ The Format Tag library : Used for formatting the data in a JSP page. ✔ The SQL Tag library : Used to access the relational database used in a JSP pages
  • 29. JSTL Core Tags The core tags are used to perform iteration, conditional processing, and also support of expression language for tags in JSP pages. The JSTL Core Tags are divided into following categories ✔ General-purpose Tags. ✔ Conditional and looping Tags. ✔ Networking Tags. General-purpose Tags : ✔ <c:out> ✔ <c:set> ✔ <c:remove> ✔ <c:catch>
  • 30. //core.jsp <form action="show.jsp" method="post"> FirstName:<input type="text" name="fname"/><br/> LastName:<input type="text" name="lname"/><br/> <input type="submit" value="submit"/> </form> //show.jsp <%@ taglib uri=" " prefix="c" %> First Name:<c:out value="${param.fname}"></c:out><br/> Last Name:<c:out value="${param.lname}"></c:out> General-purpose Tags :
  • 31. Conditional and looping Tags : ✔ <c: if> ✔ <c: choose> ✔ <c: when> ✔ <c: otherwise> ✔ <c: forEach> ✔ <c: forToken> Networking Tags ✔ <c: import> ✔ <c: url> ✔ <c: redirect> ✔ <c: param> JSTL Core Tags
  • 32. Custom Tags ✔ The custom tags are user defined sets of the JSP element present in JSP tag libraries. ✔ They eliminates the possibility of scriptlet tag and separates the business logic from the JSP page. ✔ The custom tags used in the JSP page are translated into a servlet. ✔ The custom tags pass the attributes from current page to the calling page within an application. ✔ The custom tags can communicate with each other. Need of Custom Tags : ✔ Reusability ✔ Readability ✔ Maintainability
  • 33. There are two ways to use custom tags. Syntax: ✔ <prefix:tagname attr1=value1....attrn=valuen /> ✔ <prefix:tagname attr1=value1....attrn=valuen > body code </prefix:tagname> Custom Tags
  • 34. THANK YOU