Java Server Pages JSP
Agenda Introduction JSP Architecture Scripting Elements Directives Implicit Objects 2
A way to create dynamic web pages Introduction Separates the graphical design from the dynamic content A JSP page is a text document that contains: Static data (such as HTML, WML, and XML) JSP elements, which construct dynamic content. 3
First Look at Code <html> <body> I am HTML code. <br> <b><% out.println("i am JSP Code"); %></b><br> <b><% out.println("today date & time is: "+java.time.localdatetime.now()); %></b> </body> </html> 4
Processing SRC:https://www.tutorialspoint.com/jsp/jsp_architecture.htm 5
Life Cycle SRC: https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm 6
JSP/Servlet Correspondence Original JSP <%= printme() %> <% callme(); %> Possible resulting servlet code public void _jspservice(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { request.setcontenttype("text/html"); HttpSession session = request.getsession(true); JspWriter out = response.getwriter(); out.println(printme()); callme();... } 7
API Packages javax.servlet.jsp javax.servlet.jsp.tagext javax.servlet.jsp package Interfaces JspPage HttpJspPage Classes JspWriter PageContext JspFactory JspEngineInfo JspException JspError 8
Components Scripting Elements Directives Implicit Objects Actions 9
Scripting Elements Provides the ability to insert java code inside the jsp Three forms: Expressions of the form <%= expression %> that are evaluated and inserted into the output Scriptlets of the form <% code %> that are inserted into the servlet's service method Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods. 10
Expression <% = expression %> <jsp:expression> expression </jsp:expression> Example: <%= java.time.localdate.now() %> 11
Scriptlet <% code fragment %> <jsp:scriptlet> code fragment </jsp:scriptlet> Example: <% out.println("your IP address is " + request.getremoteaddr()); %> 12
Declaration <%! declaration; %> <jsp:declaration> code fragment </jsp:declaration> <%! int i = 0; %> <%! int i; %> <%! Student student = new Student(); %> <%! int area(int a, int b){ return a*b; } %> 13
Example: Math.html <form method="post" action="math.jsp"> <input type="radio" name= operator" value="add" checked>addition<br> <input type="radio" name= operator" value= subtract" >Subtraction<br> <input type="radio" name= operator" value="multiply" >Multiplication<br> <input type="radio" name="operator" value="divide" >Division<br> <br><br> Enter first Value <input type="text" name= input1" value=""><br> Enter second Value <input type="text" name= input2" value=""><br> <input type="submit" name="result"> </form> 14
Example: Math.jsp <body> <%!int number1, number2, result;%> <H1> Result for <%=request.getparameter("operator")%></h1> <% number1 = Integer.parseInt(request.getParameter("input1")); number2 = Integer.parseInt(request.getParameter("input2")); result = 0; String str = request.getparameter("operator"); if (str.equals("add")) { result = number1 + number2; } if (str.equals("subtract")) { result = number1 - number2; } if (str.equals("multiply")) { result = number1 * number2; } if (str.equals("divide")) { result = number1 / number2; } %> Result is <%=result%> </body> 15
Directives Affects the overall structure of the servlet class Tells the web container how to translate a JSP page into the corresponding servlet. <%@ directive attribute="value" %> <%@ page... %> Defines attributes that apply to an entire JSP page. <%@ include... %> Includes a file during the translation phase. <%@ taglib... %> Declares a tag library, containing custom actions, used in the page 16
Page Directive <%@ page attribute="value" %> <jsp:directive.page attribute="value" /> Example: <%@ page extends= myclass import= java.util.* contenttype= text/html errorpage= error.jsp %> 17
Page directive Directives Values Description Language scriptinglanguage The default value is "java". Extends classname Import importlist java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.* are imported implicitily Session true false Buffer none sizekb Default 8 kb autoflush true false isthreadsafe true false Info info_text retrieved later by using getservletinfo() method errorpage iserrorpage contenttype pageencoding error_url true false contentinfo pageencodinginfo 18
errorpage & iserrorpage Authenticate.html Authenticate.jsp <%@ page errorpage="loginerrorpage.jsp" %> LoginErrorPage.jsp <%@ page iserrorpage="true" %> 19
Include Directive Includes a static file <%@ include file="relative url" > <jsp:directive.include file="relative url" /> Example: main.jsp: Current date and time is: <%@include file= date.jsp date.jsp: <%@page import = java.util.* %> <% =(new java.util.date()).tolocalestring() %> 20
Implicit Objects Object Description request This is the HttpServletRequest object associated with the request. response This is the HttpServletResponse object associated with the response to the client. out session application config pagecontext This is the PrintWriter object used to send output to the client. This is the HttpSession object associated with the request. This is the ServletContext object associated with application context. This is the ServletConfig object associated with the page. This encapsulates use of server-specific features like higher performance JspWriters. page Exception This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. The Exception object allows the exception data to be accessed by designated JSP. 21
References https://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html https://www.javatpoint.com/jsp-tutorial 22
Thank you 23