Retrieving data from URLs using fopen - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Retrieving data from URLs using fopen

Description:

Servlets provide a more modern, Java-centric approach. ... But you can download remote servlets too. Consistent server-side VM. Much easier to test ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 21
Provided by: Dim149
Category:
Tags: data | download | fopen | java | retrieving | urls | using | vm

less

Transcript and Presenter's Notes

Title: Retrieving data from URLs using fopen


1
Retrieving data from URLs using fopen()
  • lt?php
  • url "http//www.cs.ucsd.edu/elkan/134A/"
  • if (fp fopen(url, "r"))
  • while (buf fread(fp, 8192)) print buf
  • fclose(fp)
  • else
  • print("Cannot retrieve url\n")
  • ?gt

2
Extracting content from Web pages
  • lt?php
  • url "http//www.cs.ucsd.edu/elkan/134A/"
  • if (fp fopen(url, "r"))
  • while (!feof(fp)) buf . fread(fp, 8192)
  • fclose(fp)
  • if (preg_match('/ltTITLEgt(lt)lt\/TITLEgt/i',
    buf, matches))
  • print "Title matches1 ltBRgt\n"
  • if (preg_match('/ltmeta name"Author"
    content"(.)"gt/i', buf, matches))
  • print "Author matches1 ltBRgt\n"
  • if (preg_match('/Most recently updated on (.)
    by/i', buf, matches))
  • print "Date matches1 ltBRgt\n"
  • else
  • print("Cannot retrieve url\n")
  • ?gt

3
Server
  • Developing a server
  • How client requests are processed
  • CGI (C, Perl, Python, )
  • ASP
  • PHP
  • Servlets/JSP

4
Server-side architecture
5
ASP Architecture
6
ASP Architecture
7
What Is A Servlet
  • A Java object
  • Plug-in for a web server
  • Replacement for CGI scripts
  • Can also be used to extend server as a plug-in
  • Full power of Java
  • Platform-independent
  • Database access
  • Fun to write

8
What Is A Servlet
  • The key idea is to replace the CGI script with a
    Java object
  • A servlet is simply an object that inherits from
    javax.servlet.GenericServlet
  • Web servlets actually extend a subclass
    (javax.servlet.http.HttpServlet)
  • It has a very different lifecycle from an
    application (no equivalent of main)
  • This is the only spot in the web architecture
    that you modify

9
Server/Service/Servlet
  • server - a process running on a host machine
  • Apache, Java Web Server
  • service - a protocol running on a port
  • HTTP, FTP
  • servlet - a module running inside a service
  • PhoneServlet

10
Servlet/Service/Server Diagram
11
Server-Side Java Application Architectures
Java UI
Java Business Logic
Messaging Protocols
Data Sources Storage
JSP
JavaBeans
RMI
Filesystem5 X
CORBA
JMS
RDBMS
Servlet
EJB
JNI
Data Feed
Servlet
12
Servlet ArchitecturesThree-tier system
  • Tier 1 Client
  • HTML browser
  • Java client
  • Tier 2 Servlets
  • embody business logic
  • secure, robust
  • Tier 3 Data Sources
  • Java can talk to SQL, CORBA, OODB, File system,
    etc. etc.

13
Servlet Architectures N-tier system
  • Tier 1 HTML Browser
  • Tier 2 Servlet
  • User interface
  • Tier 3 EJB/CORBA/RMI Objects
  • Business logic
  • Tier 4 Other Servers (e.g. RDBMS)
  • Data storage

14
CGI and Servlets
  • In conventional CGI, a Web site developer writes
    the executable programs that process form inputs
    in a language such as Perl or C.
  • The program (or script) is executed once each
    time a form is submitted.
  • Servlets provide a more modern, Java-centric
    approach.
  • The server incorporates a Java Virtual Machine,
    which is running continuously.
  • Invocation of a CGI script is replaced invocation
    of a method on a servlet object.

15
Advantages of Servlets
  • Invocation of a single Java method is typically
    much cheaper than starting a whole new program.
    So servlets are typically more efficient than CGI
    scripts.
  • This is important if we planning to centralize
    processing in the server (rather than, say,
    delegate processing to an applet or browser
    script).
  • Faster and Leaner
  • No fork-process like Perl
  • No need to initialize for each request
  • Only lightweight thread context switching
  • Built-in multithreading

16
Servlets vs. CGI
  • Besides this we have the usual advantages of
    Java
  • Portability,
  • A fully object-oriented environment for
    large-scale program development.
  • Library infrastructure for decoding form data,
    handling cookies, etc (although many of these
    things are also available in Perl).
  • Servlets are the foundation for Java Server Pages.

17
Servlets vs. CGI (Cont.)
  • Easy to manage state
  • share data across successive requests
  • share data between concurrent requests
  • use hidden fields, cookies, or sessions
  • Write once, run anywhere
  • It's easy to write unportable Perl
  • Servlets have standard API
  • Supports all methods
  • GET, POST, PUT, DELETE, et al.

18
Servlets vs. Applets
  • Servlets have no GUI
  • Server-side, not client-side
  • Different security model
  • Installed, not downloaded
  • But you can download remote servlets too
  • Consistent server-side VM
  • Much easier to test

19
Web Server Architecture Java Servlet vs. Java
Applet
Web Browser
Web Browser
Browser Extension
Server HTTP
Server HTTP
API
Servlet
JDBC
Socket TCP
JDBC
DBMS
DBMS
Database
Database
20
Typical Modes of Operation of a Servlet Container
Tomcat
Browser
1. Stand-alone
8080
Servlet Request
Client
Server
Apache
2. In-process servlet container
Browser
Tomcat
80
Servlet Request
Client
Server
Apache
3. Out-of- process servlet container
80
Browser
Servlet Request
Tomcat
Client
8007
Server
Write a Comment
User Comments (0)
About PowerShow.com