Java web programming

4,494 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,494
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
42
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Java web programming

    1. 1. Java Web & JDBC Programming by qrtt1, 2012.8
    2. 2. about mework for Muzee Co.lang: Java, c, pythonRDMIS (Server Admin)Performance Tuning
    3. 3. Write a Simple Web App TODO List user login todo list todo new todo modify todo delete todo
    4. 4. 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
    5. 5. Needs SkillsUse the Database SystemJDBC ProgrammingWeb App Programming View Presentation: JSP Business logic: Servlet & related APIs
    6. 6. Development EnvironmentTomcat 7 (Servlet/JSP Container) Support Servlet 2.x/3.xMySQL & Connector/JEclipse for JavaEE
    7. 7. Tomcat 7.xServlet & JSP Container
    8. 8. MySQL settingsset UTF-8 as default encodingset INNODB as default engine (if ver < 5.5) enable innodb_file_per_table
    9. 9. Eclipse SettingsPreference Workspace Text File Encoding: UTF-8Web JSP Files Encoding:
    10. 10. JDBC OverviewPrepare Database (we use MySQL today)Understand JDBC API Driver Common UsageUse Connection PoolLeverage JDBC helper library
    11. 11. Prepare MySQLUsing MySQL ShellManage User create account = id + host grant privilegesManage Storage create database create tablesDo Query CRUD
    12. 12. 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
    13. 13. Lab JDBCDatabase: todo_web table: users table: todosQuery find a user by emailUpdate the content of a todo
    14. 14. DAO & JDBC HelperCommon DBCPCommon DBUtilsMapping POJO and the row of a table
    15. 15. 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}
    16. 16. JDBC Programming QANext Web Programming
    17. 17. Web App ProgrammingWithout Servlet/JSPUsing Servlet/JSPWeb Framework
    18. 18. Browsingbrowser web server Get ME URL It’s the content of your URL
    19. 19. HTTP Protocolbrowser web server HTTP REQUEST HTTP RESPONSE
    20. 20. Let’s Speak In HTTPbrowser web server HTTP REQUEST GET /index.html HTTP/1.0rn Host: www.google.comrn rn HTTP RESPONSE HTTP 200 OKrn Content-Length: 16384rn Content-Type: text/html; charset=utf-8rn rn <html>......
    21. 21. Socket Programming (client.getOutputStream().write (“ ”) GET /index.html HTTP/1.0rn Host: www.google.comrn rn .getBytes() )socket client socket server client.getOutputStream() ( (“ ”) HTTP 200 OKrn ) Content-Length: 16384rn Content-Type: text/html; charset=utf-8rn .write rn .getBytes() <html>......
    22. 22. Dynamic Contentbrowser web server HTTP REQUEST GET /index.html HTTP/1.0rn Host: www.google.comrn rn CGI (Common Gateway Interface) HTTP RESPONSE Static Resource Handler
    23. 23. Common Gateway InterfaceHTTP REQUEST web server URI Dispatcher /icon/funny.png /cgi-bin/weather.pl /index.html /cgi-bin/stocks.pl Static Resource Handler CGI Handler HTTP RESPONSE
    24. 24. CGI is a language-free-method How does CGI work ? web server HTTP REQUESTCGI Handler parsing HTTP Request to ENVIRONMENT variable dispatch depends on URI redirect HTTP Request body to STDIN CGI Application #!/usr/bin/perl print "Content-type:text/htmlrnrn"; 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; Generate HTTP Response to STDOUT HTTP RESPONSE
    25. 25. How does Others work ? web server HTTP REQUESTOthers Handler parsing HTTP Request to XXX language variable (defined like CGI does) dispatch depends on URI redirect HTTP Request body to STDIN 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] Generate HTTP Response by OUTPUT method (In this case, it is a callable object start_response and return the content) HTTP RESPONSE
    26. 26. How does Java Servlet work ? web server HTTP REQUESTServlet Contiainer 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>"); } } Generate HTTP Response by HttpServletResponse object HTTP RESPONSE
    27. 27. DemoWrite Java Web App the hard way Let’s reinvent the wheel 沒有 Java Web Tech. 之前,該如何用 Java 實 作 Web Application ?
    28. 28. Web Programming QANext Java Web Programming
    29. 29. Web Programming In Java Web Application Servlet Filter Listener JSP Server-Side Resource. Ex. Database
    30. 30. Lab First Web App: Hello Servlet Implements a Servlet extends HttpServlet resp.getWriter().println(...) Set Up Deployment Descriptor WEB-INF/web.xml
    31. 31. Servlet FeaturesHttpServlet for business logicFilter usually for decorating or apply changes to HttpServletRequest or HttpServletResponseListener & Events usually for initializing and cleaning up resources.
    32. 32. FilterA filter is a reusable piece of code that cantransform the content of HTTP requests,responses, and header information. Filters donot generally create a response or respondto a request as servlets do, rather theymodify or adapt the requests for a resource,and modify or adapt responses from aresource.
    33. 33. Web Event & ListenerServlet Events javax.servlet.ServletContextListener javax.servlet.ServletContextAttributeListenerHttp Session Events javax.servlet.http.HttpSessionListener javax.servlet.http.HttpSessionAttributeListener javax.servlet.http.HttpSessionActivationListener javax.servlet.http.HttpSessionBindingListenerServlet Request Events javax.servlet.ServletRequestListener javax.servlet.ServletRequestAttributeListener
    34. 34. Hello JSPIt’s so easy: out.println(“Hello JSP”)Very features in JSP should know Directive, Action, Taglibs & JSTL Expression Language (EL) Implicit Objects notorious scriptlet <% ... %>
    35. 35. ex. A heavy JSP WebProject Name: TodoJspWebTODO list A user can login/logout A user can create, update, and delete the todo item
    36. 36. JSP should be simpleWe ONLY USE page, taglib and include directive @page for http header settings @taglib for jstl declaration @include for assemble page fragments EL to show variablesBusiness Logic is not my business.
    37. 37. 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 令人肅然起敬
    38. 38. HTTP REQUEST Model 2 HTTP RESPONSEservlet/jsp container Prepare View (JSP) show data from {application, session, request, ...} Flow Controller (Servlet) HttpServletResponse set up data to {application, session, request, ...} HttpServletRequest Business Object Business Object
    39. 39. ex. A Model 2 WebProject Name: TodoModel2WebA Mini Web Framework web.framework.WebController web.framework.ActionMapper
    40. 40. Java Web Programming QA Any Questions or Suggestions !?

    ×