Page 1 :
Java Server Page (JSP), What is JSP?, 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, etc., , Advantages of JSP, 1) Extension to Servlet, 2) Easy to maintain, 3) Fast Development: No need to recompile and redeploy, 4) Less code than Servlet, , Life Cycle of a JSP Page, The JSP page follows these phases:, , , , , , , , , Translation of JSP Page, Compilation of JSP Page, Class loading (class file is loaded by the class loader), Instantiation (Object of the Generated Servlet is created)., Initialization ( jspInit() method is invoked by the container)., Request processing ( _jspService() method is invoked by the container)., Destroy ( jspDestroy() method is invoked by the container)., , Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of, JSP.
Page 3 :
Directory Structure of JSP, The directory structure of JSP page is same as Servlet. We contains the JSP page, outside the WEB-INF folder or in any directory., , Scripting Elements, The scripting element provides the ability to insert java code inside the JSP. There are, three types of scripting elements:, , , , , Scriptlet tag, Expression tag, Declaration tag, , JSP Scriptlet Tag, A Sriptlet tag is used to execute java source code in JSP. Syntax is as follows:, <% java source code %>, You can write XML equivalent of the above syntax as follows:, <jsp:scriptlet>, code fragment, </jsp:scriptlet>
Page 4 :
Simple Example of JSP Scriptlet Tag, In this example, we are displaying a welcome message., 1., 2., 3., 4., 5., , <html>, <body>, <% out.print("welcome to jsp"); %>, </body>, </html>, , Example of JSP Scriptlet tag that prints the user name, 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, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <form action="welcome.jsp">, <input type="text" name="uname">, <input type="submit" value="go"><br/>, </form>, </body>, </html>, , welcome.jsp, 1., 2., 3., 4., 5., 6., 7., 8., 9., , <html>, <body>, <%, String name=request.getParameter("uname");, out.print("welcome "+name);, %>, </form>, </body>, </html>
Page 5 :
JSP Expression Tag, The 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 of JSP Expression Tag, <%= statement %>, You can write XML equivalent of the above syntax as follows:, <jsp:expression>, expression, </jsp:expression>, , Example of JSP Expression Tag, In this example of JSP expression tag, we are simply displaying a welcome message., 1., 2., 3., 4., 5., , <html>, <body>, <%= "welcome to jsp" %>, </body>, , 6. </html>, , Note: Do not end your statement with semicolon in case of expression tag., , Example of JSP expression tag that prints current time, To display the current time, we have used the getTime() method of Calendar class. The, getTime() is an instance method of Calendar class, so we have called it after getting the, instance of Calendar class by the getInstance() method., , index.jsp, 1., 2., 3., 4., , <html>, <body>, Current Time: <%= java.util.Calendar.getInstance().getTime() %>, </body>, 5. </html>
Page 6 :
Example of JSP expression tag that prints the user name, In this example, we are printing the username using the expression tag. The index.html, file gets the username and sends the request to the welcome.jsp file, which displays the, username., , index.html, 1., 2., 3., 4., 5., 6., 7., 8., 9., , <html>, <body>, <form action="welcome.jsp">, <input type="text" name="uname"><br/>, <input type="submit" value="go">, </form>, </body>, </html>, , welcome.jsp, 1., 2., 3., 4., 5., 6., , <html>, <body>, <%= "Welcome "+request.getParameter("uname") %>, </form>, </body>, </html>, , JSP Declaration Tag, The 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 of JSP declaration tag, The syntax of the declaration tag is as follows:, <%! field or method declaration %>, You can write XML equivalent of the above syntax as follows:, <jsp:declaration>, code fragment, </jsp:declaration>
Page 7 :
Difference between the JSP scriptlet tag and JSP, declaration tag ?, JSP Scriptlet Tag, , JSP Declaration Tag, , TheJSP scriptlet tag can only declare, variables not methods., , The JSP declaration tag can declare variables, as well as methods., , The declaration of scriptlet tag is placed, inside the _jspService() method., , The declaration of JSP declaration tag is, placed outside the _jspService() method., , Example of JSP declaration tag that declares field, In this example of JSP declaration tag, we are declaring the field and printing the value, of the declared field using the JSP expression tag., , index.jsp, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <%! int data=50; %>, <%= "Value of the variable is:"+data %>, </body>, </html>, , Example of JSP declaration tag that declares method, In this example of JSP declaration tag, we are defining the method which returns the, cube of given number and calling this method from the JSP expression tag. But we can, also use JSP scriptlet tag to call the declared method., , index.jsp, 1. <html>, 2. <body>, 3., 4. <%!, 5. int cube(int n){, 6. return n*n*n*;, 7. }, 8. %>, 9., 10., <%= "Cube of 3 is:"+cube(3) %>, 11.
Page 11 :
second.jsp, 1., 2., 3., 4., 5., , <html>, <body>, <%, , String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCO, PE);, 6. out.print("Hello "+name);, 7., 8. %>, 9. </body>, 10., </html>, , JSP directives, The jsp directives are messages that tells the web container how to translate a JSP, page into the corresponding servlet., There are three types of directives:, , , , , page directive, include directive, taglib directive, , Syntax of JSP Directive, <%@ directive attribute="value" %>, JSP page directive, The page directive defines attributes that apply to an entire JSP page., Syntax of JSP page directive, <%@ page attribute="value" %>, Attributes of JSP page directive, , , , , , , , , , , import, contentType, extends, info, buffer, language, isELIgnored, isThreadSafe, autoFlush
Page 12 :
session, pageEncoding, errorPage, isErrorPage, , , , 1), , import, , The import attribute is used to import class, interface or all the members of a package., It is similar to import keyword in java class or interface., , Example of import attribute, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <%@ page import="java.util.Date" %>, Today is: <%= new Date() %>, </body>, </html>, , 2), , contentType, , The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type, of the HTTP response.The default value is "text/html;charset=ISO-8859-1"., Example of contentType attribute, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <%@ page contentType=application/msword %>, Today is: <%= new java.util.Date() %>, </body>, </html>, , 3), , extends, , The extends attribute defines the parent class that will be inherited by the generated
Page 13 :
servlet. It is rarely used., , 4), , info, , This attribute simply sets the information of the JSP page which is retrieved later by, using getServletInfo() method of Servlet interface., Example of info attribute, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <%@ page info="composed by Sonoo Jaiswal" %>, Today is: <%= new java.util.Date() %>, </body>, </html>, , The web container will create a method getServletInfo() in the resulting servlet. For, example:, 1. public String getServletInfo() {, 2. return "composed by Sonoo Jaiswal";, 3. }, , 5), , buffer, , The buffer attribute sets the buffer size in kilobytes to handle output generated by the, JSP page. The default size of the buffer is 8Kb., Example of buffer attribute, 1., 2., 3., 4., 5., 6., 7., 8., , <html>, <body>, <%@ page buffer="16kb" %>, Today is: <%= new java.util.Date() %>, </body>, </html>
Page 14 :
6), , language, , The language attribute specifies the scripting language used in the JSP page. The default, value is "java"., , 7), , isThreadSafe, , Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP, page, you can use isThreadSafe attribute of page directive.The value of isThreadSafe, value is true.If you make it false, the web container will serialize the multiple requests,, i.e. it will wait until the JSP finishes responding to a request before passing another, request to it.If you make the value of isThreadSafe attribute like:, <%@ page isThreadSafe="false" %>, The web container in such a case, will generate the servlet as:, 1. public class SimplePage_jsp extends HttpJspBase, 2. implements SingleThreadModel{, 3. ......., 4. }, , 8), , errorPage, , The errorPage attribute is used to define the error page, if exception occurs in the, current page, it will be redirected to the error page., Example of errorPage attribute, 1. //index.jsp, 2. <html>, 3. <body>, 4., 5. <%@ page errorPage="myerrorpage.jsp" %>, 6., 7. <%= 100/0 %>, 8., 9. </body>, 10., </html>
Page 15 :
9), , isErrorPage, , The isErrorPage attribute is used to declare that the current page is the error page., Note: The exception object can only be used in the error page., Example of isErrorPage attribute, 1. //myerrorpage.jsp, 2. <html>, 3. <body>, 4., 5. <%@ page isErrorPage="true" %>, 6., 7. Sorry an exception occured!<br>, 8. The exception is: <%= exception %>, 9., 10., </body>, 11., </html>, , Include directive, The include directive is used to include the contents of any resource it may be JSP file,, html file or text file. The include directive includes the original content of the included, resource at page translation time (the JSP page is translated only once so it will be, better to include static resource)., Advantage of Include directive, Code Reusability, Syntax of include directive, 1. <%@ include file="resourceName" %>, Example of include directive, In this example, we are including the content of the header.html file. To run this, example you must create an header.html file., 1., 2., 3., 4., 5., 6., 7., , <html>, <body>, <%@ include file="header.html" %>, Today is: <%= java.util.Calendar.getInstance().getTime() %>
Page 20 :
Reading Cookies with JSP, <html>, <body>, <center>, <h1>Reading Cookies</h1>, </center>, <%, Cookie cookie = null;, Cookie[] cookies = null;, // Get an array of Cookies associated with this domain, cookies = request.getCookies();, if( cookies != null ){, out.println("<h2> Found Cookies Name and Value</h2>");, for (int i = 0; i < cookies.length; i++){, cookie = cookies[i];, out.print("Name : " + cookie.getName( ) + ", ");, out.print("Value: " + cookie.getValue( )+" <br/>");, }, }else{, out.println("<h2>No cookies founds</h2>");, }, %>, </body>, </html>, , Delete Cookies with JSP, To delete cookies is very simple. If you want to delete a cookie then you simply need to, follow up following three steps:, Read an already existing cookie and store it in Cookie object., Set cookie age as zero using setMaxAge() method to delete an existing cookie., Add this cookie back into response header., , , , Example:, <html>, <body>, <center>, <h1>Reading Cookies</h1>, </center>, <%, Cookie cookie = null;, Cookie[] cookies = null;, // Get an array of Cookies associated with this domain, cookies = request.getCookies();, if( cookies != null ){, out.println("<h2> Found Cookies Name and Value</h2>");, for (int i = 0; i < cookies.length; i++){