Java Servletit vs. Microsoftin Active Server Page (ASP)

Download Report

Transcript Java Servletit vs. Microsoftin Active Server Page (ASP)

Java Servletit vs.
Microsoftin Active Server
Page (ASP)
Olli Pöyry
Essaim Oy
Staattinen .html
<HTML>
<TITLE>Esimerkki</TITLE>
<H1>Tämä on staattinen html
tiedosto</H1>
<BODY>
Tämä on <B>tiedoston</B> tekstiä
</BODY></HTML>
Three-tier architecture
Active Server Pages .asp
 Active Server Pages is an open, compile-free application
environment in which you can combine HTML, scripts,
and reusable ActiveX server components to create
dynamic and powerful Web-based business solutions.
Active Server Pages enables server side scripting for IIS
with native support for both VBScript and Jscript.
 Files created with Active Server Pages have the
extension .ASP.
 ASP uses the delimiters (better known to you and me as
"thing-a-ma-bobs that specify the beginning and end")
"<%" and "%>" to enclose script commands.
Esimerkki
Java Servlets for Middle
Tiers
 Clearly the most common use of Java Servlets will be to function as
part of middle tiers in enterprise networks, connecting to SQL
databases via JDBC. Just as perhaps 80% of the world's
applications are database applications, many corporate developers
will use Java Servlets for an endless variety of purposes over
intranets, extranets and the Internet. Whether Human Resources
needs to deploy an application about employee benefits, or an
enterprise-wide solution is needed for customer service, account
management and inventory control, Java Servlets and the Web
service or NC (Network Computer) service offer a flexible and
extensible solution.
Scriptiä sisältävä .html
(esim. .asp tai .jsp)
<%@ Language=VBScript %>
<HTML>
<BODY>
Block 1 - Straight HTML - Before the
Code Block<P>
<SCRIPT LANGUAGE=javascript
RUNAT=Server>
Response.Write ("Block 2 - SCRIPT
LANGUAGE=javascript
RUNAT=Server<P> ");
</SCRIPT>
<%
Response.Write ("Block 3 -
Scripts.asp
Java Servlet
source
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet {
public void doGet (
HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
out.println("<HEAD><TITLE> SimpleServlet Output
</TITLE></HEAD><BODY>");
out.println("<h1>
SimpleServlet Output </h1>");
binary
out.println("<P>This is output from SimpleServlet.");
Directory of D:\JRun\2.2\jsm-default\services\jse\servlets
out.println("</BODY>");
out.close();
24.10.97 09:54
1 066 SimpleServlet.class
}
}
Vaihtoehdot .html
generointiin
Script
Käännetty
.asp
Active Server Page
.jsp
Java Server Page
-
.dll
Visual Basic DLL
.class
Java Servlet
Oracle WebServer
PL/SQL
ASP Built-In Objects
 Six objects come built-in with IIS 4.0. These objects
enable your pages to communicate effectively with the
server, the application, the current session, and the
user:






Request object -- Gets information from the user. You can get FORM data, read
cookies, and so forth.
Response object -- Sends information to the user. You can send text to the page,
redirect to another URL, and set cookies.
Server object -- Interacts with the server. You can access a database, read files,
and find out about the capabilities of the browser.
Session object -- Enables you to manage information about the current session
(each user has one session per open browser).
Application object -- Will store information for all sessions.
ObjectContext object -- Used for committing and aborting transactions (new in
IIS 4.0)
.asp sivulta Oracleen
VBScript / JavaScript
OLE
ActiveX Data Objects DAO
OLEDB
ODBC
ODBC Oracle Driver
Oracle Objects for OLE
Esimerkki
Source
.asp sivulle voi lukea
koodia toisesta tiedostosta
<!--#include
virtual="/si/VarmistaYhteys.inc"-->
<!--#include virtual="/si/dbUtil.inc"-->
Yleiskäyttöisiä aliohjelmia ja globaaleja
muuttujia voi sijoittaa .inc -tiedostoihin
Koodi voidaan kääntää
Visual Basicilla
<HTML><HEAD><TITLE>Example 1</TITLE> </HEAD><BODY>
<%
Set ObjReference = Server.CreateObject("Example1.HelloWorld")
ObjReference.SayHello
%>
</BODY></HTML>
 Visual Basic DLLs will generally run more efficiently than scripts
written in any scripting language. Encapsulate the Visual Basic code
in DLLs and use Server.CreateObject().
 VBScript

Visual Basic, esim.
Dim sSql As String
SimpleServlet.java
import java.io.*;
import javax.servlet.*;
public class SimpleServlet extends GenericServlet
{
public void service( ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
ServletOutputStream out = res.getOutputStream();
out.println(”<HEAD><TITLE> SimpleServlet Output ");
out.println("</TITLE></HEAD><BODY>");
out.println("<h1> SimpleServlet Output </h1>");
out.println("</BODY>");
}
}
Servletistä Oracleen
Java (sisältää JDBC)
Oracle JDBC Driver (Oracle RDBMS
rompulta)
a Java applet and a Java
servlet?
Basically, a servlet is the opposite end of
an applet. A servlet can almost be thought
of as a server-side applet. Servlets run
inside the Web server in the way that
applets run inside the Web browser. The
browser can submit a request to execute
a servlet directly; it can be stand-alone in
terms of its actions -- as a browser can
request an applet directly.
Java intro
import java.sql.*;
class Employee
{
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
while (rset.next ())
System.out.println (rset.getString (1));
Hello.java
}
Emp.java
}
Do servlets require a
specialized server?
To run a Java servlet, the first thing you
need is a Java Virtual Machine (JVM)
running on the server. It's analogous to
running Java applets in a browser. The
browser that supports applets must be
running a JVM. So, for a server to run a
servlet, it has to be running a JVM.
Java Servlet API
 Additionally, the server also has to support the Java
Servlet API. This API, developed by JavaSoft, defines
how and when the servlet communicates with the
server. Essentially, the Servlet API is a well-defined set
of function calls to get and send information to and from
the server. The servlet needs to be able to access
server-defined variables, issue redirects, send error
messages and the like.
 The Servlet API is a set of Java classes that can be
downloaded directly from Sun, independently from the
Java Web Server. This is the Java Servlet Development
Kit.
Servlet API Docs
Servlet tuotteita
Sun
Java Web Server
$ 295
IBM
WebSphere
$ 795
Live Software, Inc.
JRun Servlet Engine $ 0
WebLogic Tengah
WAICoolRunner
…
http://jserv.javasoft.com/products/javaserver/servlets/environments.html
Servlet engine vain osa
kokonaisuutta
WebSphere lisäpalvelut
“Oracle Extended Internet
Technology”
 The Extended Internet Technology platform consists of Oracle Lite
3.5; a miniature client-side application server called Oracle Lite Web
Server; and a small-footprint replication service called Oracle Mobile
Agents 3.0, for synchronizing with an Oracle8i RDBMS.
 The Oracle Lite Web Server can serve HTML documents, as well
as deploy Java applets or servlets via the standard Java virtual
machine on the client side, said Jacob Christfort, director of product
management and marketing for Oracle mobile and embedded
products. The miniature application server allows users to run fairly
powerful "distributed" applications for uses such as sales force
automation or spreadsheets.
 The Extended Internet Technology will enter beta in November, with
shipping due in January.
http://www.infoworld.com/cgi-bin/displayStory.pl?980924.wcextended.htm
IBM WebSphere Studio
 IBM WebSphere Studio V1.0 provides a complete set of
tools covering Web project organization, Java Servlet
code generation Wizards, HTML authoring, script
development, and Java code development for
applications deployed on WebSphere Application Server.
By enabling easy creation of servlets and JavaBeans,
WebSphere Studio gives you all of the advantages of
Java servlet server-side applications - easy data sharing,
powerful Java APIs, session tracking, and resource
access.
Verkosta haettavissa
Java Development jdk116-win32.exe
Kit 1.1.6
jdk116-doc.zip
8 906 504
3 333 565
Java Servlet
Development Kit
2.0
972 636
jsdk20-win32.exe
Live Software, Inc. jrun22a.zip
JRun Servlet Engine jrun22docs.zip
7 259 038
1 238 121
Servletin kutsu
http://127.0.0.1:8000/servlet/SnoopServlet
http://localhost/testi.shtml
jossa esim.
<SERVLET name=me.BudMenu>
<PARAM NAME=kategoria VALUE=mcp>
</SERVLET>
HelloServlet.java
Servlet + Oracle =
EmpServ.java
EmpServ2.java
Osa sovelluksen luokista muita
kuin HttpServlet aliluokkia
Osa HttpServlet aliluokan
aliluokkia
Override inherited methods
Java Developer Connection:in
servlet -sovellus
http://http://developer.java.sun.com
Sama
kuva
Together
Servleteille
standardiominaisuudet -1
public abstract class MeServlet extends javax.servlet.http.HttpServlet
{
public static String sConnString = null;
public static String sOraUser = null;
public static String sOraPasswd = null;
...
Servleteille
standardiominaisuudet -2
public abstract class MeStdServlet extends MeServlet
{
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
try {
MeEnviron meEnviron = new MeEnviron();
if( ! meEnviron.Init( req, res, getServletContext(), this )) {
System.out.println( "Systeemivirhe MeStdServlet meEnviron.Init" );
VirheInit( req, res );
return;
}
service( req, res, meEnviron );
} catch ( Exception e ) {
System.out.println( "Virhe " + e );
}
}
abstract protected void service (HttpServletRequest req, HttpServletResponse res,
MeEnviron meEnviron) throws ServletException, IOException;
Servleteille
standardiominaisuudet -3
public class Bud10 extends MeStdServlet
{
public void service (HttpServletRequest req, HttpServletResponse res, MeEnviron meEnviron)
throws ServletException, IOException
{
Java - call by value!
try {
CallableStatement call =
meEnviron.oraYht.conn.prepareCall ( sql );
if( bDebug )
System.out.println( sql );
Toinen tapa - getServlet()
 The other powerful capability of servlets comes from their ability to
"communicate" with each other. That is, one servlet can request the
services of another servlet. For example, if you created a servlet
that handled database connectivity, you could have that servlet
loaded on Web-server startup; then any subsequent servlet could
use the facilities of the database servlet without implementing
database code directly. The following example shows a portion of
code that exemplifies this.
DatabaseServlet dbServlet =
(DatabaseServlet) context.getServlet
("DatabaseServlet");
ResultSet rs = dbServlet.executeQuery("SELECT * From
Table");
Virhekäsittely
public static void copy(String from, String to)
throws IOException
{
FileInputStream fis = null;
FileOutputStream fos = null;
On Error Goto 99
Resume Next
try {
fis = new FileInputStream(from);
fos = new FileOutputStream(to);
}
final int BUFSIZ = 1024;
byte buf[] = new byte[BUFSIZ];
int len = 0;
ASP =
while ((len = fis.read(buf)) > 0)
fos.write(buf, 0, len);
}
finally {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
Servlet =
Servlet Lifecycle
The lifecycle of a servlet begins when it is loaded into a Web server and
ends when it is terminated or is reloaded. When a servlet is loaded, the
server creates an instance of the servlet and calls the servlet init() method.
A servlet may be loaded:
 automatically at server startup
 at first client request
 when a servlet is reloaded
When a client request arrives at the server, the server creates a Request
object and a Response object that are specific to that client request. The
server then invokes the servlet's service() method, passing both the
Request object and Response object as parameters.
The servlet gets information about the request from the Request object,
processes the request, and uses methods of the Response object to return
a response to the client. The service() method may invoke other methods
during the processing of the request, e.g., doGet(), doPost(), or additional
methods you write.
When the server no longer needs the servlet, the server invokes the
servlet's destroy() method.
http://www.software.ibm.com/webservers/appserv/instructions.htm#servelife
Yhteenveto
Hyvää
Huonoa
Active Server  Sivuja helppo
 Spagettiainekset
Pages
synnyttää, ei
valmiina
tarvitse kääntää
 Microsoft
 FrontPage ym.
 Tietokantalinkki
käyttömahdollisuus kömpelömpi
Servlet
 Ammattilaiselle
tehokas
 Osa laajempaa
EJB, CORBA ym.
arkkitehtuuria ja
markkinaa
 Kieli vaikeampi
 Ohjelmat
käännettävä