1 / 24

JSP

JSP. Java Server Pages. Java Server Pages. JSP ( Java Server Pages ) is an alternate way of creating servlets JSP is written as ordinary HTML , with a little Java mixed in The Java is enclosed in special tags, such as <% ... %> The HTML is known as the template text

star
Download Presentation

JSP

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. JSP Java Server Pages Softsmith Infotech

  2. Java Server Pages • JSP (Java Server Pages) is an alternate way of creating servlets • JSP is written as ordinary HTML, with a little Java mixed in • The Java is enclosed in special tags, such as <% ... %> • The HTML is known as the template text • JSP files must have the extension .jsp • JSP is translated into a Java servlet, which is then compiled • Servlets are run in the usual way • The browser or other client sees only the resultant HTML, as usual • Tomcat knows how to handle servlets and JSP pages Softsmith Infotech

  3. Jsp Execution Softsmith Infotech

  4. JSP scripting elements • There is more than one type of JSP “tag,” depending on what you want done with the Java • <%= expression %> • The expression is evaluated and the result is inserted into the HTML page • <% code %> • The code is inserted into the servlet's service method • This construction is called a scriptlet • <%! declarations %> • The declarations are inserted into the servlet class, not into a method Softsmith Infotech

  5. <HTML><BODY>Hello!  The time is now <%= new java.util.Date() %></BODY></HTML> Notes: The<%= ... %> tag is used, because we are computing a value and inserting it into the HTML The fully qualified name (java.util.Date) is used, instead of the short name (Date), because we haven’t yet talked about how to do import declarations Example JSP Softsmith Infotech

  6. Variables • You can declare your own variables, as usual • JSP provides several predefined variables • request : The HttpServletRequest parameter • response : The HttpServletResponse parameter • session : The HttpSession associated with the request, or null if there is none • out : A JspWriter (like a PrintWriter) used to send output to the client • Example: • Your hostname: <%= request.getRemoteHost() %> Softsmith Infotech

  7. Scriptlets • Scriptlets are enclosed in <% ... %> tags • Scriptlets do not produce a value that is inserted directly into the HTML (as is done with <%= ... %>) • Scriptlets are Java code that may write into the HTML • Example:<% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData);%> • Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled • Example:<%if (Math.random() < 0.5) { %> Have a <B>nice</B> day!<% } else { %>Have a <B>lousy</B> day!<%}%> Softsmith Infotech

  8. Declarations • Use <%! ... %> for declarations to be added to your servlet class, not to any particular method • Caution: Servlets are multithreaded, so nonlocal variables must be handled with extreme care • If declared with<% ... %>, variables are local and OK • Data can also safely be put in the request or session objects • Example:<%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> • You can use<%! ... %> to declare methods as easily as to declare variables Softsmith Infotech

  9. Directives • Directives affect the servlet class itself • A directive has the form:<%@ directiveattribute="value" %>or<%@ directiveattribute1="value1" attribute2="value2" ...attributeN="valueN" %> • The most useful directive is page, which lets you import packages • Example:<%@ page import="java.util.*" %> Softsmith Infotech

  10. JSP Comments • Different from HTML comments. • HTML comments are visible to client. <!-- an HTML comment --> • JSP comments are used for documenting JSP code . • JSP comments are not visible client-side. <%-- a JSP comment --%> Softsmith Infotech

  11. The include directive • The include directive inserts another file into the file being parsed • The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives • Syntax: <%@ include file="URL" %> • The URL is treated as relative to the JSP page • If the URL begins with a slash, it is treated as relative to the home directory of the Web server • The include directive is especially useful for inserting things like navigation bars Softsmith Infotech

  12. Actions • Actions are XML-syntax tags used to control the servlet engine • <jsp:include page="URL" /> • Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) • This is great for rapidly changing data • <jsp:forward page="URL" /><jsp:forward page="<%= JavaExpression %>" /> • Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL Softsmith Infotech

  13. JSP in XML • JSP can be embedded in XML as well as in HTML • Due to XML’s syntax rules, the tags must be different (but they do the same things) • HTML: <%=expression %>XML: <jsp:expression>expression</jsp:expression> • HTML: <%code%>XML: <jsp:scriptlet>code</jsp:scriptlet> • HTML: <%!declarations%>XML: <jsp:declaration>declarations</jsp:declaration> • HTML: <%@ include file=URL%>XML: <jsp:directive.include file="URL"/> Softsmith Infotech

  14. Action – useBean tag • The useBean action tag is the most commonly used tag because of its powerful features. • It allows a JSP to create an instance or receive an instance of a Java Bean. • It is used for creating or instantiating a bean with a specific name and scope. • Examples • <jsp:useBean id=“time" scope="session" class="com.time.CurrentTimeBean" /> Softsmith Infotech

  15. Session in jsp • 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 (i) Session Object (ii) Cookies (iii) Hidden Form Fields (iv) URL Rewriting Softsmith Infotech

  16. Javascript Softsmith Infotech

  17. JavaScript language • The JavaScript language is an interpreted, object oriented based scripting language. • JavaScript coding statements are commonly embedded inside a webpage using theHTML tags <SCRIPT> </SCRIPT>. • When the web browser is rendering thewebpage, any JavaScript coding statements encountered are immediately interpretedand run before the rendering continues. Softsmith Infotech

  18. Where to use JavaScript • Verify the end user input data contains legitimate data values. • Handle any arithmetic or logic processing that needs to be executed on the web browser. • Directly manipulate the web browser objects such as the status bar, address bar, and the web browser's rendered controls (such as textboxes, selection lists, etc...). • Handle and process client-side events that have been generated. Softsmith Infotech

  19. How to run JavaScripts • JavaScript statements are interpreted and run by the web browser • Most common web browsers contain a JavaScript interpreter program • interpret and run the JavaScript commands that have been embedded inside a webpage. Question1: In a web application, which entity executes JSPs? Question2: In a web application, which entity executes JavaScript commands? Softsmith Infotech

  20. JavaScript programming example <HTML> 2 <HEAD><TITLE> JavaScript Example1 </TITLE></HEAD> 3 <BODY> 4 <H1>Javascript Example1</H1> 5 An alert box will appear using the window object <br> 6 7 <SCRIPT> 8 window.alert("hello world"); 9 </SCRIPT> 10 11 Goodbye! 12 13 </BODY> 14 </HTML> Softsmith Infotech

  21. Events An eventis an identifiable occurrence or action that has taken place in the system. Writing programs that generate and handle events is known as event programming. Events are typically generated in the following ways. • GUI programs can generate an event to indicate there has been some interaction between the widget and the end user. • when an end user presses a button, a click event is generated. • Input/output programs can generate an event to indicate an input/output event has occurred. • A mouse event is generated when the end user moves the mouse. Softsmith Infotech

  22. Binding Events Softsmith Infotech

  23. Binding Events In client side web programming, the web browser contains it's own event listener. Therefore you must do two things in order to process an event that has been generated. 1. Write the code that binds the event to the event handler. 2.  Write the code for the event handler. Softsmith Infotech

  24. Event Description click Used to indicate an entity (usually a control) has been clicked submit Used to indicate the submit button has been pushed mouseover Used to indicate the mouse cursor has been moved over the entity. Typically the entity is a control or an anchor. mouseout Used to indicate the mouse cursor has been moved away from the entity. Typically the entity is a control or an anchor. load Used to indicate the webpage has been completely parsed and loaded into the web browser. unload Used to indicate the webpage has been unloaded from the web browser. Unloading a webpage occurs when the web browser is about to load another webpage. The unload event is the last event that will be generated from the current webpage before the next webpage is loaded. When the web browser is closed or exited, the unload event is not generated. Common web browser events Softsmith Infotech

More Related