Exp 22

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

EXP 22

Practical No. 22: Write a Servlet program to send


username and password using HTML forms and
authenticate the user.
X. Program Code
1. HTML Code to Send Username and Password

loginpage.html

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>User Login</h2>
<form
action="http://localhost:8080/examples/servlets/servlet/AthonticationServlet
" method="POST">
User Name: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>

2. Java Code for Authentication Servlet


AthonticationServlet.java

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 X1 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 = request.getParameter("username");
String password = request.getParameter("password");

if (username.equals("abhishek") && password.equals(pass)) {


out.println("<h2>Login Successful</h2>");
out.println("<p>Length of Username: " + username.length() + "
</p>");
} else {
out.println("<h2>Login Unsuccessful</h2>");
}
out.close();
}
}

XIII. Exercise
1. Servlet Program to Retrieve Data from List and Radio Button
HTML Form for List and Radio Buttons (listRadio.html)

<!DOCTYPE html>
<html>
<head>
<title>Selection Form</title>
</head>
<body>
<h2>Select Your Preferences</h2>
<form
action="http://localhost:8080/examples/servlets/servlet/SelectionServlet"
method="POST">
<label for="color">Choose your favorite color:</label>
<select name="color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select><br><br>

<label>Select your gender:</label><br>


<input type="radio" name="gender" value="Male"> Male<br>
<input type="radio" name="gender" value="Female"> Female<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Servlet to Handle List and Radio Buttons (SelectionServlet.java)

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 XIII1 extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String color = request.getParameter("color");


String gender = request.getParameter("gender");

out.println("<h2>Your Preferences</h2>");
out.println("<p>Favorite Color: " + color + "</p>");
out.println("<p>Gender: " + gender + "</p>");

out.close();
}
}

2. Program to Receive Student Subject Marks and Determine


Pass/Fail

HTML Form for Marks Submission (marks.html)

<!DOCTYPE html>
<html>
<head>
<title>Student Marks Submission</title>
</head>
<body>
<h2>Submit Your Marks</h2>
<form
action="http://localhost:8080/examples/servlets/servlet/MarksServlet"
method="POST">
Enter Marks: <input type="number" name="marks" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Servlet to Handle Marks Submission (MarksServlet.java)

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 XIII2 extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

int marks = Integer.parseInt(request.getParameter("marks"));


if (marks >= 40) {
out.println("<h2>Status: Passed</h2>");
} else {
out.println("<h2>Status: Failed</h2>");
}

out.close();
}
}

Outputs:

You might also like