Java Server Pages: Softsmith Infotech
Java Server Pages: Softsmith Infotech
Softsmith Infotech
Softsmith Infotech
Jsp Execution
Softsmith Infotech
Softsmith Infotech
Example JSP
<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 havent yet talked about how to do import declarations
Softsmith Infotech
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
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
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
Directives
Directives affect the servlet class itself A directive has the form: <%@ directive attribute="value" %> or <%@ directive attribute1="value1" attribute2="value2" ... attributeN="valueN" %> The most useful directive is page, which lets you import packages Example: <%@ page import="java.util.*" %>
Softsmith Infotech
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
10
Softsmith Infotech
11
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
12
JSP in XML
JSP can be embedded in XML as well as in HTML Due to XMLs 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
13
Softsmith Infotech
14
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
15
Javascript
Softsmith Infotech
16
JavaScript language
The JavaScript language is an interpreted, object oriented based scripting language. JavaScript coding statements are commonly embedded inside a webpage using the HTML tags <SCRIPT> </SCRIPT>. When the web browser is rendering the webpage, any JavaScript coding statements encountered are immediately interpreted and run before the rendering continues.
Softsmith Infotech
17
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
18
Softsmith Infotech
19
20
Events
An event is 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
21
Binding Events
Softsmith Infotech
22
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
23
click
Used to indicate an entity (usually a control) has been clicked Used to indicate the submit button has been pushed Used to indicate the mouse cursor has been moved over the entity. Typically the entity is a control or an anchor. Used to indicate the mouse cursor has been moved away from the entity. Typically the entity is a control or an anchor. Used to indicate the webpage has been completely parsed and loaded into the web browser. 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 Softsmith 24 browser is closed or exited, the unload eventInfotech is not generated.
submit
mouseover
mouseout
load
unload