Jsp 01


Published on

introduction about JSP, introduction about java server pages

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Jsp 01

  1. 1. Trainer = Subhasis Nayak CMC
  2. 2. What is JSP ? <ul><li>It is a textual document that describes how to create a response object from a request object for given protocol. </li></ul><ul><li>The processing of JSP page may involve: </li></ul><ul><ul><li>Creating objects </li></ul></ul><ul><ul><li>Using objects. </li></ul></ul><ul><li>The default request & response objects are </li></ul><ul><ul><li>HttpServletRequest </li></ul></ul><ul><ul><li>HttpServletResponse </li></ul></ul>
  3. 3. What it provides? <ul><li>Capability to create both static & dynamic components. </li></ul><ul><li>Dynamic capability that Servlet can provide. </li></ul><ul><ul><li>Integration of content. </li></ul></ul><ul><ul><li>Manipulating data. </li></ul></ul>
  4. 4. Information about JSP <ul><li>Java Server pages has these features: </li></ul><ul><ul><li>Language for developing java server pages. </li></ul></ul><ul><ul><li>Constructs for accessing server-side objects . </li></ul></ul><ul><ul><li>Defining extensions to the JSP language . </li></ul></ul><ul><li>It is a text based document that contains two types of text. </li></ul><ul><ul><li>Static template data </li></ul></ul><ul><ul><li>JSP elements which construct dynamic data . </li></ul></ul>
  5. 5. JSP life cycle <ul><li>Some how similar to Servlet life cycle. </li></ul><ul><li>When any request mapped to a JSP, it is handled by a special Servlet event. </li></ul><ul><ul><li>Servlet checks the java Server Page’s Servlet is older than the java Server Page . </li></ul></ul><ul><ul><li>If so it translates to java Server Page into a Servlet class & compiles the class. </li></ul></ul><ul><li>Next the Servlet sends response to the java server page. </li></ul>
  6. 6. Advantages of JSP <ul><li>Build process is performed automatically . </li></ul><ul><li>Translation phase treat each data type in java Server Pages differently. </li></ul><ul><li>Template data is transformed into code . </li></ul><ul><ul><li>This code emits that data stream that returns data to the client. </li></ul></ul><ul><li>It is faster than Servlet. </li></ul>
  7. 7. JSP Architecture Model <ul><li>According to Sun there 2 architectural model for building application using JSP & Servlet technology. Those are: </li></ul><ul><ul><li>JSP model 1 </li></ul></ul><ul><ul><li>JSP model 2 </li></ul></ul>
  8. 8. JSP model 1 <ul><li>In this model the every request to a JSP page. The requested page is completed responsible for doing all the tasks required for fulfilling the request. </li></ul>1.request 4.response 2.Create Java beans 3.Retrieve data Not good for Complex EIS/DB Browser JSP Java Beans Suite able for small application. In big application we put huge logic code to JSP.
  9. 9. JSP model 2 <ul><li>This architecture follows MVC design pattern. </li></ul><ul><ul><li>Servlet act as Controller </li></ul></ul><ul><ul><li>JSP act as View </li></ul></ul><ul><ul><li>Java Bean act as Model </li></ul></ul><ul><li>All requests to Servlet. They analyze the request and collect data required to generate response into java beans objects. Then Servlet dispatch the request to JSP. </li></ul><ul><li>JSP use data stored in java beans to generate a presentable response. </li></ul>
  10. 10. Cont’d …… 4.Invoke JSP 6.response 2.Create Java beans 3.Retrieve data 2.Use Java beans 1.request EIS/DB Browser Servlet (Controller) Java Beans (Model) JSP (view) <ul><li>Higher security </li></ul><ul><li>Easy maintainability </li></ul>
  11. 11. JSP life Cycle. <ul><li>For 1 st time when we accessed the JSP page server is slower after that it will faster . </li></ul><ul><li>When we access JSP it is converted to it’s Servlet class before it can be used to service client side request . </li></ul><ul><li>For each request the JSP engine checks the timestamps of source JSP page and corresponding Servlet if JSP is newer then it will be again converted to it’s equivalent Servlet. This process consists of 7-phases. </li></ul>
  12. 12. Cont’d …..
  13. 13. Elements of JSP JSP tag description Syntax directive Specifies translation time instruction to JSP engine <%@Directives%> Declaration It declare & define variables , methods. <%! Some java code %> Scriptlets Tags are used to embed java code in JSp page. Allows to write free-form java code in JSP page <%count++%> Expression Used to print value in the out put html file <%=an expression%> Action Provides request time instructions to the JSp engine. <jsp:actionname/> comment Used for documentation <%--Any text--%>
  14. 14. A simple JSP page <html> <body> <% out.println(&quot;<h1>Hello World!</h1>&quot;);%> </body> </html> Scriptlet
  15. 15. The processing of JSP <ul><li>When the browser asks the Web server for a JSP, the Web server passes control to a JSP container . </li></ul><ul><li>A container works with the Web server to provide the runtime environment and other services a JSP needs . </li></ul><ul><li>It knows how to understand the special elements that are part of JSPs . Because this is the first time this JSP has been invoked , the JSP container converts it into an executable unit called a Servlet . </li></ul>
  16. 16. Cont’d …… <ul><li>The entire page, including the parts that are in HTML, is translated into source code . After code translation, the JSP container compiles the Servlet , loads it automatically, and then executes it. </li></ul><ul><li>Typically, the JSP container checks to see whether a Servlet for a JSP file already exists . </li></ul><ul><ul><li>If not it do the translation process </li></ul></ul><ul><ul><li>If version not match do the translation process </li></ul></ul>
  17. 17. Let’s see the translated code of a JSP <html> <body> <h1>Hello World!</h1> </body> </html> out.write(&quot;<html> &quot;); out.write(&quot;<body> &quot;); out.println(&quot;<h1>Hello World!</h1>&quot;); out.write(&quot; &quot;); out.write(&quot;</body> &quot;); out.write(&quot;</html> &quot;); JSP Servlet Don’t worry about Servlet file of JSP. JSP, why?
  18. 18. Let’s display Something Output swill same The second one is the short hand code
  19. 19. Program displaying date
  20. 20. variables <ul><li>You can declare your own variables, as usual </li></ul><ul><li>JSP provides several predefined variables </li></ul><ul><ul><li>request : The HttpServletRequest parameter </li></ul></ul><ul><ul><li>response : The HttpServletResponse parameter </li></ul></ul><ul><ul><li>session : The HttpSession associated with the request, or null if there is none </li></ul></ul><ul><ul><li>out : A JspWriter (like a PrintWriter ) used to send output to the client </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li>Your hostname: <%= request.getRemoteHost() %> </li></ul></ul>
  21. 21. Scriptlets <ul><li>Scriptlets are enclosed in <% ... %> tags </li></ul><ul><ul><li>Scriptlets do not produce a value that is inserted directly into the HTML (as is done with <%= ... %> ) </li></ul></ul><ul><ul><li>Scriptlets are Java code that may write into the HTML </li></ul></ul><ul><ul><li>Example: <% String queryData = request.getQueryString(); out.println(&quot;Attached GET data: &quot; + queryData); %> </li></ul></ul><ul><li>Scriptlets are inserted into the Servlet exactly as written, and are not compile d until the entire Servlet is compiled </li></ul><ul><ul><li>Example: <% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>lousy</B> day! <% } %> </li></ul></ul>
  22. 22. Declarations <ul><li>Use <%! ... %> for declarations to be added to your Servlet class, not to any particular method </li></ul><ul><ul><li>Caution: Servlet are multithreaded, so nonlocal variables must be handled with extreme care </li></ul></ul><ul><ul><li>If declared with <% ... %> , variables are local and OK </li></ul></ul><ul><ul><li>Data can also safely be put in the request or session objects </li></ul></ul><ul><li>Example: <%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> </li></ul><ul><li>You can use <%! ... %> to declare methods as easily as to declare variables </li></ul>
  23. 23. Directives <ul><li>Directives affect the Servlet class itself </li></ul><ul><li>A directive has the form: <%@ directive attribute =&quot; value &quot; %> or <%@ directive attribute1 =&quot; value1 &quot; attribute2 =&quot; value2 &quot; ... attributeN =&quot; valueN &quot; %> </li></ul><ul><li>The most useful directive is page , which lets you import packages </li></ul><ul><ul><li>Example: <%@ page import=&quot;java.util.*&quot; %> </li></ul></ul>
  24. 24. JSP Comments <ul><li>Different from HTML comments. </li></ul><ul><li>HTML comments are visible to client. </li></ul><ul><li><!-- an HTML comment --> </li></ul><ul><li>JSP comments are used for documenting JSP code . </li></ul><ul><li>JSP comments are not visible client-side. </li></ul><ul><li><%-- a JSP comment --%> </li></ul>
  25. 25. The include directive <ul><li>The include directive inserts another file into the file being parsed </li></ul><ul><ul><li>The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives </li></ul></ul><ul><li>Syntax: <%@ include file=&quot; URL &quot; %> </li></ul><ul><ul><li>The URL is treated as relative to the JSP page </li></ul></ul><ul><li>The include directive is especially useful for inserting things like navigation bars </li></ul>
  26. 26. Actions <ul><li>Actions are XML-syntax tags used to control the Servlet engine </li></ul><ul><li><jsp:include page=&quot; URL &quot; /> </li></ul><ul><ul><li>Inserts the indicated relative URL at execution time ( not at compile time , like the include directive does) </li></ul></ul><ul><ul><li>This is great for rapidly changing data </li></ul></ul><ul><li><jsp:forward page=&quot; URL &quot; /> <jsp:forward page=&quot;<%= JavaExpression %>&quot; /> </li></ul><ul><ul><li>Jump to the (static) URL or the ( dynamically computed ) JavaExpression resulting in a URL </li></ul></ul>
  27. 27. JSP in XML <ul><li>JSP can be embedded in XML as well as in HTML </li></ul><ul><li>Due to XML’s syntax rules, the tags must be different (but they do the same things) </li></ul><ul><li>HTML: <%= expression %> XML: <jsp:expression> expression </jsp:expression> </li></ul><ul><li>HTML: <% code %> XML: <jsp:scriptlet> code </jsp:scriptlet> </li></ul><ul><li>HTML: <%! declarations %> XML: <jsp:declaration> declarations </jsp:declaration> </li></ul><ul><li>HTML: <%@ include file= URL %> XML: <jsp:directive.include file=&quot; URL &quot;/> </li></ul>
  28. 28. Action – useBean tag <ul><li>The useBean action tag is the most commonly used tag because of its powerful features. </li></ul><ul><li>It allows a JSP to create an instance or receive an instance of a Java Bean. </li></ul><ul><li>It is used for creating or instantiating a bean with a specific name and scope . </li></ul><ul><li>Examples </li></ul><ul><ul><li><jsp:useBean id=“time&quot; scope=&quot;session&quot; class=&quot;com.time.CurrentTimeBean&quot; /> </li></ul></ul>
  29. 29. Session in jsp <ul><li>In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie . Session management done by: </li></ul><ul><ul><ul><li>Session Object </li></ul></ul></ul><ul><ul><ul><li>Cookies </li></ul></ul></ul><ul><ul><ul><li>Hidden Form Fields </li></ul></ul></ul><ul><ul><ul><li>URL Rewriting </li></ul></ul></ul>
  30. 30. Program for math calculation Declarative tag Expression tag
  31. 31. Program to display the string
  32. 32. XML style
  33. 33. Compare XML tag with normal Normal JSP tag XML tag of the JSP tag
  34. 34. Program to write scriptlet tag Scriptlet tag
  35. 35. Lab questions? <ul><li>Declare an integer type variable and display it . </li></ul><ul><li>Write the XMl style tag to declare and display the variable. </li></ul><ul><li>Write a program to compare a string is equal to “welcome” .if equal display something else display something. </li></ul><ul><li>Write a program to compare two strings values: </li></ul><ul><ul><li>1 st string = “my fname” </li></ul></ul><ul><ul><li>2 nd sting = “my lname” </li></ul></ul><ul><li>If both strings are true then display “both are true” else display “one of them not true or no one is true” </li></ul><ul><ul><li> </li></ul></ul>
  36. 36. Hands on lab Next session