0% found this document useful (0 votes)
13 views10 pages

Exp 24

AJP

Uploaded by

Amar Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Exp 24

AJP

Uploaded by

Amar Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

HWAIZA SIDDIQUI 19431 EXPERIMENT NO: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

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<display-name>Demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

Output:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24

Q2. Write the output of following servlet code.


Java1 code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
Cookie ck=new Cookie("uname",n);//creating cookie object
response.addCookie(ck);//adding cookie in the response
//creating submit button
out.print("<form action='http://localhost:8080/exp24q2/SecondServlet'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
out.close();
}
catch(Exception e){System.out.println(e);}
}
}

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

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<display-name>Demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24

<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();

String name = request.getParameter("userName");


String password = request.getParameter("userPassword");
pwriter.print("<h1>");
pwriter.print("Hello "+name);
pwriter.print("<br>Your Password is: "+password);

Cookie c1=new Cookie("userName",name);


HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24

Cookie c2=new Cookie("userPassword",password);

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

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<display-name>Demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<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:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24

Q2) Develop program to get the browser Information.


Java code:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class exp24q4 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");out.println("<head>");out.println("</head>");
out.println("<body><center>");out.println("<h1>");
out.println("Browser Details ");out.println("<br>");
out.println(" Character Encoding : " + request.getCharacterEncoding());
out.println("<br>");
out.println("Content Type : " + request.getContentType());
out.println("<br>");
out.println("Content Length: " + request.getContentLength());
out.println("<br>");
out.println("Protocol : " + request.getProtocol());
out.println("<br>");
out.println("Server Name : " + request.getServerName());
out.println("<br>");
out.println("Server Port : " + request.getServerPort());
out.println("<br>");
out.println("Remote Host : " + request.getRemoteHost());
out.println("<br>");
out.println("<br>");out.println("</h1>");
out.println("</center></body>"); out.println("</html>");
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

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>exp24q4</servlet-name>
<servlet-class>exp24q4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exp24q4</servlet-name>
<url-pattern>/exp24q4</url-pattern>
</servlet-mapping>
</web-app>

Output:
HWAIZA SIDDIQUI 19431 EXPERIMENT NO:24

You might also like