0% found this document useful (0 votes)
35 views17 pages

AJP - PR22 - 68 - B - Abhishek Hagawane

The document describes a servlet program that authenticates users by sending username and password from an HTML form to the server. The servlet program receives the username and password as parameters, checks if the password matches expected, and returns "Login Successful" or "Login Unsuccessful". It also includes code for a program that receives student subject marks, calculates the average, and returns "Passed" if the average is more than 35 or "Failed" otherwise.
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)
35 views17 pages

AJP - PR22 - 68 - B - Abhishek Hagawane

The document describes a servlet program that authenticates users by sending username and password from an HTML form to the server. The servlet program receives the username and password as parameters, checks if the password matches expected, and returns "Login Successful" or "Login Unsuccessful". It also includes code for a program that receives student subject marks, calculates the average, and returns "Passed" if the average is more than 35 or "Failed" otherwise.
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/ 17

Practical No.

22

Date of Performance: 17th Dec 2021

Date of Submission: 17th Dec 2021

Name: Abhishek Hagawane

Roll No & Div: 68 B

TITLE: Write a Servlet program to send username and password using HTML
forms and authenticate the user.

COURSE OUTCOME: Develop programs using Servlet.

PRACTICAL OUTCOME: Write a program to demonstrate the use of


HttpServlet as a parameterized servlet
Program Code

1. Write a Program to send the username to server and server will send

the length of username to client Code:

Client html code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="add">
Enter Username : <input type="text" name="num1"><br>
Enter Password : <input type="text" name="num2"><br>
<input type="submit">
</form>

</body>
</html>
Server class code:

package com.Practical22;

import java.io.IOException; import


java.io.PrintWriter;

import javax.servlet.http.HttpServlet; import


javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;

public class usernameLength extends HttpServlet{ public void


service(HttpServletRequest req,HttpServletResponse res) throws
IOException{

{
String i=req.getParameter("num1");
String j=req.getParameter("num2");

PrintWriter out=res.getWriter();

res.getWriter().println("Length of username is :
"+i.length()+"\nLength of password is : "+j.length());

}
}
}
Output:
2. Write the output of following code cosidering below HTML is front

end and servlet as back end Code:

Client html code:

<html>
<body>
<form action="find" method="POST">
User Name:<input type="text" name="username"><br>
Password:<input type="password" name="password" ><br>
<input type="submit">
</form>
</body> </html>

Server class code:

package com.Practical22;
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 AthonticationServlet 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
username,password;
password=request.getParameter("password");
if(password.equals(pass))

{
out.println("Login Successfull");
} else

{
out.println("Login Unsuccessfull");
}
}
}
Output:

Entering correct password:


Entering incorrect password:
Exercise

1. Develop a program to receive student subject marks through


HTML forms TextField and send the response as passed or Failed in
Examination...

Code:

Client html code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Result</title>
</head>
<body>
<form method="post" action="result">
<h1> Enter Marks:</h1>
<h2>AJP : <input type="text" name="num1"/> </h2>
<h2> OS : <input type="text" name="num2"/> </h2>
<h2> ST : <input type="text" name="num3"/> </h2> <h2>
JS : <input type="text" name="num4"/> </h2>
<input type="submit"> <br/>
</form>
</body>
</html>

Server class code:

//1. Develop a program to receive student subject marks through HTML


forms TextField and send the response as passed or Failed in Examination..

package com.Practical22;

import java.io.IOException; import


java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;
import org.eclipse.jdt.internal.compiler.batch.Main;

public class Checkpass extends HttpServlet{ public void


doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); int
num1,num2,num3,num4; num1 =
Integer.parseInt(request.getParameter("num1")); num2 =
Integer.parseInt(request.getParameter("num2")); num3 =
Integer.parseInt(request.getParameter("num3")); num4 =
Integer.parseInt(request.getParameter("num4"));
if(num1>35 && num2>35 && num3>35 && num4>35)

{
float a=(num1+num2+num3+num4)/4;
out.println("<h1>Congratulations!!! You are Passed with "+a+"%
marks</h1>");

}
else
{
out.println("<h1> You are Failed!!!</h1>");
}
}
}
Output:
Entering less than 35 marks in one subject

You might also like