3. Question
• Blocking I/O model
recvfrom
application
System call
Process
datagram
Process blocks in
call to recvfrom
Wait for data
Copy data from
kernel to user
No datagram
ready
Datagram ready
Copy datagram
Copy complete
Return OK
kernel
4. Question
• Non-Blocking I/O model
recvfrom
application
System call
Process
datagram
Process repeatedly
calls recvfrom
waiting for an OK
return(polling)
Wait for data
Copy data from
kernel to user
No datagram
ready
Datagram ready
Copy datagram
Copy complete
Return OK
kernel
EWOULDBLOCK
recvfrom
System call No datagram
readyEWOULDBLOCK
5. Question
• Asynchronous I/O model
recvfrom
application
System call
Signal handler
process
datagram
Process continues
executing
Wait for data
Copy data from
kernel to user
No datagram
ready
Datagram ready
Copy datagram
Copy complete
Deliver signal
specofied in recvfrom
kernel
return
6. Question
• Synchronous I/O
• Definition
– A synchronous I/O operation causes the
requesting process to be blocked until that I/O
operation completes.
7. Question
• How to solve multiple I/O devices?
• Use select()
A
B
A
Select()
device
12. Introduction
• You can use JSP or servlet to solve dynamic
web page needs. JSP is web page design
oriented, and servlet is program designed
oriented. Knowing their character can
cooperate two expertise and make the
achievement better.
13. CGI
• Common Gateway Interface
• A standard method for web server software to
delegate the generation of web content to
executable files.
• Web server send request to external program,
and the program response with static file.
14. CGI
Web Server
CGI Program Database
Web Browser
1. Request
2. Input data
3. May access database
4. Response as HTML doc
Figure: CGI workflow
15. CGI
• CGI can designed as any computer language.
Perl is the most popular.
• When server receive a request, it produce a
new process to execute CGI program.
• Consume many memory.
• Each CGI program is isolated.
16. Servlet
• In order to solve the disadvantage of CGI,
servlet is developed.
• Servlet is designed to substitute CGI.
• The word servlet is composed of “server” and
“let”. In Java, “let” means small application.
• Servlet is run in server side, and applet is run
in client side.
17. Servlet
Client Browser Web Server
Servlet Container
Servlet
1. Browser
requests page
2. Web server
delegates to container
3. Container executes servlet,
creating instance if required
4. Servlet executes,
generates response
5. Server
returns response
Figure: Servlet workflow
18. Servlet
• Web container
– Execute JSP and servlet.
– Like Java only recognizes JVM, servlet/JSP only
recognizes web container.
– Parsing HTTP request, create instance like
HttpServletRequest、 HttpServletResponse、
HttpSession
19. Servlet
• Servlet life cycle
– void init()
• When servlet is first loaded, initialize servlet status
– void service()
• Serve a request. Can be called multiple times.
– void destroy()
• Dispose servlet.
20. Servlet
• Since the output of servlet is HTML, the usage
of servlets doesn’t constrain by browser.
• No matter how many requests, only one
servlet will be loaded into JVM.
• When there is a request, there is a thread, not
process.
• Servlets is persistent until be deystroyed.
24. Implement of Servlet
• javax.servlet
– The javax.servlet package contains a number of
classes and interfaces that describe and define the
contracts between a servlet class and the runtime
environment provided for an instance of such a
class by a conforming servlet container
25. Implement of Servlet
• javax.servlet.http
– The javax.servlet.http package contains a number
of classes and interfaces that describe and define
the contracts between a servlet class running
under the HTTP protocol and the runtime
environment provided for an instance of such a
class by a conforming servlet container.
28. Implement of Servlet
• service() can handle doGet() and doPost()
• Get
– The request parameter shows on URL
– Not proper for password
– Not too long data
– Good for setting as tags or bookmarks
– Don’t change the status of server
29. Implement of Servlet
• Post
– The request parameter is in Body
– Good for password
– Good for long data
– Not proper for tags or bookmarks
– May change content of database
30. Implement of Servlet
• HttpServletRequest
– The object of client sending request to server
– E.g. Form input data
• HttpServletResponse
– The object of server responding to client
– E.g. Show HTML doc on browser
31. Implement of Servlet
• HttpServletResponseObjectName.setContentType(“ContentType”)
• E.g. res.serContentTyoe(“text/html;charset=Big5”)
32. Implement of Servlet
• In order to show information on web page,
use HttpServletResponse object create a
PrintWriter object.
• And use PrintWriter to write HTML tags.
39. JSP
• When a JSP file is accessed, the container
converts it into a Java class that implements
the javax.servlet.jsp.HttpJspPage interface.
The various JSP building blocks are translated
into Java code and compiled into a Servlet.
40. JSP
• The element of JSP can be divided into three
kinds
– Directive
• Definite the static information of web page.
– Action
• Send data according to the request of client.
– Scripting
• Small piece of java code. Only execute if client request.
42. JSP
• Implicit object
• Corresponds to an object in servlet
• out
– corresponds to JspWriter, which associates
PrintWriter
• Request
– corresponds to HttpServletRequest
• Response
– corresponds to HttpServletResponse