Steps:
1. create any folder (ex: httpsess)
2. maintain the folder structure
3. set the classpath for tomcat server, jdbc connector
4. deploy your application(your folder ex" httpsess) in webapps folder under Tomcat server
5. start the server
6. execute your application in any browser
1. create any folder (ex: httpsess)
2. maintain the folder structure
Login.html
reg.jsp
registration.jsp
1. DriverConnection.java
reg.jsp
registration.jsp
1. DriverConnection.java
2. UserDAO.java
3.Product.java
4. ProductDAO.java
5. LoginServlet.java
6. UserHomeServlet.java
7. GetProductServlet.java
8. AddProductServlet.java
9. GetCartDetailsServlet.java
10. LogoutServlet.java
4. create WEB-INF folder
Under WEB-INF folder
(a) create classes folder
(b) create lib folder(mandatory): bcoz we are using jdbc drivers so mysql-connector-java-5.1.18-bin.jar should be copied into this folder
(c) create web.xml file for configuration
<html>
<head>
<title>Shopping Cart Example</title>
</head>
<body>
<table width="95%" height="82%" border="0">
<tr align="center">
<td height="39" colspan="2"><strong><font size="5">MyShopping Site</font></strong></td>
</tr>
<tr>
<td width="21%" height="478" valign="top"><p> </p>
<blockquote>
<p> </p>
</blockquote></td>
<td width="70%" align="center" valign="middle">
<p><font size="6">Login</font></p>
<form name="form1" method="post" action="login">
<table width="36%" border="0">
<tr>
<td width="45%"><strong>User Name</strong></td>
<td width="55%"><input type="text" name="uname"></td>
</tr>
<tr>
<td><strong>Password</strong></td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr align="right">
<td><div align="center">
<input type="submit" name="Submit" value="LogIN">
</div></td>
<td align="center"><input type="reset" name="Submit2" value="Reset"></td>
</tr>
<tr align="right">
<td>
<div align="center">
<input type="button" value="Put Your Text Here" onClick="window.location.href='reg.jsp'">
</div></td>
</tr>
</table>
</form></td>
</tr>
<tr align="center">
<td colspan="2"><em>© Copyrights 2005-09</em></td>
</tr>
</table>
</body>
</html>
//reg.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="registration.jsp" method="post">
<table border="0" width="30" cellpadding="5">
<thead>
<tr colspan="2"></tr>
<tr>
<td>First name</td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"/> </td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset"/></td>
</tr>
<tr>
<td colspan="2">Already Registered!! <a href="index.jsp">Login here</a></td>
</tr>
</thead>
</table>
</form>
</body>
</html>
//registration.jsp
<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String user=request.getParameter("uname");
String pass=request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "admin");
Statement st = con.createStatement();
String query="insert into registration values (fname,lname,email,user,pass)";
//Execute the query
int count=st.executeUpdate(query);
out.println("Number of Rows effected by this query="+count);
/*out.println(fname);
out.println(lname);
out.println(email);
out.println(user);
out.println(pass);*/
if(count>0)
out.println("Registered successfully"+"<a href='index.jsp'> Go to Login</a>");
else
response.sendRedirect("index.jsp");
%>
</body>
</html>
//create the servlet programs
1. DriverConnection.java
2. UserDAO.java
3.Product.java
4. ProductDAO.java
5. LoginServlet.java
6. UserHomeServlet.java
7. GetProductServlet.java
8. AddProductServlet.java
9. GetCartDetailsServlet.java
10. LogoutServlet.java
Maintain the folder structure:
//DriverConnection.java
/**
*
*/
package com.rajendra.servlets;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* @author rajendra
*
*/
public class DriverConnection {
public static Connection getConnection()
throws Exception{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","admin");
}
}
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs!=null)
hs.invalidate();
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
2. Now compil e the servlet(java) programs to get the class files
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UserHomeServlet</servlet-name>
<servlet-class>com.rajendra.servlets.UserHomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GetProductsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.GetProductsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AddProductsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.AddProductsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>GetCartDetailsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.GetCartDetailsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserHomeServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetProductsServlet</servlet-name>
<url-pattern>/getProducts</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddProductsServlet</servlet-name>
<url-pattern>/addProducts</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetCartDetailsServlet</servlet-name>
<url-pattern>/getCart</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Login.html</welcome-file>
</welcome-file-list>
</web-app>
}
//DriverConnection.java
/**
*
*/
package com.rajendra.servlets;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* @author rajendra
*
*/
public class DriverConnection {
public static Connection getConnection()
throws Exception{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","admin");
}
}
////UserDAO.JAVA
package com.rajendra.servlets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class UserDAO {
public boolean validate(String uname, String pass){
try {
Connection con=DriverConnection.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(
"select * from userdetails where uname=\'"+uname+"\' and pass=\'"+pass+"\'");
return rs.next();
}//try
catch(Exception e){
e.printStackTrace();
}
return false;
}
}
//3.Product.java
package com.rajendra.servlets;
public class Product implements java.io.Serializable {
String code, name;
double qty;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQty() {
return qty;
}
public void setQty(double qty) {
this.qty = qty;
}
public boolean equals(Object o){
if (o instanceof Product)
return ((Product)o).code.equals(code);
return false;
}
}
package com.rajendra.servlets;
public class Product implements java.io.Serializable {
String code, name;
double qty;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQty() {
return qty;
}
public void setQty(double qty) {
this.qty = qty;
}
public boolean equals(Object o){
if (o instanceof Product)
return ((Product)o).code.equals(code);
return false;
}
}
//4. ProductDAO.java
package com.rajendra.servlets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class ProductsDAO {
public Collection getProducts(){
try {
Connection con=DriverConnection.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from products");
/*
* Create the table prodcuts in test database
*/
ArrayList products=new ArrayList();
while (rs.next()){
Product p=new Product();
p.code=rs.getString(1);
p.name=rs.getString(2);
p.qty=rs.getDouble(3);
products.add(p);
}
return products;
}//try
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
package com.rajendra.servlets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class ProductsDAO {
public Collection getProducts(){
try {
Connection con=DriverConnection.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from products");
/*
* Create the table prodcuts in test database
*/
ArrayList products=new ArrayList();
while (rs.next()){
Product p=new Product();
p.code=rs.getString(1);
p.name=rs.getString(2);
p.qty=rs.getDouble(3);
products.add(p);
}
return products;
}//try
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
//5. LoginServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void init(){
ud=new UserDAO();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
ud=new UserDAO();
if (ud.validate(uname,pass)){
//start a session
HttpSession hs=request.getSession();
hs.setAttribute("UserName",uname);
RequestDispatcher rd=request.getRequestDispatcher("home");
rd.forward(request,response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
}
UserDAO ud;
}
package com.rajendra.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void init(){
ud=new UserDAO();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
ud=new UserDAO();
if (ud.validate(uname,pass)){
//start a session
HttpSession hs=request.getSession();
hs.setAttribute("UserName",uname);
RequestDispatcher rd=request.getRequestDispatcher("home");
rd.forward(request,response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
}
UserDAO ud;
}
//6. UserHomeServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserHomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><font size='6'>Welcome , "+uname+"</font></td>");
out.println("</tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserHomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><font size='6'>Welcome , "+uname+"</font></td>");
out.println("</tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
//7. GetProductServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class GetProductsServlet extends HttpServlet {
public void init() throws ServletException {
productsdao=new ProductsDAO();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>");
out.println("<form name='form1' method='post' action='"+response.encodeURL("addProducts")+"'>");
out.println("<table width='100%' border='1'>");
out.println("<tr> ");
out.println("<th width='8%'>Check</th>");
out.println("<th width='24%'>Product Code</th>");
out.println("<th width='28%'>Product Name</th>");
out.println("<th width='20%'>Available Quantity</th>");
out.println("<th width='20%'>Required Quantity</th>");
out.println("</tr>");
Collection products=productsdao.getProducts();
if (products==null){
out.println("<tr><td colspan=5 align=center>");
out.println("No Products Available");
out.println("</td></tr>");
}
else{
Iterator i=products.iterator();
while(i.hasNext()){
Product p=(Product)i.next();
out.println("<tr>");
out.println("<td align='center'>");
out.println("<input type='checkbox' name='products' value='"+p.getCode()+"'/>");
out.println("<input type='hidden' name='"+p.getCode()+"Name' value='"+p.getName()+"'/></td>");
out.println("<td>"+p.getCode()+"</td>");
out.println("<td>"+p.getName()+"</td>");
out.println("<td>"+p.getQty()+"</td>");
out.println("<td><input type='text' name='"+p.getCode()+"'/></td>");
out.println("</tr>");
}
}
out.println("</table>");
out.println("<br/><center>");
out.println("<input type=submit value='Add Products to Cart'/>");
out.println("</center>");
out.println("</form>");
out.println("</td></tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
ProductsDAO productsdao;
}
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class GetProductsServlet extends HttpServlet {
public void init() throws ServletException {
productsdao=new ProductsDAO();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>");
out.println("<form name='form1' method='post' action='"+response.encodeURL("addProducts")+"'>");
out.println("<table width='100%' border='1'>");
out.println("<tr> ");
out.println("<th width='8%'>Check</th>");
out.println("<th width='24%'>Product Code</th>");
out.println("<th width='28%'>Product Name</th>");
out.println("<th width='20%'>Available Quantity</th>");
out.println("<th width='20%'>Required Quantity</th>");
out.println("</tr>");
Collection products=productsdao.getProducts();
if (products==null){
out.println("<tr><td colspan=5 align=center>");
out.println("No Products Available");
out.println("</td></tr>");
}
else{
Iterator i=products.iterator();
while(i.hasNext()){
Product p=(Product)i.next();
out.println("<tr>");
out.println("<td align='center'>");
out.println("<input type='checkbox' name='products' value='"+p.getCode()+"'/>");
out.println("<input type='hidden' name='"+p.getCode()+"Name' value='"+p.getName()+"'/></td>");
out.println("<td>"+p.getCode()+"</td>");
out.println("<td>"+p.getName()+"</td>");
out.println("<td>"+p.getQty()+"</td>");
out.println("<td><input type='text' name='"+p.getCode()+"'/></td>");
out.println("</tr>");
}
}
out.println("</table>");
out.println("<br/><center>");
out.println("<input type=submit value='Add Products to Cart'/>");
out.println("</center>");
out.println("</form>");
out.println("</td></tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
ProductsDAO productsdao;
}
//8. AddProductServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddProductsServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
ArrayList products=(ArrayList)hs.getAttribute("products");
if (products==null){
products=new ArrayList();
hs.setAttribute("products",products);
}
String[] pcodes=request.getParameterValues("products");
for (int i=0;i<pcodes.length;i++){
if (request.getParameter(pcodes[i]).equals(""))
continue;
Product p=new Product();
p.code=pcodes[i];
int j=products.indexOf(p);
if (j!=-1){
p=(Product)products.get(j);
p.qty+=Double.parseDouble(request.getParameter(pcodes[i]));
}
else{
p.name=request.getParameter(pcodes[i]+"Name");
p.qty=Double.parseDouble(request.getParameter(pcodes[i]));
products.add(p);
}
}//for
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>" );
out.println("Products Added successfully to your cart</td>");
out.println("</tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddProductsServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
ArrayList products=(ArrayList)hs.getAttribute("products");
if (products==null){
products=new ArrayList();
hs.setAttribute("products",products);
}
String[] pcodes=request.getParameterValues("products");
for (int i=0;i<pcodes.length;i++){
if (request.getParameter(pcodes[i]).equals(""))
continue;
Product p=new Product();
p.code=pcodes[i];
int j=products.indexOf(p);
if (j!=-1){
p=(Product)products.get(j);
p.qty+=Double.parseDouble(request.getParameter(pcodes[i]));
}
else{
p.name=request.getParameter(pcodes[i]+"Name");
p.qty=Double.parseDouble(request.getParameter(pcodes[i]));
products.add(p);
}
}//for
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>" );
out.println("Products Added successfully to your cart</td>");
out.println("</tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
//9. GetCartDetailsServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class GetCartDetailsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>");
out.println("<center>Your cart contains the following products");
out.println("<table width='80%' border='1'>");
out.println("<tr>");
out.println("<th width='24%'>Product Code</th>");
out.println("<th width='28%'>Product Name</th>");
out.println("<th width='20%'>Quantity</th>");
out.println("</tr>");
Collection products=(Collection)hs.getAttribute("products");
if (products==null){
out.println("<tr><td colspan=3 align=center>");
out.println("You have not added any products into cart");
out.println("</td></tr>");
}
else {
Iterator i=products.iterator();
while(i.hasNext()){
Product p=(Product)i.next();
out.println("<tr>");
out.println("<td>"+p.getCode()+"</td>");
out.println("<td>"+p.getName()+"</td>");
out.println("<td>"+p.getQty()+"</td>");
out.println("</tr>");
}
}
out.println("</table></center>");
out.println("</td></tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class GetCartDetailsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs==null){
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
return;
}
String uname=(String)hs.getAttribute("UserName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html> <head>");
out.println("<title>Shopping Cart Example</title>");
out.println("</head> <body>");
out.println("<table width='100%' height='90%' border='1'>");
out.println("<tr align='center'> ");
out.println("<td height='39' colspan='2'><strong><font size='5'>MyShopping Site</font></strong></td>");
out.println("</tr>");
out.println("<tr> ");
out.println("<td width='18%' height='500' valign='top'><p> </p>");
out.println("<blockquote>");
out.println("<p><a href='"+response.encodeURL("getProducts")+"'>View Products</a></p>");
out.println("<p><a href='"+response.encodeURL("getCart")+"'>View Cart Details</a></p>");
out.println("<p><a href='"+response.encodeURL("logout")+"'>Logout</a></p>");
out.println("</blockquote></td>");
out.println("<td width='82%' align='left' valign='top'><p><font size='6'>Welcome , "+uname+"</font></p>");
out.println("<center>Your cart contains the following products");
out.println("<table width='80%' border='1'>");
out.println("<tr>");
out.println("<th width='24%'>Product Code</th>");
out.println("<th width='28%'>Product Name</th>");
out.println("<th width='20%'>Quantity</th>");
out.println("</tr>");
Collection products=(Collection)hs.getAttribute("products");
if (products==null){
out.println("<tr><td colspan=3 align=center>");
out.println("You have not added any products into cart");
out.println("</td></tr>");
}
else {
Iterator i=products.iterator();
while(i.hasNext()){
Product p=(Product)i.next();
out.println("<tr>");
out.println("<td>"+p.getCode()+"</td>");
out.println("<td>"+p.getName()+"</td>");
out.println("<td>"+p.getQty()+"</td>");
out.println("</tr>");
}
}
out.println("</table></center>");
out.println("</td></tr>");
out.println("<tr align='center'> ");
out.println("<td colspan='2'><em>©Copyrights 2005-09</em></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body> </html>");
out.flush();
out.close();
}
}
//10. LogoutServlet.java
package com.rajendra.servlets;import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession(false);
if (hs!=null)
hs.invalidate();
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
1. set the classpath
D:\\httpsess>set CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar ;.;
D:\\httpsess>set CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar ;.;
//if you are using Eclipse then use set the path by placing the mouse on project, right click, choose build path, configure build path, AddExternal Jar files (click), browse the files
D:\\cookiesex>javac -d . *.java
3. Now copy the class files
4. paste into classes folder of WEB-INF folder
(c) create web.xml file for configuration under WEB-INF folder
<web-app><servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UserHomeServlet</servlet-name>
<servlet-class>com.rajendra.servlets.UserHomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GetProductsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.GetProductsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AddProductsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.AddProductsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>GetCartDetailsServlet</servlet-name>
<servlet-class>com.rajendra.servlets.GetCartDetailsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserHomeServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetProductsServlet</servlet-name>
<url-pattern>/getProducts</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddProductsServlet</servlet-name>
<url-pattern>/addProducts</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetCartDetailsServlet</servlet-name>
<url-pattern>/getCart</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Login.html</welcome-file>
</welcome-file-list>
</web-app>
4. deploy your application(your folder ex" httpsess) in webapps folder under Tomcat server
steps for deploying your application
1. you can create war file also to deploy
preparing war file using cmd prompt
D:\\httpsess>jar -cvf httpsess.war Home.html WEB-INF
(6) start the tomcat server
(7) deploy the servlet(servlet folder or war file into the webapps folder of Tomcat
(or) paste the ur application (ex: httpsess.war) into the webapps folder of tomcat
(8)runing the servlet program
http://localhost:8888/httpsess