1 / 50

Unit-vi & vii Java server pages

Java Server Pages (JSP) is a server-side technology allowing web programmers to create dynamic webpages in response to client requests. It's a high-level abstraction of Java Servlets. JSP runs under a web container, compiles pages on the server, and provides excellent server-side programming for database access in web applications.

nadinek
Download Presentation

Unit-vi & vii Java server pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit-vi & viiJava server pages

  2. Jsp is server side technology that enables web programmers to generate webpages dynamically in response to client requests. • Jsp is nothing but high level abstraction of java servlet technology. • Jsp pages run under the supervision of an environment called web container. • web container compiles the page on the server and generates servlet, which is loaded in the JRE. • Jsp technology provides excellent server side programming for web applications that need a database access.

  3. JSP Overview - Servlets v/s JSPs JSPs : Java within HTML Presentation logic Servlets : HTML within Java business logic public void doGet(request, response) { PrintWriter out = response.getWriter(); String name = request.getParameter(name); out.println(“<html><body>”); out.println("Hello” + name); out.println(“</body></html>”); } <html> <body> <% String name = request.getParameter(name); %> Hello <%= name %> </body> </html>

  4. JSP Overview - What is a JSP In the end, a JSP is just a Servlet. Is loaded and Initialized as Is translated to Compiles to writes JSP Import javax. servlet. HttpServlet.* 0010 0001 1100 1001 0001 0011 Servlet MyJsp_jsp Servlet MyJsp.jsp MyJsp_jsp.java MyJsp_jsp.class

  5. JSP Life Cycle JSP page (MyFirstJSP.jsp) -> Translated to Servle(MyFirstJSP.servlet) -> Compiled to class (MyFirstJSP.class) -> Loaded into memory (Initialization) -> Execution (repeats) -> Destruction Any change in JSP page automatically repeats the whole life cycle.

  6. jsp anatomy /components Jsp page is a simple web page which contains the jsp elements & template text • Template data: every thing in the web page what the jsp container doesn’t understand is called template data. • Template text can be scripting code such as html,wml,xml (or)simple text • Jsp elements:

  7. Jsp elements:

  8. Directives: • Directives: a jsp may contain instructions to be used by the jsp container to indicate how this page is interpreted and executed. Those instructions are called Directives • Delimited by <%@ and %>

  9. page Directive • Specifies JSP’s global settings in JSP container • First line of a JSP file • Example: • <%@ Directive attribute=“value –of _attribute”%> • <%@ page language=“java” contentType=“text/html” %>

  10. page Directive

  11. page Directive (cont.)

  12. include Directive • Includes content of another resource at JSP translation time • Not as flexible as <jsp:include> action • <tr> <td style = "width: 160px"> <%-- include toc.html in this JSP --%>//commments <%@include file ="toc.html"%> </td> <td style = "vertical-align: top"> <%-- include clock2.jsp in this JSP --%> <%@include file ="clock2.jsp" %> </td> </tr>

  13. Tag lib • It is used to specify the custom tags. The custom tags are those tags that can be created by the user. • <% taglib uri=“taglibraryURI” prefix=“tagPrefix” %> • http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

  14. Action elements • The elements which are used to access the bean in page is called JSP action elements. • Standard Actions • Custom actions • Jsp standard tag library

  15. Standard Actions • Predefined JSP tags that encapsulate functionality • Provide access to common tasks performed in a JSP • Including content from other resources • Forwarding requests to other resources • Interacting with JavaBeans • JSP containers process actions at request time • Delimited by <jsp:action> and </jsp:action> • Often performed based on information from client request

  16. Standard Actions

  17. Standard Actions (cont.)

  18. <jsp:include> Action • Enables dynamic content to be included in a JSP • More flexible than include directive • Requires more overhead when page contents change frequently • Example: • <Html> • <head></head> • <Body> • <jsp:include page="banner.html" flush="true" /></body></html>

  19. <jsp:forward> & <jsp:param> Actions • <jsp:forward> action • Enables JSP to forward request to different resource • <jsp:param> action • Specifies name/value pairs of information to pass to other actions • Example:<jsp:forward page=“forward2.jsp"> <jsp:param name = "date" value = "<%= new java.util.Date() %>" /> </jsp:forward>

  20. <jsp:plugin> Action <html> <head> <title>Using jsp:plugin to load an applet</title> </head> <body> <jsp:plugin type = "applet" code = "com.deitel.advjhtp1.jsp.applet.ShapesApplet" codebase = "/advjhtp1/jsp" width = "400" height = "400"> <jsp:params> <jsp:param name = "red" value = "255" /> <jsp:param name = "green" value = "255" /> <jsp:param name = "blue" value = "0" /> </jsp:params> </jsp:plugin> </body> </html> • Adds an applet or JavaBean to a Web page

  21. Custom actions • Custom action element allows us to create user –defined tags. • The programmer can hide the java class code and can acess that code from the jsp page

  22. Jsp standard tag library • JSTL standard for jsp tag library. • J2ee is typically used for server side programming using java and JSTL a component of J2EE web application development platform. • Tag library is useful for performing some common tasks such as condition execution, loop execution, data procession….. • Core • Xml • Sql • Formatting • Functions • http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm.

  23. Core • The core tag is used nearly in all the web applications. • Tag is useful for performing basic input & output, for looping operation or evaluating expression.

  24. Jsp Example • A simple jsp program to display date and time •   <%@ page language="java" contentType="text/html"%> • <%@ page import="java.lang.*" import="java.util.*"%> • <html> • <head> • <title> laxmi first JSP Pro</title> • </head> • <body> • <h1> Welcome to my First JSP Program</h1> • <%out.println("JSP is equal to HTML and JAVA");%> • <%= new Date()%> • </body> • </html>

  25. scriptlets • Scripting elements means writing java code in jsp. • Three type of elements: • Scriptlet • Expressions • Declarations

  26. Implicit Objects • Implicit Objects are predefined variables used to access request application data. These objects are used by the scripting elements. • Objects created by the servlet container • Application • Session • Request • Response • Exception • Out • Config • Pagecontext • Page

  27. Application Object • Used to share data between all of the pages that make up an application • All users share the Application Object • Can be accessed from any JSP in the application via the ServletContext • javax.servlet.http.ServletContext

  28. Application Object Methods • getAttribute(String name) • getAttributeNames • setAttribute(String objName, Object object) • removeAttribute(String objName) • getMajorVersion() • getMinorVersion() • getServerInfo() • getInitParameter(String name) • getInitParameterNames • getResourceAsStream(Path) • log(Message)

  29. Sessions • A session is a concept that represents a series of HTTP requests and responses between a specific browser and a specific server. • Sessions are managed by the server • Created by the server when the first request is received from a browser on a new host the server starts the session and assigns it an ID. • The ID is sent to the browser as a cookie and remembered by the server

  30. Sessions (more) • The server creates a Session object that contains the attributes: • Creation Time • ID • Last Time Accessed • Maximum Inactive Time allowed. • Javax.servlet.http.HttpSession

  31. HTTPSession Interface • getAttribute() – returns the object associated with the specified key string • getAttributeNames() – returns an enumeration of the key strings in the session • getCreationTime() – returns the time the session was created; millsecs past midnight since Jan 1, 1970 GMT • getID() – returns the session ID as a string • getLastAccessedTime() – returns thelast time the client sent a request for this session • setAttribute() – associates an object with a key string in the session • removeAttribute() – remove an attribute from the session

  32. request Object • Javax.servlet. http.httpservletrequest. • The object request is of type Javax.servlet.http.httpservletrequest. • This denotes the data included with the HTTP Request. • The client makes a request that is then passed to the server. • The requested object is used to take the value from client’s web browser and pass it to the server. • This is performed using HTTP request like headers, cookies and arguments. • Methods: • getContentLength() • getServerName()

  33. response Object • Javax.servlet.http.HttpServletResponse • The HTTP Response data. • The result or the information that fulfills an Http request • The object response is of type javax.servlet.http. httpservletresponse. Generally, the object response is used with cookies. The response object is used to build the HTTP response header • Methods: • addCookie() • AddHeader()

  34. pageContext Object • Javax.servlet.jsp.pagecontext • Used to access page attributes and also to access all the namespaces associated with a JSP page. • Methods: • getRequest() • getpage() • getResponse()

  35. Out Object • The Output stream in the context of page. • The class or the interface name of the Out object is jsp.JspWriter. • The Out object is written: Javax.servlet.jsp.JspWriter • Methods: • Clear() • newLine()

  36. config Object • This is used to get information regarding the Servlet configuration • The class or the interface name of the Config object is ServletConfig. • The object Config is written Javax.servlet.http.ServletConfig • Methods: • getinitParameter() • getServletName()

  37. page Object • The Page object denotes the JSP page • Used for calling any instance of a Page's servlet. • The class or the interface name of the Page object is jsp.HttpJspPage. • The Page object is written: Java.lang.Object

  38. Exception • This object is for handling error pages and contain information about runtime error

  39. Conditional processing(scriptlet) • To build the complex logic we require some conditional statements. • We can embed the java conditional statements in the jsp. this task can be done by scriptlets. • Scriptlets allows us to embed the java code in the jsp page. • Syntax: • <% any code %> • <%@ page language="java" contentType="text/html"%> • <%@ page import="java.lang.*" import="java.util.*"%> • <html><head> • <title> laxmi first scriplet</title> • </head><body> • <h1> Welcome to my First scriplet Program</h1> • <%out.println("JSP is very interesting");%> • </body></html>

  40. <%@ page language="java" contentType="text/html"%> • <%@ page import="java.lang.*" import="java.util.*"%> • <html><head> • <title> laxmi second scriplet</title> • </head><body> • <h1> Welcome to my second scriplet Program</h1> • <strong> • <%out.println(“value of I is:");%> • <%for(int i=1;i<5;i++){ • Out.print(“<br/>”); • If(i%2==0) • { • Out.print(“<i/>”); • Out.println(“Even value is : “+i); • } • else • { • Out.print(“<i/>”); • Out.println(“Odd value is : “+i); • } • }%> • </strong> • </body></html>

  41. Output: • Value of I is : • Odd value is :1 • Even value is:2 • Odd value is:3 • Even value is :4

  42. Displaying values using an expression to set an attribute • The Jsp expressions are used to insert java values directly into the output. • Syntax: • <%=expressioncode%> • Expression gets evaluated at run time and then the result will get displayed on the web browser. • <%@ page language="java" contentType="text/html"%> • <html><head> • <title> int to expression</title> • </head><body> • <h1> Welcome to expression</h1> • <strong> • Now the date and time is = <% new java.util.date()%> • </strong> • </body> </html>

  43. <%= request.getServerName()%> (implict object are used) • <%= request.getServerport()%> (implict object are used)

  44. Declaring variables and methods • Jsp allows us to use the user defined variables and methods using declaration. • The variables and methods can be used along with the scriptlets and expression. • Syntax:<%! Any java code%> • <%@ page language="java" contentType="text/html"%> • <%! • String msg=“hello”;%> • <%! Public String MyFunction(String msg) • { return msg; } %> • <html> <title> use of methods</tilte>></head> • <body> <% out.print(“before function call:”+msg);%> • <br/> After function call: <% =MyFuncion(“technical pub”)%></body></html>

  45. ERROR DEUGGING • <%@page language="java" contentType="text/html"> • <%@page import="java.util.*"%> • <html> • <body> • Today's Date is: • <%=new Date().toString()%> • </body> • </html>

  46. Scriptlet demo • <%@page language="java" contentType="text/html"%> • <html> • <head> • <title>Introduction to SCRITPTLET</title> • </head> • <body> • <h1> • <%ou.println(" HIIIIIIIII ");%> • </h1> • </body> • </html>

More Related