Java Slip1 Program 2 | TYBcs | B.Sc. Computer Science - IProgramx

Q. Design an HTML page containing 4 option buttons (Painting, Drawing, Singing and swimming) and 2 buttons reset and submit. When the user clicks submit, the server responds by adding a cookie containing the selected hobby and sends a message back to the client. Program should not allow duplicate cookies to be written.



Program:

//html page
<html>
<body>
<form method=get action="http://localhost:8080/Slip1Prog2">
<br>
<input type=radio value=Painting name=r1>Painting<br>
<input type=radio value=Drawing name=r1>Drawing<br>
<input type=radio value=Singing name=r1>Singing<br>
<input type=radio value=Swimming name=r1>Swimming<br>
<br>
<input type=submit value=Submit>
<input type=reset value=Reset><br>
</form>
</body>
</html>
//java prog
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
class Slip1Prog2 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String s=req.getParameter("r1");
Cookie c[]=req.getCookies();
if(c!=null)
{
pw.println("Existing Cookies are<br>");
for(int i=0;i<c.length;i++)
{
String nm=c[i].getName();
String val=c[i].getValue();
pw.println("Name:"+nm+"<br>Value:"+val+"<br>");
}
for(int i=0;i<c.length;i++)
{
if(c[i].getName().equals("hobby"))
{
pw.println("<br>Cookie Is present");
return;
}
}
}
Cookie c1=new Cookie("hobby",s);
res.addCookie(c1);
pw.println("Cookie Created");
}
}

Post a Comment

0 Comments