JDBC (Java DataBase Connectivity) There are four types of JDBC drivers: 1. JDBC-ODBC Bridge Driver 2. Native Driver 3. Network Protocol Driver 4. Thin Driver
1) JDBC-ODBC bridge driver The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.
Advantages 1. easy to use. 2. can be easily connected to any database. Disadvantages 1. Performance degraded because JDBC method call is converted into the ODBC function calls. 2. The ODBC driver needs to be installed on the client machine. 2. Native-API driver The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.
Advantages 1. performance upgraded than JDBC-ODBC bridge driver. Disadvantages 1. The Native driver needs to be installed on the each client machine. 2. The Vendor client library needs to be installed on client machine.
3) Network Protocol driver The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.
Advantages 1. No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.. Disadvantages 1. Network support is required on client machine. 2. Requires database-specific coding to be done in the middle tier. 3. Maintenance of Network Protocol driver becomes costly because it requires database- specific coding to be done in the middle tier. 4) Thin driver The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.
Advantages 1. Better performance than all other drivers. 2. No software is required at client side or server side. Disadvantages 1. Drivers depend on the Database.
Java Database Connectivity with 5 Steps 1. Register the Driver class 2. Create connection 3. Create statement 4. Execute queries 5. Close connection
Servlet 1. Servlet is a technology which is used to create a web application. 2. Servlet is an API that provides many interfaces and classes including documentation. 3. Servlet is an interface that must be implemented for creating any Servlet. 4. Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. 5. Servlet is a web component that is deployed on the server to create a dynamic web page.
Advantages of Servlet 1. Better performance: because it creates a thread for each request, not process. 2. Portability: because it uses Java language. 3. Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc. 4. Secure: because it uses java language.
Servlet Life Cycle
Steps for Servlet execution 1. Servlet class is loaded. 2. Servlet instance is created. 3. init method is invoked. 4. service method is invoked. 5. destroy method is invoked.
HttpServlet Class The HttpServlet class extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, etc. Methods of HttpServlet class There are many methods in HttpServlet class. They are as follows: 1. public void service(ServletRequest req,ServletResponse res) dispatches the request to the protected service method by converting the request and response object into http type. 2. protected void service(HttpServletRequest req, HttpServletResponse res) receives the request from the service method, and dispatches the request to the method depending on the incoming http request type. 3. protected void doGet(HttpServletRequest req, HttpServletResponse res) handles the GET request. It is invoked by the web container. 4. protected void doPost(HttpServletRequest req, HttpServletResponse res) handles the POST request. It is invoked by the web container.
Apache Tomcat Server 1. Apache Tomcat is an open source web server that is developed by Apache software foundation. It basically make our Java Web applications to run on host and server based system and it is configured on local host port 8080. 2. Apache Tomcat is used to deploy your Java Servlets and JSPs. So in your Java project you can build your WAR (short for Web ARchive) file, and just drop it in the deploy directory in Tomcat. So basically Apache is an HTTP Server, serving HTTP. Tomcat is a Servlet and JSP Server serving Java technologies.
IDE’s (Integrated Development Environment) 1. Eclipse 2. MyEclipse 3. NetBeans 4. intelliJ Database SQL Queries • INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); // to insert the data into the database • SELECT  SELECT * FROM table_name; //to extract all records from database • WHERE  SELECT column1, column2, ... FROM table_name WHERE condition; //to extract record based on some condition • UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; //to update columns based on some condition • DELETE FROM table_name WHERE condition; //to delete particular record
Statement VS PreparedStatement
Cookies in Servlet/JSP 1. A cookie is a small piece of information that is persisted between the multiple client requests. 2. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. How Cookie works By default, each request is considered as a new request. In cookies technique, we add cookie with response from the Servlet/JSP. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user. Types of Cookie There are 2 types of cookies in Servlets/JSP. 1. Non-persistent cookie 2. Persistent cookie
1. Non-persistent cookie It is valid for single session only. It is removed each time when user closes the browser. 2. Persistent cookie It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout. Advantage of Cookies 1. Simplest technique of maintaining the state. 2. Cookies are maintained at client side. Disadvantage of Cookies 1. It will not work if cookie is disabled from the browser. 2. Only textual information can be set in Cookie object.
Session in Servlet/JSP 1. Session simply means a particular interval of time. 2. Session Tracking is a way to maintain state (data) of an user. It is also known as session management. 3. Session is used To recognize the user It is used to recognize the particular user. HttpSession Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.
How to create Session object The HttpServletRequest interface provides two methods to get the object of HttpSession: 1. public HttpSession getSession(): Returns the current session associated with this request, or if the request does not have a session, creates one. 2. public HttpSession getSession(boolean create): Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
Commonly used methods of HttpSession interface 1. public String getId(): Returns a string containing the unique identifier value. 2. public long getCreationTime(): Returns the time when this session was created, measured in milliseconds. 3. public long getLastAccessedTime(): Returns the last time the client sent a request associated with this session, as the number of milliseconds. 4. public void invalidate(): Invalidates this session then unbinds any objects bound to it.

Advanced java+JDBC+Servlet

  • 2.
    JDBC (Java DataBaseConnectivity) There are four types of JDBC drivers: 1. JDBC-ODBC Bridge Driver 2. Native Driver 3. Network Protocol Driver 4. Thin Driver
  • 3.
    1) JDBC-ODBC bridgedriver The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.
  • 4.
    Advantages 1. easy touse. 2. can be easily connected to any database. Disadvantages 1. Performance degraded because JDBC method call is converted into the ODBC function calls. 2. The ODBC driver needs to be installed on the client machine. 2. Native-API driver The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.
  • 5.
    Advantages 1. performance upgradedthan JDBC-ODBC bridge driver. Disadvantages 1. The Native driver needs to be installed on the each client machine. 2. The Vendor client library needs to be installed on client machine.
  • 6.
    3) Network Protocoldriver The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.
  • 7.
    Advantages 1. No clientside library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.. Disadvantages 1. Network support is required on client machine. 2. Requires database-specific coding to be done in the middle tier. 3. Maintenance of Network Protocol driver becomes costly because it requires database- specific coding to be done in the middle tier. 4) Thin driver The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.
  • 8.
    Advantages 1. Better performancethan all other drivers. 2. No software is required at client side or server side. Disadvantages 1. Drivers depend on the Database.
  • 9.
    Java Database Connectivitywith 5 Steps 1. Register the Driver class 2. Create connection 3. Create statement 4. Execute queries 5. Close connection
  • 10.
    Servlet 1. Servlet isa technology which is used to create a web application. 2. Servlet is an API that provides many interfaces and classes including documentation. 3. Servlet is an interface that must be implemented for creating any Servlet. 4. Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. 5. Servlet is a web component that is deployed on the server to create a dynamic web page.
  • 11.
    Advantages of Servlet 1.Better performance: because it creates a thread for each request, not process. 2. Portability: because it uses Java language. 3. Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc. 4. Secure: because it uses java language.
  • 12.
  • 13.
    Steps for Servletexecution 1. Servlet class is loaded. 2. Servlet instance is created. 3. init method is invoked. 4. service method is invoked. 5. destroy method is invoked.
  • 14.
    HttpServlet Class The HttpServletclass extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, etc. Methods of HttpServlet class There are many methods in HttpServlet class. They are as follows: 1. public void service(ServletRequest req,ServletResponse res) dispatches the request to the protected service method by converting the request and response object into http type. 2. protected void service(HttpServletRequest req, HttpServletResponse res) receives the request from the service method, and dispatches the request to the method depending on the incoming http request type. 3. protected void doGet(HttpServletRequest req, HttpServletResponse res) handles the GET request. It is invoked by the web container. 4. protected void doPost(HttpServletRequest req, HttpServletResponse res) handles the POST request. It is invoked by the web container.
  • 15.
    Apache Tomcat Server 1.Apache Tomcat is an open source web server that is developed by Apache software foundation. It basically make our Java Web applications to run on host and server based system and it is configured on local host port 8080. 2. Apache Tomcat is used to deploy your Java Servlets and JSPs. So in your Java project you can build your WAR (short for Web ARchive) file, and just drop it in the deploy directory in Tomcat. So basically Apache is an HTTP Server, serving HTTP. Tomcat is a Servlet and JSP Server serving Java technologies.
  • 16.
    IDE’s (Integrated DevelopmentEnvironment) 1. Eclipse 2. MyEclipse 3. NetBeans 4. intelliJ Database SQL Queries • INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); // to insert the data into the database • SELECT  SELECT * FROM table_name; //to extract all records from database • WHERE  SELECT column1, column2, ... FROM table_name WHERE condition; //to extract record based on some condition • UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; //to update columns based on some condition • DELETE FROM table_name WHERE condition; //to delete particular record
  • 17.
  • 18.
    Cookies in Servlet/JSP 1.A cookie is a small piece of information that is persisted between the multiple client requests. 2. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. How Cookie works By default, each request is considered as a new request. In cookies technique, we add cookie with response from the Servlet/JSP. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user. Types of Cookie There are 2 types of cookies in Servlets/JSP. 1. Non-persistent cookie 2. Persistent cookie
  • 19.
    1. Non-persistent cookie Itis valid for single session only. It is removed each time when user closes the browser. 2. Persistent cookie It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout. Advantage of Cookies 1. Simplest technique of maintaining the state. 2. Cookies are maintained at client side. Disadvantage of Cookies 1. It will not work if cookie is disabled from the browser. 2. Only textual information can be set in Cookie object.
  • 20.
    Session in Servlet/JSP 1.Session simply means a particular interval of time. 2. Session Tracking is a way to maintain state (data) of an user. It is also known as session management. 3. Session is used To recognize the user It is used to recognize the particular user. HttpSession Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.
  • 21.
    How to createSession object The HttpServletRequest interface provides two methods to get the object of HttpSession: 1. public HttpSession getSession(): Returns the current session associated with this request, or if the request does not have a session, creates one. 2. public HttpSession getSession(boolean create): Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
  • 22.
    Commonly used methodsof HttpSession interface 1. public String getId(): Returns a string containing the unique identifier value. 2. public long getCreationTime(): Returns the time when this session was created, measured in milliseconds. 3. public long getLastAccessedTime(): Returns the last time the client sent a request associated with this session, as the number of milliseconds. 4. public void invalidate(): Invalidates this session then unbinds any objects bound to it.