280 likes | 409 Views
Middleware (2). Nclab M.S. Jinwon Lee. Contents. CGI, Servlet, JSP → Client Session Management JDBC (Java Database Connectivity) Connection pooling. Session. Connection Pooling. Overview. Client Browser. JDBC. Engine Supporting JSPs and Servlets. JSP,. Servlet.
E N D
Middleware (2) Nclab M.S. Jinwon Lee
Contents • CGI, Servlet, JSP → • Client Session Management • JDBC(Java Database Connectivity) • Connection pooling
Session Connection Pooling Overview Client Browser JDBC Engine Supporting JSPs and Servlets JSP, Servlet Database
Stateless HTTP Protocol • HTTP transaction is independent to each request. • Problems at Data Tracking, User Identification. • Ex) website login, shopping cart. • Need to mange state information between transaction requests and Web Application → Session Tracking
Session Tracking • Session Tracking • Manage states information • Client-side : Cookie • Server-side: HttpSession (Servlet API)
Cookies • A cookie is a piece of information that a site hands to your browser • The browser hands the site the cookie on each subsequent request • Unless the cookie is deleted, expired, modified..
Servlet/JSP support Cookies • Javax.serlvet.http package support Cookie class • Send to Client (HttpServletResponse class) Cookie myCookie = new Cookie(“key”, “value”) // Create cookie and Set attribute response.addCookie(myCookie); // Send Cookie to Client(Cookie is part of the HTTP response header.) • Receive from Client(HttpServletRequest class) Cookie [] allCookie = request.getCookies(); // Receive allCookie from Client String value = myCookie[i].getValue(); // Find the specific one you are interested in
Cookies don’t always taste good • Cookies can only contain small amounts of data (all cookie data are strings) • Cookies might not be accepted by the browser • Cookies can get stale → Use Object in Server-side (HttpSession)
HttpSession • A session is a continuous connection from the same browser over a fixed period of time. • Default is 30 minutes for Servlet Container • To maintain session information, Use session with browsercookies or encodeURL • Javax.servlet.http package support HttpSession interface
HttpSession • How to obtain HttpSession object HttpSession session = request.getSession(true); // read session id from cookie or encodeURL,and // if there are valid session object, return object. // else create new session with cookie • You can maintain a set of (key, value) paired information in the HttpSession object. • To store session-specific information, use the putValue(key, value) & getValue(key).
encodeURL • If the browser doesn’t accept cookies, session id is appended to each URL in the HTML response • When the rewritten URL is accessed, the server extracts the appended session id • String URL encodeURL(String URL); • session id is append to URL and return URL • [URL] http://local/servlet/MyServlet?id=nannom • [After encodeURL] http://local/servlet/MyServlet?id=nannom&JSevID= xxxxxxx
Session Implemention • Informations of HTTP transaction are managed by using HttpSession → Security, efficiency • To track the HttpSession, it is the best way that user accept cookie
JDBC • Application 개발시 Database system 과의 연동이 필수적 → JDBC(Java Database Connectivity) • JDBC API • Java로 작성된 Program을 일반 Database에 연결하기 위한 API 제공
JDBC Overview • DB와 User사이에 Abstract Layer 제공. • 한가지 API만을 이용하여 서로 상이한 DB사용 가능. • 대부분의 DB Vendor가 JDBC Spec.에 따라 JDBC Driver를 구현하여 제공.
Java Application JDBC API JDBC Driver Manager (java.sql.*) JDBC Driver API JDBC Driver DB
JAVA 어플리케이션 JDBC-ODBC Bridge 기존의 ODBC 드라이버 Native API( C/C++) DataBase protocol DataBase JDBC Driver • Type I : JDBC-ODBC bridge plus ODBC driver
Type II : Native-API partly-Java driver ( JDBC의 호출을 각 DB Client API로 변환하여 호출)
장점 • JDBC 호출을 DBMS의 고유한 Protocol로 직접 변환해 처리하기 때문에 처리 속도가 가장 빠르다. • Internet/Intranet에 가장 적합한 방식 • 단점 • DB 고유의 Protocol을 알고 있어야만 하므로 JDBC Driver Vendor가 만들기에는 어렵고 주로 DBMS Vendor에 의해 제공된다.
java.sql package JDBC 2.0 core API • Major interface • Making a connection with a data source • DriverManager class • Connection interface • Sending SQL statements to a database • Statement interface • Retrieving and updating the results of a query • ResultSet interface • Throwing exceptions • SQLException
Programming with JDBC API 1. DriverManager에 해당 DBMS driver등록 Class.forName(String className ); “com.jk.jdbc.Driver” 2. 해당 Driver로부터 Connection 객체 획득 String jdbcURL="jdbc:jk:kmindb1@143.248.148.160: 1433"; String user=“scott”; String passwd=“tiger”; Connection conn=DriverManager.getConnection( jdbcURL, user, passwd);
3. Connection 객체로부터 Statement 객체 획득 Statement stmt=conn.createStatement(); 4. Statement method를 이용하여 SQL 실행 ResultSet rset=stmt.executeQuery(“SELECT id, name FROM emp”); 5. 결과를 ResultSet으로 받아서 처리 while(rset.next()) { System.out.println(“사번:” + rset.getInt(“id”)); System.out.println(“이름:” + rset.getString(“name”)); } 6. 종료 rset.close(); stmt.close(); conn.close();
Problems • Accessing the database can be slow • Lots of years and tears are spent optimizing database access • An obvious optimization is to reuse the JDBC connections
References • Books • Professional Java server programming • Website • http://java.sun.com/servlet • http://www.javanuri.net (servlet/jsp lecture 7 ~ 9)