middleware 3/29/2001 kang, seungwoo lee, jinwon. description of topics 1. cgi, servlets, jsps 2....
TRANSCRIPT
![Page 1: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/1.jpg)
Middleware
3/29/2001
Kang, Seungwoo
Lee, Jinwon
![Page 2: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/2.jpg)
Description of Topics
1. CGI, Servlets, JSPs
2. Sessions/Cookies
3. Database Connection(JDBC, Connection pooling)
![Page 3: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/3.jpg)
The Static Web
Browser HTTP Server
HTMLFiles
Operating System
Operating System
HTTP Request
HTML
![Page 4: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/4.jpg)
The Dynamic Web
Browser HTTP Server
.javafiles
Operating System
Operating System
HTTP Request
HTML ServletEngine
DataFiles
JVM
![Page 5: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/5.jpg)
The Dynamic Web
To evolve from the static to the dynamic web requires: Generating HTML with scripts/programs
CGI, Servlet Tools & techniques to make it manageable
JSP Accessing and Updating data
JDBC
![Page 6: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/6.jpg)
CGI
![Page 7: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/7.jpg)
CGI
CGI is not a language. It's a simple protocol that can be used to communicate between Web server and CGI program.
A CGI script can be written in any language (Virtually any Programming language: C, Perl, shell
script)
![Page 8: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/8.jpg)
#include <stdio.h>void main() {/** Print the CGI response header, required for all HTML output. **/
printf(“Content-type:text/html\n\n”);
/** Print the HTML response page to STDOUT. **/
printf(“<html><head><title>Hello,CGI</title></head>\n”);
printf(“<body><h1>Hello CGI</h1></body></html>”);
}
CGI
![Page 9: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/9.jpg)
CGI
![Page 10: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/10.jpg)
CGI Pros and Cons
Pros Simplicity
Cons Performance problem Security problem
![Page 11: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/11.jpg)
Servlet
![Page 12: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/12.jpg)
Servlet Basics
What is Servlet? A program written in Java that runs on the
server, as opposed to the browser (applets). Invoked with “servlet” keyword in URL
E.g. http://domain-name/servlet/MyServlet
![Page 13: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/13.jpg)
Servlets vs. CGI Servlets have several advantages over CGI
A Servlet does not run in a separate process. A Servlet stays in memory between requests. A
CGI program needs to be loaded and started for each CGI request.
There is only a single instance which answers all requests concurrently. This saves memory
A Servlet can be run in a Sandbox, which allows secure use of untrusted and potentially harmful Servlets.
![Page 14: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/14.jpg)
![Page 15: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/15.jpg)
Servlet Container (engine) Take and convert clients’ requests to an “object”
understood by a servlet Provide an “object” to be used for response Manage the servlet lifecycle
Make a servlet instance Call the instance’s init() Call the instance’s service() upon request Call the instance’s destroy() before destroying the
instance Mark the instance for garbage collection
![Page 16: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/16.jpg)
Add-on Servlet Engines Plug into popular web servers
Stand-alone Servlet Engines Supports Web Server function
JSDK(Java Servlet Development Kit)2.1 Apache JServ 1.1/Tomcat 3.2.1
![Page 17: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/17.jpg)
Servlet Flow
http://nclab.kaist.ac.kr/servlet/MyServlet
HTTP request
response
(HTML page)
Servlet
engine
MyServlet
![Page 18: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/18.jpg)
Servlet Flow A client Web browser submits HTTP
request specifying servlet, e.g., http://nclab.kaist.ac.kr/servlet/MyServlet
The HTTP server receives the request from the client. The server parses the request and forwards it to the servlet.
![Page 19: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/19.jpg)
Servlet Flow An instance of the servlet is created
(or activated) to process the request. The servlet processes any form input data &
generates an HTML response. This response is relayed back through the
HTTP server to the client browser, which displays the page.
![Page 20: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/20.jpg)
Temporary Servlets loaded on demand offer a good way to conserve resources in
the server for less-used functions.
![Page 21: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/21.jpg)
Permanent Servlets Loaded when the server is started, and live until
the server is shutdown Servlets are installed as permanent extensions to a
server when their start-up costs are very high (such as establishing a
connection with a DBMS), they offer permanent server-side functionality (such as
an RMI service) they must respond as fast as possible to client requests.
![Page 22: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/22.jpg)
Key Servlet Packages
Java Servlet API Defines the interface between servlets and
servers javax.servlet javax.servlet.http
![Page 23: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/23.jpg)
javax.servlet.Servlet Interface
init(ServletConfig) Initializes the servlet.
service(ServletRequest, ServletResponse) Carries out a single request from the client.
destroy() Called by the servlet container to indicate to a servlet that the
servlet is being taken out of service. allow servlet to clean up any resources (such as open files or
database connections) before the servlet is unloaded
![Page 24: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/24.jpg)
getServletConfig() • Returns a ServletConfig object, which contains
any initialization parameters and startup configuration for this servlet.
getServletInfo() • Returns a string containing information about
the servlet
![Page 25: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/25.jpg)
HTTP Support Classes javax.servlet.http.HttpServlet provides an implementation
of the javax.servlet.Servlet interface The easiest way to write an HTTP servlet is to extend
HttpServlet and add your own processing. The class HttpServlet provides a service() method that
dispatches the HTTP messages to one of several methods: doGet(), doPost(), doHead(), doDelete(), doPut() …
corresponding directly with the HTTP protocol methods.
![Page 26: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/26.jpg)
A servlet's doGet() method should Read request data, such as input parameters
http://localhost/servlets/LoanCalculator?principal=200&interest=0.05 ·····
Set response headers (length, type, and encoding)
Write the response data
![Page 27: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/27.jpg)
HelloServlet Exampleimport java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body><head><title>Hello 서블릿 </title></head>");
out.println("<h1> 첫번째 서블릿 입니다 . </h1>");
out.println("</body></html>");
}
![Page 28: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/28.jpg)
![Page 29: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/29.jpg)
Servlet - features Platform-independent Performance Easily extended due to class/package/bean
concepts in Java
![Page 30: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/30.jpg)
JSP
![Page 31: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/31.jpg)
JSP
Why do we need JSP technology if we already have servlets?
![Page 32: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/32.jpg)
JSP Combine static HTML and dynamic Java in
one programming concept Simplified, fast creation of web pages that
display dynamically-generated content. Separation of web presentation from web
content Microsoft’s similar concept
Active Server Pages
![Page 33: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/33.jpg)
JSP JSP code is compiled into Java servlet
class, then executed Remains in memory Only when any change in source file,
repeat the first step
![Page 34: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/34.jpg)
JSP tags
Comment <!-- comment --> : HTML comment <%-- comment --%> : Hidden comment
Declaration <%! declaration; %>
Expression <%= expression %>
![Page 35: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/35.jpg)
JSP tags Scriptlet
<% code fragment %> Include Directive
<%@ include file=“relative URL” %> Page Directive
<%@ page language=“java” %> <%@ page import=“package.*” %> <%@ page contentType=“text/html” %> <%@ session=“true|false” %>
![Page 36: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/36.jpg)
Simple JSP Code
<html>
<head>
<title> 첫번째 jsp 페이지 </title>
</head>
<body bgcolor=ivory>
<center><h1>first jsp page</h1>
<%! String str=“NC Lab."; %>
<ul>
<% for (int i=0; i < 5; i++) {
out.println("<li>" + i);
}
%>
</ul>
<%= str %>
</center>
</body>
</html>
![Page 37: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/37.jpg)
![Page 38: Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection](https://reader035.vdocuments.net/reader035/viewer/2022081513/5697bfdb1a28abf838cb0a7d/html5/thumbnails/38.jpg)
More informations http://developer.java.sun.com/developer/onlineTraining/
Servlets/Fundamentals/servlets.html http://java.sun.com/products/servlet/2.3/javadoc
Servlet packages http://java.sun.com/j2ee/docs.html
J2EE Documents http://java.sun.com/products/jdk/1.2/docs/api
J2SE v1.2.2 API Specification http://www.javanuri.com http://www.javastudy.co.kr