Sei sulla pagina 1di 5

DEVELOPMENT OF ON-LINE SHOPPING CART APPLICATION USING SERVLET

AIM:
To write the java code to develop an on-line shopping Cart application using servlet.

PROCEDURE:
1. Create a directory structure under Tomcat for your application.
2. Write the servlet source code. You need to import the javax.servlet package and the
javax.servlet.http package in your source file.
3. Compile your source code.
4. Create a deployment descriptor.
5. Run Tomcat.

SOURCE CODE:
LoginForm.html:
<html>
<head>
<body>
<div align="center">
<br> <br>
<form action="http://localhost:8080/cseapps/LoginServlet" method="post">
enter username:
<input type="text" value=""name="User">
<br>Enter Password:
<input type="text" value=""name="Password">
<br> enter card ID:
<input type="text" value="" name="CardID">
<br><br><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
LoginServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet


{
protected void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String usr=req.getParameter("User");
String pwd=req.getParameter("Password");
String card=req.getParameter("CardID");
boolean flag=true;
String[] userID=getInitParameter("usernames").split(",");
String[] password=getInitParameter("passwords").split(",");
String[] cardids=getInitParameter("cardIDs").split(",");
int i;
for(i=0;i<userID.length;i++)
{
if(userID[i].equals(usr)&&password[i].equals(pwd)&&cardids[i].equals(card))
{
flag=false;
Cookie MyCookie= new Cookie("CurrentUser",usr);
MyCookie.setMaxAge(60*60);
res.addCookie(MyCookie);
res.sendRedirect("http://localhost:8080/cseapps/LoginSuccess");
}}
if(flag==true)
{
out.println("<h4>invalid user,Please try again by clicking following link</h4>");
out.println("<a href='http://localhost:8080/LoginForm.html'>"+"LoginForm.html");
}
}
}

LoginSuccess.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginSuccess extends HttpServlet
{
protected void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
Cookie[]my_cookies=req.getCookies();
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<b>");
String userName=null;
if(my_cookies!=null)
{
for(Cookie cookie:my_cookies)
{
if(cookie.getName().equals("CurrentUser"))
userName=cookie.getValue();
}
}
out.print("<h3>LoginSuccess!!!Welcome</h3>");
out.print("<h2>This is a Shopping cart for"+userName+"</h2>");
out.close();
}
}

OUTPUT:
RESULT:
Thus the development of on-line shopping Cart application using servlet is written,
entered and output was executed successfully and

Potrebbero piacerti anche