0% found this document useful (0 votes)
24 views14 pages

Ajp Prac 22

Uploaded by

saeedarwatkar
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)
24 views14 pages

Ajp Prac 22

Uploaded by

saeedarwatkar
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/ 14

Practical No.

22
1) Write a program to send username and server will send the length of the
username to client.

i) Html file:
<!DOCTYPE html>

<html>

<body>

<form method="get" action="findlength">

Username:<input type="text" name="t1"><!-- comment -->

<br><br><!-- comment -->

<input type="submit" value="Check length">

</form>

</body>

</html>

ii) Servlet file:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.annotation.*;

@ WebServlet("/findlength")

public class findlength extends HttpServlet

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException

String u1=req.getParameter("t1");
int l=u1.length();

PrintWriter pw=res.getWriter();

pw.println("Length of the Username is:"+l);

pw.close();

Output:
2) Write a servlet program to retrieve data from list and radio button using
HTML forms.

i) Html file:
<!DOCTYPE html>

<html>

<head>

<title>Form with List and Radio Buttons</title>

</head>

<body>

<h2>Choose from below :</h2>

<form method="get" action="list_radio_Servlet">

<label for="fruit">Favorite Fruit:</label>

<select id="fruit" name="fruit">

<option value="apple">Apple</option>

<option value="banana">Banana</option>

<option value="orange">Orange</option>

<option value="grape">Grape</option>

</select>

<br><br>

<label>Payment Method:</label><br>

<input type="radio" name="payment" > Credit Card

<input type="radio" name="payment" > PayPal

<input type="radio" name="payment" value="Bank Transfer" > Bank Transfer

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

</form>

</body></html>
ii) Servlet file:

import java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/list_radio_Servlett")

public class list_radio_Servlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

String s1 = req.getParameter("fruit");

String s2 = req.getParameter("payment");

pw.println("<h2>Your Preferences:</h2>");

pw.println("<p><b>Favorite Fruit:</b> " + s1 + "</p>");

pw.println("<p><b>Preferred Payment Method:</b> " + s2 + "</p>");

pw.close();

}
iii) Web.xml
<?xml version="1.0" encoding="UTF-8"?>

<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_4_0.xsd"

version="4.0">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<servlet>

<servlet-name>list_radio_Servlet</servlet-name>

<servlet-class>list_radio_Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>list_radio_Servlet</servlet-name>

<url-pattern>/list_radio_Servlet</url-pattern>

</servlet-mapping>

</web-app>
Output :
3) Develop a program to receive a student subject marks through Html forms TextField
and send the response as passed or failed in Exam.

i) Html file:

<!DOCTYPE html>

<html>

<head>

<title>Student Marks Submission</title>

</head>

<body>

<h2>Enter Your Marks</h2>

<form method="post" action="MarksServlet">

<label for="subject1">Subject 1 Marks:</label>

<input type="number" id="subject1" name="subject1" required><br><br>

<label for="subject2">Subject 2 Marks:</label>

<input type="number" id="subject2" name="subject2" required><br><br>

<label for="subject3">Subject 3 Marks:</label>

<input type="number" id="subject3" name="subject3" required><br><br>

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

</form>

</body>

</html>
ii) Servlet File:

<!DOCTYPE html>

<html>

<head>

<title>Student Marks Submission</title>

</head>

<body>

<h2>Enter Your Marks</h2>

<form method="post" action="MarksServlet">

<label for="subject1">Subject 1 Marks:</label>

<input type="number" id="subject1" name="subject1" required><br><br>

<label for="subject2">Subject 2 Marks:</label>

<input type="number" id="subject2" name="subject2" required><br><br>

<label for="subject3">Subject 3 Marks:</label>

<input type="number" id="subject3" name="subject3" required><br><br>

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

</form>

</body>

</html>
iii) Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<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_4_0.xsd"

version="4.0">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<servlet>

<servlet-name>MarksServlet</servlet-name>

<servlet-class>MarksServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MarksServlet</servlet-name>

<url-pattern>/MarksServlet</url-pattern>

</servlet-mapping>

</web-app>
Output:
4) Write the output of the following code considering below HTML is front end and
servlet as back end.

i) Html file:

<!DOCTYPE html>

<html>

<head>

<title>Authentication checking</title>

</head>

<body>

<h2>Enter Your Credentials</h2>

<form method="post" action="AuthenticateServlet">

User Name: <input type="text" name="username" ><br><br>

Password : <input type="password" name="password" ><br><br>

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

</form>

</body>

</html>
ii) Servlet file:

import java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/AuthenticateServlet")

public class AuthenticateServlet extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

String username,password;

username=req.getParameter("username");

password=req.getParameter("password");

if(username.equals("Abhishek")&& password.equals("abhishek12345"))

pw.println("Login succesfully");

else

pw.println("Login Unsuccessful");

pw.close();

}
iii) Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<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_4_0.xsd"

version="4.0">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<servlet>

<servlet-name>AuthenticateServlet</servlet-name>

<servlet-class>AuthenticateServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>AuthenticateServlet</servlet-name>

<url-pattern>/AuthenticateServlet</url-pattern>

</servlet-mapping>

</web-app>
Output:

You might also like