0% found this document useful (0 votes)
25 views7 pages

Ajp 22

Uploaded by

Zara Shaikh
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)
25 views7 pages

Ajp 22

Uploaded by

Zara Shaikh
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/ 7

X.

Program Code:
Q1.Write a Program to send the username to server and server will send the length of
username to client.
Java Code:
import java.io.*;
import javax.servlet.*;
public class exp22q1 extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();

String p1=req.getParameter("t1");
String p2=req.getParameter("t2");

if((p1.equals(""))||(p2.equals("")))
{
pw.println("<h1>Fill the Fields</h1>");
}
else
{
pw.println("<h1>Password = "+p2+"<br> Length of Password ="+p2.length()+"</h1>");
}
pw.close();
}
}

Html code:
<html>
<head>
</head>
<body>
<form action="exp22q1">
Name: <input type="text" name="t1">
Password: <input type="password" name="t2">
<input type="submit" value="Click Here to Get Password Length ">
<input type="reset" value="Reset">
</form>

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>exp22q1</servlet-name>
<servlet-class>exp22q1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exp22q1</servlet-name>
<url-pattern>/exp22q1</url-pattern>
</servlet-mapping>
</web-app>

Output:

Midhat
6

Q2) Write the output of following code cosidering below HTML is front end and servlet
as back end.
Java Code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class exp22q2 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String pass="abhishek12345";
String uname,password;
uname=request.getParameter("uname");
password=request.getParameter("password");
if(uname.equals(uname) && password.equals(pass))
{
out.println("Login Successfull");
}
else
{
out.println("Login Unsuccessfull");
}
}
}

Html code:
<html>
<body>
<form action="exp22q2" method="POST">
User Name:<input type="text" name="uname"><br>
Password:<input type="password" name="password" ><br>
<input type="submit">
</form>
</body>
</html>

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>exp22q2</servlet-name>
<servlet-class>exp22q2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exp22q2</servlet-name>
<url-pattern>/exp22q2</url-pattern>
</servlet-mapping>
</web-app>

Output:

EXERCISE:
Q1. Develop servlet program to retrieve data from List and Radio Button using HTML
Forms.
Java Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class exp22q3 extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
String value;
res.setContentType("text/html");
value = req.getParameter("dish");
PrintWriter pw = res.getWriter();
pw.println("<html><body bgcolor=green>");
pw.println("<h3>Your favorite dish is = <font color=yellow>" +value+"</font></h3>");
}
}
Html code:
<body>
<form method="get" action="exp22q3">
<h2>Select your favorite dish</h2>
<input type="radio" name="dish" value="Indian"> Indian<br>
<input type="radio" name="dish" value="Continental">Continental<br>
<input type="radio" name="dish" value="Chinese"> Chinese<br>
<input type="radio" name="dish" value="Italian"> Italian<br>
<input type="radio" name="dish" value="Russian"> Russian<br>
<br><br><br>

<input type="submit" value="Submit"></form>


</body>
</html>
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>exp22q3</servlet-name>
<servlet-class>exp22q3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exp22q3</servlet-name>
<url-pattern>/exp22q3</url-pattern>
</servlet-mapping>
</web-app>

Output:
Q4) Develop a program to receive student subject marks through HTML forms
TextField and send the response as passed or Failed in Examination...
Java Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class exp22q4 extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
String value;
res.setContentType("text/html");
value = req.getParameter("item");
PrintWriter pw = res.getWriter();
pw.println("<html><body bgcolor=green>");
pw.println("<h3>Hatim Mullajiwala-19410 <br> Your Have Selected = <font
color=yellow>" +value+"</font></h3>");
}
}

Html code:
<body>
<form method="get" action="exp22q4">
<h2>Select Any Item From the Dropdown List</h2>
<select name="item">
<option value="No Item Selected">No Item Selected</option>
<option value="item1">Item 1</option>
<option value="item2">Item 2</option>
<option value="item3">Item 3</option>
<option value="item4">Item 4</option>
</select>
<br><br><br>
<input type="submit" value="Submit"></form>
</body>
</html>

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>exp22q4</servlet-name>
<servlet-class>exp22q4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exp22q4</servlet-name>
<url-pattern>/exp22q4</url-pattern>
</servlet-mapping>
</web-app>

Output:

You might also like