Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Web & JDBC Programming Introduction

Java Web & JDBC Programming Introduction

Ching Yi Chan

August 18, 2012
Tweet

More Decks by Ching Yi Chan

Other Decks in Education

Transcript

  1. about me work for Muzee Co. lang: Java, c, python

    RD MIS (Server Admin) Performance Tuning
  2. Write a Simple Web App TODO List user login todo

    list todo new todo modify todo delete todo
  3. Source Code About this tutorial web server and handler https:/

    /github.com/qrtt1/tut_web pure jdbc lab https:/ /github.com/qrtt1/tut.TodoJdbc jdbc + dbcp + dbutils https:/ /github.com/qrtt1/tut.TodoDao todo web in model 1 https:/ /github.com/qrtt1/tut.TodoJspWeb todo web in model 2 https:/ /github.com/qrtt1/tut.TodoModel2Web
  4. Needs Skills Use the Database System JDBC Programming Web App

    Programming View Presentation: JSP Business logic: Servlet & related APIs
  5. MySQL settings set UTF-8 as default encoding set INNODB as

    default engine (if ver < 5.5) enable innodb_file_per_table
  6. JDBC Overview Prepare Database (we use MySQL today) Understand JDBC

    API Driver Common Usage Use Connection Pool Leverage JDBC helper library
  7. Prepare MySQL Using MySQL Shell Manage User create account =

    id + host grant privileges Manage Storage create database create tables Do Query CRUD
  8. pure JDBC programming Load JDBC Driver Class.forName(...) Prepare JDBC URL

    jdbc:mysql:/ /... Get Connection from DriverManager With URL Do Query With Connection Object PreparedStatement, Statement executeQuery, executeUpdate
  9. Lab JDBC Database: todo_web table: users table: todos Query find

    a user by email Update the content of a todo
  10. Lab JDBC Helper & DAO Use Connection Pool set up

    validate string DBUtils use QueryRunner and ResultSetHandler Work with Object {User, Todo} access by DAO {UserDao, TodoDao}
  11. browser web server HTTP REQUEST HTTP RESPONSE Let’s Speak In

    HTTP GET /index.html HTTP/1.0\r\n Host: www.google.com\r\n \r\n HTTP 200 OK\r\n Content-Length: 16384\r\n Content-Type: text/html; charset=utf-8\r\n \r\n <html>......
  12. socket client socket server Socket Programming GET /index.html HTTP/1.0\r\n Host:

    www.google.com\r\n \r\n (“ ”) .getBytes() client.getOutputStream().write ( ) HTTP 200 OK\r\n Content-Length: 16384\r\n Content-Type: text/html; charset=utf-8\r\n \r\n <html>...... client.getOutputStream() .write ( ) (“ ”) .getBytes()
  13. browser web server HTTP REQUEST HTTP RESPONSE Dynamic Content CGI

    (Common Gateway Interface) GET /index.html HTTP/1.0\r\n Host: www.google.com\r\n \r\n Static Resource Handler
  14. web server HTTP REQUEST HTTP RESPONSE Common Gateway Interface CGI

    Handler Static Resource Handler URI Dispatcher /icon/funny.png /index.html /cgi-bin/weather.pl /cgi-bin/stocks.pl
  15. HTTP REQUEST How does CGI work ? CGI Handler web

    server parsing HTTP Request to ENVIRONMENT variable redirect HTTP Request body to STDIN dispatch depends on URI CGI Application #!/usr/bin/perl print "Content-type:text/html\r\n\r\n"; print '<html>'; print '<head>'; print '<title>Hello Word - First CGI Program</title>'; print '</head>'; print '<body>'; print '<h2>Hello Word! This is my first CGI program</h2>'; print '</body>'; print '</html>'; 1; HTTP RESPONSE Generate HTTP Response to STDOUT CGI is a language-free-method
  16. HTTP REQUEST How does Others work ? Others Handler web

    server parsing HTTP Request to XXX language variable (defined like CGI does) redirect HTTP Request body to STDIN dispatch depends on URI Other Server Side Programming Language def simple_app(environ, start_response): """Simplest possible application object""" status = '200 OK' response_headers = [('Content-type','text/plain')] start_response(status, response_headers) return ['Hello world!\n'] HTTP RESPONSE Generate HTTP Response by OUTPUT method (In this case, it is a callable object start_response and return the content)
  17. HTTP REQUEST How does Java Servlet work ? Servlet Contiainer

    web server parsing HTTP Request to HttpServletRequest object dispatch depends on Servlet URI mapping (defined in web.xml, also in annotation after servlet 3.0) Java Servlet public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("<html><body>"); resp.getWriter().write("Hello Servlet"); resp.getWriter().write("</body></html>"); } } HTTP RESPONSE Generate HTTP Response by HttpServletResponse object
  18. Demo Write Java Web App the hard way ӚϞ Java

    Web Tech. ʘۃd༈νО͜ Java ྼ Ъ Web Application ճk Let’s reinvent the wheel
  19. Lab First Web App: Hello Servlet Implements a Servlet extends

    HttpServlet resp.getWriter().println(...) Set Up Deployment Descriptor WEB-INF/web.xml
  20. Servlet Features HttpServlet for business logic Filter usually for decorating

    or apply changes to HttpServletRequest or HttpServletResponse Listener & Events usually for initializing and cleaning up resources.
  21. Filter A filter is a reusable piece of code that

    can transform the content of HTTP requests, responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.
  22. Web Event & Listener Servlet Events javax.servlet.ServletContextListener javax.servlet.ServletContextAttributeListener Http Session

    Events javax.servlet.http.HttpSessionListener javax.servlet.http.HttpSessionAttributeListener javax.servlet.http.HttpSessionActivationListener javax.servlet.http.HttpSessionBindingListener Servlet Request Events javax.servlet.ServletRequestListener javax.servlet.ServletRequestAttributeListener
  23. Hello JSP It’s so easy: out.println(“Hello JSP”) Very features in

    JSP should know Directive, Action, Taglibs & JSTL Expression Language (EL) Implicit Objects notorious scriptlet <% ... %>
  24. ex. A heavy JSP Web Project Name: TodoJspWeb TODO list

    A user can login/logout A user can create, update, and delete the todo item
  25. JSP should be simple We ONLY USE page, taglib and

    include directive @page for http header settings @taglib for jstl declaration @include for assemble page fragments EL to show variables Business Logic is not my business.
  26. Java Web Programming Model Everything in JSP = Model 1

    Web MVC = Model 2 flow control in Servlet business logic delegate to other Object jsp for view only Everything in Servlet = Model Awesome ˿ɛഠ್ৎห
  27. servlet/jsp container HTTP REQUEST Business Object Model 2 Flow Controller

    (Servlet) Prepare View (JSP) HttpServletRequest HttpServletResponse HTTP RESPONSE Business Object set up data to {application, session, request, ...} show data from {application, session, request, ...}
  28. ex. A Model 2 Web Project Name: TodoModel2Web A Mini

    Web Framework web.framework.WebController web.framework.ActionMapper