Exp 24
Exp 24
PROGRAM CODE:
Q1) Write a Program to Create Cookie.
Java code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();
String name = request.getParameter("userName");
String password = request.getParameter("userPassword");
pwriter.print("<h1>");
pwriter.print("Hello, <br>Cookie 1 = "+name);
pwriter.print("<br>Cookie 2 =: "+password);
Cookie c1=new Cookie("userName",name);
Cookie c2=new Cookie("userPassword",password);
response.addCookie(c1);
response.addCookie(c2);
pwriter.print("<h1>Cookies are Set using addCookie() method</h1>");
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
}
}
}
Html code:
<body><h1><center>Program to set Cookies
<form action="login">
<br> User Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPassword"/><br/>
<input type="submit" value="submit"/>
</form></center>
</body>
Xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Output:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24
Java code2:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}
catch(Exception e){System.out.println(e);}
}
}
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24
Html code:
<body><h1><center>Program to set Cookies
<form action="http://localhost:8080/exp24q2/FirstServlet" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
</body>
Xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/SecondServlet</url-pattern>
</servlet-mapping>
</web-app>
Output:
EXERCISE:
Q1) Develop a program to collect user information using cookie.
Java1 code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();
response.addCookie(c1);
response.addCookie(c2);
pwriter.print("<br><a href='SecondServlet'>View Details</a>");
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
}
}
}
Java2 code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();
Cookie c[]=request.getCookies();
pwriter.print("<h1>Cookie Value 1:</h1> "+c[0].getValue());
pwriter.print("<h1>Cookie Value 2: </h1>"+c[1].getValue());
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
}
}
}
Html code:
<body><h1><center>Program to collect user information Using Cookies
<form action="FirstServlet">
<br> User Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPassword"/><br/>
<input type="submit" value="submit"/>
</form></center>
</body>
Xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Output:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24
out.close();
}
}
Xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Output:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24