assalamualaikum kawan-kawan :)
postingan ini adalah lanjutan yang sebelumnya tentang Servlet Filter, kali ini saya akan sharing tentang program Java Servlet Context dan Listener. Langsung aja kita buat programnya
pertama kita buat
index.html
kemudian kita tambahkan script dibawah ini :
lalu kita buat
ServletContextClass.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
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 ROOT
*/
public class ServletContextClass extends HttpServlet {
/**
* 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*/
out.println("");
out.println("");
out.println("Servlet ServletContextClass");
out.println("");
out.println("");
out.println("
Servlet ServletContextClass 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);
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext ctx = getServletContext();
ctx.setAttribute("name", "Eresha Wikrama");
out.print(ctx.getAttribute("name"));
Enumeration temp = ctx.getAttributeNames();
while (temp.hasMoreElements()) {
String attrName = (String) temp.nextElement();
out.print("Attribut " + attrName + " , nilainya = " + ctx.getAttribute(attrName));
}
out.print("
getServletContextName() : " + ctx.getServletContextName());
out.print("
" + ctx.getInitParameter("bhs1"));
Enumeration temp2 = ctx.getInitParameterNames();
while (temp2.hasMoreElements()) {
String paramName = (String) temp2.nextElement();
out.print("
Attribut " + paramName + " , nilainya = " + ctx.getInitParameter(paramName));
}
out.print("
getServerInfo() : " + ctx.getServerInfo());
out.print("
getRealPath() : " + ctx.getRealPath(""));
ctx.log("Hello world");
ctx.setAttribute("name", "Eresha Wikrama Bogor, Teknik Informatika");
ctx.setAttribute("name", null);
request.setAttribute("word", "never say give up in this world");
request.setAttribute("word", "do your best and let God do the rest");
request.setAttribute("word", null);
HttpSession session = request.getSession();
session.setAttribute("userSession", "EreshaWikramaSession");
session.setAttribute("userSession", "Mari belajar JAVA");
session.setAttribute("userSession", null);
}
/**
* 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";
}//
}
Kemudian buat sebuah 6 buah class untuk mengimplementasikan setiap interface listener
di atas. Sebenarnya buat 1 class juga bisa untuk mengimplementasikan keenam interface
tersebut. Namun ada baiknya dipisah saja per interface, supaya lebih mudah dicerna.
ServletContextListener
Tambahkan file listener baru dengan nama
ServletContextListenerClass.java. Ikuti langkah
berikut ini:
jikalau tidak ada, pilih other terlebih dahulu
source codenya sebagai berikut :
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Web application lifecycle listener.
* @author ROOT
*/
public class HttpSessionListenerClass implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session " + se.getSession().getId() + " sudah dibuat");
}
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session " + se.getSession().getId() + " sudah dihancurkan");
}
}
}
ServletContextAttributeListener
Selanjutnya buat file ServerListener dengan nama
ServletContextAttributeListenerClass.java,
pada bagian Interfaces to Implement, pilih Context Attribute Listener.
source codenya sebagai berikut :
import javax.servlet.ServletContext;
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 ROOT
*/
public class ServletContextAttributeListenerClass implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
ServletContext ctx = event.getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah ditambahkan");
}
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
ServletContext ctx = event.getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah dihapus");
}
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
ServletContext ctx = event.getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah diubah");
}
}
}
ServletRequestListener
Selanjutnya buat file ServerListener dengan nama
ServletRequestListenerClass.java, pada kolom
Interfaces to Implement pilih Request Listener:
source codenya sebagai berikut :
import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Web application lifecycle listener.
* @author ROOT
*/
public class ServletRequestListenerClass implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
ServletContext ctx = sre.getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "request " + sre.getServletRequest() + " sudah dihancurkan");
}
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
ServletContext ctx = sre.getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "request " + sre.getServletRequest() + " sudah diinisialisasi");
}
}
}
ServletRequestAttributeListener
Selanjutnya buat file ServerListener dengan nama
ServletRequestAttributeListenerClass.java,
pada bagian Interfaces to Implement, pilih Context Attribute Listener.
source codenya sebagai berikut :
import javax.servlet.ServletRequestAttributeEvent;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Web application lifecycle listener.
*
* @author ZOMBIE
*/
public class ServletRequestAttributeListenerClass implements javax.servlet.ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent event) {
System.out.println("Attribute ditambahkan : " + event.getName() + " = " + event.getValue());
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent event) {
System.out.println("Attribute dihapus : " + event.getName() + " = " + event.getValue());
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent event) {
System.out.println("Attribute diganti : " + event.getName() + " = " + event.getValue());
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
HttpSessionAttributeListener
Selanjutnya buat file ServerListener dengan nama
HttpSessionAttributeListenerClass.java, pada
kolom Interfaces to Implement pilih HTTP Session Attribute Listener:
source codenya sebagai berikut :
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Web application lifecycle listener.
* @author ROOT
*/
public class HttpSessionAttributeListenerClass implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
ServletContext ctx = event.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah ditambahkan");
}
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
ServletContext ctx = event.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah dihapus");
}
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
ServletContext ctx = event.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session attribute " + event.getName() + " dengan nilai : " + event.getValue()
+ " sudah diubah");
}
}
}
HttpSessionListener
Selanjutnya buat file ServerListener dengan nama
HttpSessionListenerClass.java, pada
kolom Interfaces to Implement pilih HTTP Session Attribute Listener:
source codenya sebagai berikut :
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Web application lifecycle listener.
* @author ROOT
*/
public class HttpSessionListenerClass implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session " + se.getSession().getId() + " sudah dibuat");
}
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
if (ctx != null) {
ctx.log("Context " + ctx.getServletContextName() + ", "
+ "session " + se.getSession().getId() + " sudah dihancurkan");
}
}
}
di file web.xml yang ada di folder WEB-INF kita tambahkan code berikut
bhs1
Java
bhs2
C++
bhs3
C
bhs4
VB.Net
bhs5
JSP
ServletContextListener
ServletContextListenerClass
ServletContextAttributeListener
ServletContextAttributeListenerClass
RequestListener
ServletRequestListenerClass
HttpSessionListener
HttpSessionListenerClass
RequestAttributeListener
HttpSessionAttributeListenerClass
ServletContext
ServletContext
ServletContextClass
ServletContextClass
ServletContext
/ServletContext
ServletContextClass
/ServletContextClass
30
source core selesai
tinggal kita run program. maka tampilan akhirnya seperti ini :
Semoga bermanfaat dan terimakasih :)