• No results found

8.Active Server Pages and Java V2

N/A
N/A
Protected

Academic year: 2020

Share "8.Active Server Pages and Java V2"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

WEB TECHNOLOGIES

WEB TECHNOLOGIES

WEB TECHNOLOGIES

WEB TECHNOLOGIES

UNIT

UNIT

UNIT

UNIT –

– IV ASP and XML

IV ASP and XML

IV ASP and XML

IV ASP and XML

Chapters:

Chapters:

Chapters:

Chapters:

8. Active Server Pages and Java

9. XML: Defining Data for Web Applications

(2)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

8. Active Server Pages and Java

1. What is ASP and ASP File?

ASP:

ASP stands for Active Server Pages

ASP is a Microsoft Technology

ASP is a program that runs inside IIS

IIS stands for Internet Information Services

IIS comes as a free component with Windows 2000

IIS is also a part of the Windows NT 4.0 Option Pack

The Option Pack can be downloaded from Microsoft

PWS (Personal Web Server) is a smaller - but fully functional - version of IIS

PWS can be found on Windows 95/98 CD

ASP File:

An ASP file is just the same as an HTML file

An ASP file can contain text, HTML, XML, and scripts

Scripts in an ASP file are executed on the server

An ASP file has the file extension ".asp"

2. How Does ASP Differ from HTML?

When a browser requests an HTML file, the server returns the file

When a browser requests an ASP file, IIS passes the request to the ASP engine. The

ASP engine reads the ASP file, line by line, and executes the scripts in the file.

(3)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

Ex:1 The response.write command is used to write output to a browser. The following

example sends the text "Hello World" to the browser:

<!DOCTYPE html> <html>

<body> <%

response.write("Hello World!") %>

</body> </html>

OR

<!DOCTYPE html> <html>

<body> <%

="Hello World!" %>

</body> </html>

Save the above file as asp1.asp in “C:\inetpub\wwwroot\MyWeb” directory.

Open the file in the browser. Type the file address as

“http://localhost/MyWeb/asp1.asp” and press Enter Key.

Output:

Ex:2

<!DOCTYPE html> <html>

<body> <%

response.write("<h2>You can use HTML tags to format the text!</h2>")

%>

<%

response.write("<p style='color:#0000ff'>This text is styled with the style

attribute!</p>")

%>

(4)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

Output:

Ex:3 To create a variable:

<!DOCTYPE html> <html>

<body> <%

dim name

name="Donald Duck"

response.write("My name is: " &name) %>

</body> </html>

Output:

3. Write about ASP Objects?

Objects are things which have defined boundaries. Objects provide services of other

parts of an application through their methods. A method just like a function, but again usually

objects only let the world use some of their methods. Objects are a real boom to the

developers of complex systems. These developers can now write really complex code which

can be easily added to other applications (or) easily used by other developers.

ASP technology defines five objects when we can use in our scripts. They are

1. The Request Object:

Data sent by a user is packaged as a request object which Perlscript calls $ request.

The request object may contain a number of different data items but the most important ones

(5)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

2. The Response Object:

The Response Object controls the transmission of data from the server to the web

browser. Any type of data can be sent back, normally by sending HTML. We can also

configure the HTTP header by handling things such as cookies to it directly from our ASP

objects.

The response object has a Response->Buffer property which we can set to onto

cache, the page before sending. Caching is important when we are generating lots of data and

want the user to see and the whole page rather than just part of it.

HTML pages can have an expiry time. This can be set from the ASP script and is

measured in minutes from the time that the browser receives the page. The expiry time is set

through the $Response->Expires property. Finally, browsers can be automatically redirected

to another side by using $Response->Redirect ($url).

3. The Server Object:

The server object holds information about the web server itself lets we use some of its

functionality. We can set the maximum time that a script will execute before generating an

error. This is especially useful if we are accessing the databases, which may not be present on

the system (or) which may be very busy. It avoids the situation where a single script jumps

the server for an inordinate (or) even indefinite, length of time.

4. The Session Object:

ASP provides a third option called a session. This uses cookies but they are set by the

server itself, not by the programmer and are valid only for a limited time. Using $session the

following sequences happens:

a. The user requests a page. Any cookies which are valid for the server are

returned along with the page.

b. If no cookies are sent a new session is created and a cookie is set on the

browser.

c. If cookies were sent they are checked to see if one is an ASP session ID. If a

session ID is returned, then it is checked to see if it is checked to see if it has

timed out yet.

d. If the cookies has timed out a new session is created and a new cookie set.

e. If the cookie is valid and not timed out the existing session is restored from

(6)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

4. The Application Object:

Finally we have the application object. This is used to share data within a web

application.

Ex:

<html>

<body>

<form action="#" method="post">

<p>select the Radio Button</p>

<input type="radio" name="gender" value="male">male</input>

<br>

<input type="radio" name="gender" value="female">female</input>

<br>

<p>select the Check Boxes </p>

<input type="checkbox" name="q1" value="SSC">SSC</input>

<br>

<input type="checkbox" name="q2" value="BSc">BSc</input>

<br>

<input type="submit" value="submit">

</form>

<%

Dim gender

gender=request.form("gender")

If gender<>" " Then

response.write(gender)

End If

Dim q1,q2

q1=request.form("q1")

If q1<>" " Then

response.write(q1 & " ")

End If

q2=request.form("q2")

(7)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

response.write(q2)

End If

%>

</body>

</html>

Output:

4. Write in Brief History of Java.

Java is a general-purpose, object-oriented programming language developed by Sun Microsystems of USA in 1991.Originally called Oak by James Gosling (one of the inventor

of the language). Java was invented for the development of software for consumer electronic

devices like TVs, toasters, etc. The main aim had to make java simple, portable and reliable.

Java Authors: James, Arthur Van, and others.

(8)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

JAVA Features:

As we know that the Java is an object oriented programming language developed by

Sun Microsystems of USA in 1991. Java is first programming language which is not attached

with any particular hardware or operating system. Program developed in Java can be

executed anywhere and on any system.

The features of Java are as follows:

1. Compiled and Interpreted

2. Platform Independent and portable

3. Object- oriented

4. Robust and secure

5. Distributed

6. Familiar, simple and small

7. Multithreaded and Interactive

8. High performance

9. Dynamic and Extensible

1. Compiled and Interpreted

Basically a computer language is either compiled or interpreted. Java comes together

both these approach thus making Java a two-stage system.

Java compiler translates Java code to Bytecode instructions and Java Interpreter

generate machine code that can be directly executed by machine that is running the Java

program.

2. Platform Independent and portable

Java supports the feature portability. Java programs can be easily moved from one

computer system to another and anywhere. Changes and upgrades in operating systems,

processors and system resources will not force any alteration in Java programs. This is reason

why Java has become a trendy language for programming on Internet which interconnects

different kind of systems worldwide. Java certifies portability in two ways.

First way is, Java compiler generates the bytecode and that can be executed on any

machine. Second way is, size of primitive data types are machine independent.

3. Object- oriented

Java is truly object-oriented language. In Java, almost everything is an Object. All

(9)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., classes; organize in packages that can be used in program by Inheritance. The object model in

Java is trouble-free and easy to enlarge.

4. Robust and secure

Java is a most strong language which provides many securities to make certain

reliable code. It is design as garbage –collected language, which helps the programmers

virtually from all memory management problems. Java also includes the concept of exception

handling, which detain serious errors and reduces all kind of threat of crashing the system.

Security is an important feature of Java and this is the strong reason that programmer

use this language for programming on Internet.

The absence of pointers in Java ensures that programs cannot get right of entry to

memory location without proper approval.

5. Distributed

Java is called as Distributed language for construct applications on networks which

can contribute both data and programs. Java applications can open and access remote objects

on Internet easily. That means multiple programmers at multiple remote locations to work

together on single task.

6. Simple and small

Java is very small and simple language. Java does not use pointer and header files,

goto statements, etc. It eliminates operator overloading and multiple inheritance.

7. Multithreaded and Interactive

Multithreaded means managing multiple tasks simultaneously. Java maintains

multithreaded programs. That means we need not wait for the application to complete one

task before starting next task. This feature is helpful for graphic applications.

8. High performance

Java performance is very extraordinary for an interpreted language, majorly due to the

use of intermediate bytecode. Java architecture is also designed to reduce overheads during

runtime. The incorporation of multithreading improves the execution speed of program.

9. Dynamic and Extensible

Java is also dynamic language. Java is capable of dynamically linking in new class,

libraries, methods and objects. Java can also establish the type of class through the query

(10)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., Java program is support functions written in other language such as C and C++, known as

native methods.

Java on the Web:

Java covers the whole application from server to client and back again. It provides

many powerful technologies are used to extend the browser. It provides a good, if far from

perfect, security system.

If Java is so good why do developers still use languages such as Perl and JavaScript?

Java performance is not great. CGI scripts written in Perl, run by Apache using

mod_perl, will do better than Java Servlets on many machines.

JavaScript uses elements of an HTML page and create fun DHTML effects.

HTML + JavaScript download and initializes far faster than HTML + Java.

To learn Java requires lot of time. But to learn JavaScript require less time.

Java support may be limited or non-existent on devices such as mobile phones or

WebTV sets.

Programming in Java:

Java environment includes a number of development tools, classes and methods. The

development tools are part of the system known as Java Development Kit (JDK) and the

classes and methods are part of the Java Standard Library (JSL), also known as the

Application Programming Interface (API).

Java Development kit (JDK) – The JDK comes with a set of tools that are used for

developing and running Java program. It includes:

1. Appletviewer( It is used for viewing the applet)

2. Javac(It is a Java Compiler)

3. Java(It is a java interpreter)

4. Javap(Java diassembler,which convert byte code into program description)

5. Javah(It is for java C header files)

6. Javadoc(It is for creating HTML document)

(11)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., For compiling and running the program we have to use following commands:

a) javac (Java compiler)

In java, we can use any text editor for writing program and then save that program

with ”.java” extension. Java compiler converts the source code or program in bytecode and

interpreter convert “.java” file in “.class” file.

Syntax:

C:\javac filename.java

If my filename is “abc.java” then the syntax will be

C:\javac abc.java

b) java (Java Interpreter)

As we learn that, we can use any text editor for writing program and then save that

program with “.java” extension. Java compiler convert the source code or program in

bytecode and interpreter convert “.java” file in “.class” file.

Syntax:

C:\java filename

If my filename is abc.java then the syntax will be

C:\java abc

Simple Java Program:

class FirstProgram {

public static void main(String args[]) {

System.out.println(“This is my first program”);

}

}

The file must be named “FirstProgram.java” to equivalent the class name containing the main method. Java is case sensitive.

This program defines a class called “FirstProgram”.

A class is an object oriented term. It is designed to perform a specific task. A Java class is defined by its class name, an open curly brace, a list of methods

and fields, and a close curly brace.

The name of the class is made of alphabetical characters and digits without

(12)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

The line “public static void main (String [] args )” shows where the program

will start running. The word main means that this is the main method – The

JVM starts running any program by executing this method first.

The main method in FirstProgram.java consists of a single statement System.out.println("This is my first program");

The statement outputs the character between quotes to the console.

Above explanation is about how to write program and now we have to learn where to

write program and how to compile and run the program.

For this reason, the next explanation is showing the steps:

1. Edit the program by the use of Notepad.

2. Save the program to the hard disk.

3. Compile the program with the javac command. (Java compiler)

4. If there are syntax errors, go back to Notepad and edit the program.

5. Run the program with the java command.(Java Interpreter)

6. If it does not run correctly, go back to Notepad and edit the program.

Applets:

There are two kinds of Java programs, applications (also called stand-alone

programs) and Applets. An Applet is a small Internet-based program that has the Graphical

User Interface (GUI), written in the Java programming language.

Ex: The following example is made simple enough to illustrate the essential use of Java

applets through its java.applet package.

import java.awt.*; import java.applet.*;

public class SimpleApplet extends Applet { public void paint(Graphics g) {

g.drawString("My First Applet",40,40); }

}

Save the file as SimpleApplet.java

Compile the file using javac SimpleApplet.java

How to run an Applet?

There are two ways in which one can run an applet, as follows

Executing the applet within a java-compatible web browser.

(13)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., An applet viewer executes our applet in a window. This is generally the

fastest and easiest way to test our applet.

To execute an applet in a web browser, you need to write a short HTML

text file that contains the appropriate APPLET tag.

For above example it is <html>

<body>

<applet code="SimpleApplet.class" width=200 height=100> </applet>

</body> </html>

Save this code in text file with extension .html say Myapplet.html.

On successful compilation of SimpleApplet.java file, execute this file using appletviewer

Myapplet.html or just open this html file directly in the Browser.

The output of above example appears as shown in the following figure:

Graphical User Interface (GUI):

A type of user interface item that allows people to interact with programs in more

ways than typing such as computers and many hand-held devices such as mobile phones is

called a graphical user interface (GUI). A GUI offers graphical icons, and visual indicators,

as opposed to text-based interfaces. This helps to develop more efficient programs that are

easy to work with. The user can interact with the application without any problem.

The GUI application is created in three steps. These are:

Add components to Container objects to make our GUI.

Then you need to setup event handlers for the user interaction with GUI. Explicitly display the GUI for application.

Ex: Text Area:

This is the text container component of Java AWT package. The Text Area contains

(14)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., TextArea txtArea_name = new TextArea ();

import java.awt.*;

import java.applet.Applet;

/*<applet code="TAreaTest" width=200 height=100>

</applet>

*/

public class TAreaTest extends Applet {

TextArea disp;

public void init() {

disp = new TextArea("Code goes here", 10, 30);

add (disp);

}

}

Save the file as TAreaTest. Java

Compile the file using javac TAreaTest.java

On successful compilation, execute the file using appletviewer TAreaTest.java

The output appears as shown in following figure:

Note:

Any company want to develop the website that can be developed in two ways, they are

static website and dynamic website.

A static website is one where there is no interaction from the end user. To develop static website we can use the markup languages like HTML, DHMTL, XML,

JavaScript etc.

A dynamic website is one in which there exist end user interaction. To develop dynamic websites, in industry we have lot of technologies such as CGI (Common

(15)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist., In the recent years SUN micro systems has developed a technology called Servlets to

develop the dynamic websites and also for developing distributed applications.

A distributed application is one which always runs in the context of browser or www.

The result of distributed application is always sharable across the globe. To develop

distributed application one must follow the following:

Client-Server architecture:

2-tier architecture (Client program, Database program).

3-tier or MVC (Model [Database] View [JSP] Controller [Servlets]) architecture

(Client program, Server program, Database program).

n-tier architecture (Client program, Firewall program, Server program, Database program).

To exchange the data between client and server we use a protocol caller http which is a

part of TCP/IP.

A client is the program which always makes a request to get the service from server. A server is the program which always receives the request, process the request and

gives response to ‘n’ number of clients concurrently.

A server is the third party software developed by third party vendors according to

SUN micro systems specification. All servers in the industry are developed in java language

only. The basic purpose of using server is that to get concurrent access to a server side

program.

According to industry scenario, we have two types of servers; they are web server and

(16)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

5. Explain Java Servlets.

“A Servlet is quite simply a java class that holds on to the general model of a Servlet

as defined by the Servlet API.”

A Servlet Container, also known as a Servlet Engine translates requests from

whatever protocol is being used into objects that the Servlet understands, and also gives the

Servlet an object which it can use to send a response. This container is also responsible for

managing the lifecycle of a Servlet.

Install the Apache Tomcat Server before executing servlet files. Servlets are ordinary

Java code files which are compiled into .class files using any standard Java Compiler. The

API files are supported with the Apache Tomcat server. Hence no extra downloads are

required before start of work. Once we have created a Servlet and is referred by browser

using the full path to the class file but without file extension:

http://localhost:8080/servlets/Hello Where Hello is a Servlet file.

The following example inherits the HttpServlet class and providing the

implementation of the doGet() method. The get request is the default request.

Open a new file in Notepad and type the following code: import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;

public class DemoServlet extends HttpServlet{

public void do Get(HttpServletRequest req, HttpServletResponse res) throws

ServletException, IOException{

res.setContentType(“text/html”); //setting the content type

PrintWriter pw = res.getWriter(); //get stream to write the data

//writing HTML in the stream

pw.println(“<html><body>”);

pw.println(“Welcome to Servlet”);

pw.println(“</body></html>”);

pw.colse(); //closing the stream

}

}

(17)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

Note:

But before compile the above program, set the CLASSPATH as the following:

i. Suppose the Tomcat Server installed in “C –drive”.

ii. The DemoServlet.java is in the “E:\B.Sc-2013\Servlets” directory.

iii. Now open the command prompt and change directory to “E:\B.Sc-2013\Servlets”

because I saved DemoServlet.java file in it.

iv. Now set the CLASSPATH as the following where the Java Source file (I stored

my file in E:\B.Sc-2013\Servlets) is stored and after that press the enter key.

E:\B.Sc-2013\Servlets>set CLASSPATH=%CLASSPATH%;C:\Program Files\Apache

Software Foundation\Tomcat 6.0\lib\servlet-api.jar

Here is given some necessary elements to run the simple Servlets:

<web-app>

<servlet>

<servlet-name>bvrice</servlet-name>

<servlet-class>DemoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>bvrice</servlet-name>

<url-pattern>/welcome</url-pattern>

</servlet-mapping>

</web-app>

Start the Apache Tomcat server.

Open the Browser and type the address as http://localhost:8090/br1/welcome and

press enter key.

Note:

1. In my system, Tomcat port no. is 8090 and I saved my file “DemoServlet.class” in

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\br1\WEB-INF\classes

directory.

2. Welcome is the <url-pattern> is sub element of <servlet-mapping>. This pattern is used at

client side to invoke the Servlet. The web.xml file is saved in the C:\Program Files\Apache

Software Foundation\Tomcat 6.0\webapps\br1\WEB-INF directory.

3. If we want to do one more application, create a folder such as br2 where we created br1

(18)

Written by Dept. of Computer Science @ Dr. BVRICE Bhimavaram W.G. Dist.,

Cookies:

Cookies are text files stored on the client computer and they are kept for various

information tracking purpose. Java Servlets transparently supports HTTP cookies. There are

three steps involved in identifying returning users:

Server script sends a set of cookies to the browser. For example name, age, or

identification number etc.

Browser stores this information on local machine for future use.

When next time browser sends any request to web server then it sends those cookies

information to the server and server uses that information to identify the user.

References

Related documents

If a consumer cannot solve the dispute arising from an online sales or service contract directly with the trader, the dispute still can be solved through the platform with

لوﺪﺟ 3 - رد ﺖﻋﺎﺳ ود ﺮﻫ و ﯽﺷﻮﻬﯿﺑ زا ﻞﺒﻗ ﮏﯿﻟﻮﺘﺳﺎﯾد نﻮﺧرﺎﺸﻓ ﻦﯿﮕﻧﺎﯿﻣ 6 رد ﯽﺣاﺮﺟ زا ﺲﭘ لوا ﺖﻋﺎﺳ هﻮﯿﺟ ﺮﺘﻣ ﯽﻠﯿﻣ يﺎﻨﺒﻣ ﺮﺑ ﻪﻌﻟﺎﻄﻣ ﺖﺤﺗ يﺎﻫ هوﺮﮔ زا ﮏﯾﺮﻫ (رﺎﯿﻌﻣ فاﺮﺤﻧا)

Server System P4000RP Family Service Guide Main Advanced Security Server Management Boot Options Boot Manager?. System Boot Timeout [0 – 65535, 0

Exterior: AMG bodystyling comprising front and rear aprons, side sill panels, AMG spoiler lip on the boot lid, 45.7 cm (18-inch) AMG 7-twin-spoke light-alloy wheels painted

(B) the adsorption at a single site on the surface may involve multiple molecules at the same time.. (C) the mass of gas striking a given area of surface is proportional to the

Salon International du Prêt-à-porter (Ready to wear International Fair), Paris, Porte de Versailles – January 24th to 27th. Retromobile , Paris, Porte de Versailles – February 8th

uc Transport Demand Define Actual Transport Preferences Define Transport Item Find Transport Alternativ es Gather Information DefineTransport Execution Plan Define Exception

1 This essay explores Roth’s published writings on Salonica—his newspaper and journal articles, his books, scholarly essays, and encyclopaedia entries—to examine how Roth made use