Wednesday, January 23, 2013

ServletContextListener

Program to demonstrate use of ServletContextListener

1. Open Link:  http://localhost:8080/Excercise/MailContext

The context parameter "adminMail" is made as an attribute and database connection is made when the context is initialized. The listener which checks for context initialization is MailContextListener. 

web.xml(To be stored in folder called apache tomcat/webapps/Excercise/WEB-INF in webapps):


<web-app....>

<context-param>

<param-name>adminMail</param-name>
<param-value>java123@oracle.com</param-value>
</context-param>

<listener>

<listener-class>pack1.work.Controller.MailContextListener</listener-class>
</listener>

<servlet>

<servlet-name>ContextL</servlet-name>
<servlet-class>pack1.work.Controller.ContextListenerTester</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ContextL</servlet-name>
<url-pattern>/MailContext</url-pattern>
</servlet-mapping>
</web-app>

/* This Listener gets the init parameter from DD, passes to Mail class constructor to make a object, set it as an attribute */
MailContextListener (.class to be stored in folder apache tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )

package pack1.work.Controller;


import javax.servlet.*;

public class MailContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event){
ServletContext context = event.getServletContext();
String mail = (String)context.getInitParameter("adminMail");
Mail mail_id = new Mail(mail);
context.setAttribute("admin", mail_id);
}
public void contextDestroyed(ServletContextEvent event){
             /*The attributes get released when context is destroyed, so no memory release functionality to be added*/

}
}

/*Simple method which gets the string through constructor*/
Mail.java (.class to be stored in folder apache tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )
package pack1.work.Controller;

public class Mail {
public String mail_id;
public Mail(String mail){
mail_id=mail;
}

}

/*Servlet to display the attribute set by Listener when context is initialized.*/
ContextListenerTester(.class to be stored in folder apache tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )

package pack1.work.Controller;


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ContextListenerTester extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Mail mail = (Mail)getServletContext().getAttribute("admin");
pw.println("The admin id is " + mail.mail_id);
}
}

Wednesday, January 16, 2013

Cookie useage

Program to demonstrate data stored in cookies being shared across web-app.

1. Open Link:  http://localhost:8080/Excercise/view.html
2. Fill in user name and password.
3. Select the  items to shop.
4. 3rd page displays the uesr name and password(though unlikely in real world) stored from cookies and List of items to be shopped.

 web.xml(To be stored in folder called apache-tomcat/webapps/Excercise/WEB-INF in webapps):
<web-app ...>
<servlet>
<servlet-name>cookie1</servlet-name>
<servlet-class>pack1.work.Controller.NameStore</servlet-class>
</servlet>

<servlet>
<servlet-name>cookie2</servlet-name>
<servlet-class>pack1.work.Controller.ItemStore</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>cookie1</servlet-name>
<url-pattern>/Namedisplay</url-pattern>
</servlet-mapping>
    
<servlet-mapping>
<servlet-name>cookie2</servlet-name>
<url-pattern>/Itemdisplay</url-pattern>
</servlet-mapping>
</web-app>

view.html (To be stored in folder webapps/Excercise )


<html>
<body>

Enter user name and password for detail display of shopping List:

  <form method = "POST" action = "Namedisplay">
User name: <input name="username" type="text">
Password: <input name="pwd" type="password"></br>


<input value ="Submit" type="SUBMIT">

  </form>

</body>
</html>

NameStore,java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )
/* File to store username and password in cookies*/

package pack1.work.Controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class NameStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");

String usrname =  request.getParameter("username");
String password =  request.getParameter("pwd");
HttpSession session = request.getSession();

session.setMaxInactiveInterval(20*60);
Cookie cookie1 = new Cookie("Name", usrname);
Cookie cookie2 = new Cookie("Cryptic", password);

response.addCookie(cookie1);
response.addCookie(cookie2);

cookie1.setMaxAge(20*60);
cookie2.setMaxAge(20*60);

RequestDispatcher view = request.getRequestDispatcher("ShopCart.jsp");
view.forward(request, response);
}

}

ShopCart.jsp((To be stored in folder apache-tomcat/webapps/webapps/Excercise ))
/*Jsp file that  takes you to other servlet*/
<html>
<body>
Welcome to Shopping World!!  Shop unlimited on following items!!</br>

Hi

<form method="Post" action="Itemdisplay">
<input type="checkbox" name="Shop" value = "Dress" >Dresses</br>
<input type="checkbox" name="Shop" value = "Acessories">Acessories</br>
<input type="checkbox" name = "Shop" value = "Sandals">Sandals</br>
<input type="checkbox" name = "Shop" value = "Electronics">Electronic Items</br>
<input type="checkbox" name = "Shop" value = "Vehicle">Vehicles</br>

<input type = "submit" value = "Items selected!!!">
</form>

</body>
</html>



ItemStore.java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller)

/*Program retrieves the username and password cookie.*/

package pack1.work.Controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ItemStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie cookies[] = request.getCookies();
String user="", password="";
for(int i=0; i<cookies.length; i++){

if(cookies[i].getName().equals("Name")){
user = cookies[i].getValue();

}

else if(cookies[i].getName().equals("Cryptic")){
password = cookies[i].getValue();
}

}

String[] options = request.getParameterValues("Shop");

out.println("</br>");
out.println("Hi, Your login id is: " + user + "Your password is : " + password);
out.println("</br> Above info is brought to u by COOKIES!!!");
out.println("</br> You want ");
for(int i=0; i<options.length; i++){
out.println(options[i] + " ");
}
out.println("from this site");


}
}