postingan ini adalah lanjutan yang sebelumnya tentang Servlet Filter, kali ini saya akan sharing tentang program Java Servlet Context dan Listener Database. Langsung aja kita buat programnya
pertama kita buat index.html
kemudian kita buat class bernama DBConnectionManager.java
source codenya sebagai berikut :
import java.sql.Connection; /** * * @author Ilyas A. Jaelani */ public class DBConnectionManager { private String dbURL; private String user; private String password; private Connection con; public DBConnectionManager(String url, String u, String p) { this.dbURL = url; this.user = u; this.password = p; //create db connection now } public Connection getConnection() { return this.con; } public void closeConnection() { //close DB connection here } }
buat file servlet MyServlet.java
source codenya sebagai berikut :
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * @author Ilyas A. Jaelani */ public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Processes requests for both HTTP *GET
and *POST
methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); out.println("Servlet MyServlet "); out.println(""); out.println(""); out.println("Servlet MyServlet at " + request.getContextPath() + "
"); out.println(""); out.println(""); } finally { out.close(); } } ///** * Handles the HTTP * }GET
method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // processRequest(request, response); ServletContext ctx = request.getServletContext(); ctx.setAttribute("User", "Ilyas"); String user = (String) ctx.getAttribute("User"); ctx.removeAttribute("User"); HttpSession session = request.getSession(); session.invalidate(); PrintWriter out = response.getWriter(); out.write("Hi " + user); } /** * Handles the HTTP *POST
method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }//
Tambahkan file listener baru dengan nama AppContextListener.java. Ikuti langkah sebagai berikut :
import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Web application lifecycle listener. * * @author Ilyas A. Jaelani */ public class AppContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { ServletContext ctx = sce.getServletContext(); String url = ctx.getInitParameter("DBURL"); String u = ctx.getInitParameter("DBUSER"); String p = ctx.getInitParameter("DBPWD"); //create database connection from init parameters and set it to context DBConnectionManager dbManager = new DBConnectionManager(url, u, p); ctx.setAttribute("DBManager", dbManager); System.out.println("Database connection initialized for Application."); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void contextDestroyed(ServletContextEvent sce) { ServletContext ctx = sce.getServletContext(); DBConnectionManager dbManager = (DBConnectionManager) ctx.getAttribute("DBManager"); dbManager.closeConnection(); System.out.println("Database connection closed for Application."); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
Tambahkan file listener baru dengan nama AppContextAttributeListener.java.
import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Web application lifecycle listener. * * @author Ilyas A. Jaelani */ public class AppContextAttributeListener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent event) { System.out.println("ServletContext attribute added::{" + event.getName() +"," + event.getValue() + "}"); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void attributeRemoved(ServletContextAttributeEvent event) { System.out.println("ServletContext attribute replaced::{" + event.getName() + "," + event.getValue() + "}"); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void attributeReplaced(ServletContextAttributeEvent event) { System.out.println("ServletContext attribute removed::{" + event.getName() + "," + event.getValue() + "}"); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
Tambahkan file listener baru dengan nama MySessionListener.java.
import javax.servlet.http.HttpSessionEvent; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Web application lifecycle listener. * * @author Ilyas A. Jaelani */ public class MySessionListener implements javax.servlet.http.HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("Session Created:: ID=" + se.getSession().getId()); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("Session Destroyed:: ID=" + se.getSession().getId()); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
Tambahkan file listener baru dengan nama MyServletRequestListener.java.
import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequest; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Web application lifecycle listener. * * @author Ilyas A. Jaelani */ public class MyServletRequestListener implements javax.servlet.ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { ServletRequest servletRequest = sre.getServletRequest(); System.out.println("ServletRequest destroyed. Remote IP=" + servletRequest.getRemoteAddr()); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void requestInitialized(ServletRequestEvent sre) { ServletRequest servletRequest = sre.getServletRequest(); System.out.println("ServletRequest initialized. Remote IP=" + servletRequest.getRemoteAddr()); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
0 komentar:
Posting Komentar