Home > Sample essays > Learning DHTML & JavaScript: 3.9, 3.10 & 3.11

Essay: Learning DHTML & JavaScript: 3.9, 3.10 & 3.11

Essay details and download:

  • Subject area(s): Sample essays
  • Reading time: 4 minutes
  • Price: Free download
  • Published: 1 April 2019*
  • Last Modified: 23 July 2024
  • File format: Text
  • Words: 1,259 (approx)
  • Number of pages: 6 (approx)

Text preview of this essay:

This page of the essay has 1,259 words.



3.9 DHTML with JavaScript.

DHTML use of JavaScript

In JavaScript, the statement: document.write(), is used to write output to a web page.

Example

The following example uses JavaScript to display the current date and time on a page:

<html>

<head>

<title>(Type a title for your page here)</title>

<script type="text/javascript">

function display_c(){

var refresh=1000; // Refresh rate in milli seconds

mytime=setTimeout('display_ct()',refresh)

}

function display_ct() {

var strcount

var x = new Date()

document.getElementById('ct').innerHTML = x;

tt=display_c();

}

</script>

</head>

<body onload=display_ct();>

<span id='ct' ></span>

<>/body>

</head>

3.10 Servlets

Servlet Basics

Java servlet is a Java programming language program that extends the capabilities of a server

Advantages of Servlet

Efficient

Convenient

Powerful

Portable

Inexpensive

Secure

Servlet can handle

GET Request (Default)

POST Request

Basic Servlet Structure

Both doGet() and doPost() take two arguments:

HttpServletRequest

HttpServletResponse

The HttpServletRequest lets you get at all of the incoming data; the class has methods by which you can find out about information such as form (query) data, HTTP request headers, and theclient’s hostname.

The HttpServletResponse lets you specify outgoing information such as HTTP status codes(200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.).

Most importantly, HttpServletResponse lets you obtain a PrintWriter that you use to send document content back to the client. For simple servlets, most of the effort is spent in println statements that generate the desired page.

Since doGet and doPost throw two exceptions (ServletException and IOException), you are required to include them in the method declaration

import classes in – io (for PrintWriter, etc.),

servlet (for HttpServlet, etc.), and servlet.http (for HttpServletRequest and HttpServletResponse).

3.10.1 Java Servlet Architecture

The web server needs a JSP engine ie. container to process JSP pages. The JSP container is responsible for intercepting requests for JSP pages. This tutorial makes use of Apache which has built-in JSP container to support JSP pages development.

A JSP container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to understand the special elements that are part of JSPs.

Web server using JSP

3.10.2 Servlet Life Cycle

Servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet

The servlet is initialized by calling the init () method.

The servlet calls service() method to process a client's request.

The servlet is terminated by calling the destroy() method.

Finally, servlet is garbage collected by the garbage collector of the JVM.

The init() method :

The init() method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init() method of applets.

public void init() throws ServletException {

// Initialization code…

}

example:

public void service(ServletRequest request,

ServletResponse response)

throws ServletException, IOException{

}

doGet() Method

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

// Servlet code

}

doPost() Method

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

// Servlet code

}

Architecture Digram:

The following figure depicts a typical servlet life-cycle scenario.

First the HTTP requests coming to the server are delegated to the servlet container.

The servlet container loads the servlet before invoking the service() method.

Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.

Servlet Life Cycle

3.11 Form GET and POST actions

Two commonly used methods for a request-response between a client and server are: GET and POST.

GET – Requests data from a specified resource

POST – Submits data to be processed to a specified resource

The GET Method

Syntax:/test/demo_form.asp?name1=value1&name2=value2

Some other notes on GET requests:

GET requests can be cached

GET requests remain in the browser history

GET requests can be bookmarked

GET requests should never be used when dealing with sensitive data

GET requests have length restrictions

GET requests should be used only to retrieve data

The POST Method

Syntax:POST /test/demo_form.asp HTTP/1.1

name1=value1&name2=value2

Some other notes on POST requests:

POST requests are never cached

POST requests do not remain in the browser history

POST requests cannot be bookmarked

POST requests have no restrictions on data length

Compare GET vs. POST

Other HTTP Request Methods

3.12 Session Handling

The HttpSession Object:

Apart from the above mentioned three ways, servlet provides HttpSession Interface which provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.

The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user.

You would get HttpSession object by calling the public method getSession() of HttpServletRequest, as below:

HttpSession session = request.getSession();

You need to call request.getSession() before you send any document content to the client. Here is a summary of the important methods available through HttpSession object:

S.N.

Method & Description

1

public Object getAttribute(String name)

This method returns the object bound with the specified name in this session, or null if no object is bound under the name.

2

public Enumeration getAttributeNames()

This method returns an Enumeration of String objects containing the names of all the objects bound to this session.

3

public long getCreationTime()

This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.

4

public String getId()

This method returns a string containing the unique identifier assigned to this session.

5

public long getLastAccessedTime()

This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

6

public int getMaxInactiveInterval()

This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

7

public void invalidate()

This method invalidates this session and unbinds any objects bound to it.

8

public boolean isNew()

This method returns true if the client does not yet know about the session or if the client chooses not to join the session.

9

public void removeAttribute(String name)

This method removes the object bound with the specified name from this session.

10

public void setAttribute(String name, Object value)

This method binds an object to this session, using the name specified.

11

public void setMaxInactiveInterval(int interval)

This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

3.13 Understanding Cookies

Web Browsers and Servers use HTTP protocol to communicate and HTTP is a stateless protocol.

But for a commercial website, it is required to maintain session information among different pages. For example, one user registration ends after completing many pages. But how to maintain users' session information across all the web pages.

In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.

Cookies are a plain text data record of 5 variable-length fields −

Expires − The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.

Domain − The domain name of your site.

Path − The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page.

Secure − If this field contains the word "secure", then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.

Name=Value − Cookies are set and retrieved in the form of key-value pairs

Storing Cookies

Syntax:document.cookie = "key1=value1;key2=value2;expires=date";

Example: It sets a customer name in an input cookie

<html>

<head>

<script type="text/javascript">

<!–

function WriteCookie()

{

if( document.myform.customer.value == "" ){

alert("Enter some value!");

return;

}

cookievalue= escape(document.myform.customer.value) + ";";

document.cookie="name=" + cookievalue;

document.write ("Setting Cookies : " + "name=" + cookievalue );

}

//–>

</script>

</head>

<body>

<form name="myform" action="">

Enter name: <input type="text" name="customer"/>

<input type="button" value="Set Cookie" onclick="WriteCookie();"/>

</form>

</body>

</html>

Output

About this essay:

If you use part of this page in your own work, you need to provide a citation, as follows:

Essay Sauce, Learning DHTML & JavaScript: 3.9, 3.10 & 3.11. Available from:<https://www.essaysauce.com/sample-essays/2015-11-2-1446464978/> [Accessed 14-04-26].

These Sample essays have been submitted to us by students in order to help you with your studies.

* This essay may have been previously published on EssaySauce.com and/or Essay.uk.com at an earlier date than indicated.