Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java

Java document. hshshshhshshshshs poker ey people should stop being mean and rude

Uploaded by

juberkhan47401
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java

Java document. hshshshhshshshshs poker ey people should stop being mean and rude

Uploaded by

juberkhan47401
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

TABLE OF CONTENT

Sr.n Practicals Signature


o
1 Create a servlet for a login page. If the username and
password are correct then it says message “Hello” else
a message “login failed”.
2 Create a registration servlet in Java using JDBC. Accept
the details such as Username, Password, Email, and
Country from the user using HTML Form and store the
registration details in the database.
3 Write a servlet code to store the biodata details of a
user into the database. (Name, age, address, hobbies,
gender, Qualification).
4 Write a Java Program using Servlet to authenticate
user’s username and password.
5 Develop servlet application for accepting user
registration details and displaying it on another page
(At least 6 Values).
6 Create a servlet that uses Cookies to store the number
of times a user has visited servlet.
7 Develop servlet application of basic calculator.
8 Develop servlet application to demonstrate the use of
session management.
9 Using Request Dispatcher Interface create a Servlet
which will validate the password entered by the user,
if the user has entered "Servlet" as password, then he
will be forwarded to Welcome Servlet else the user will
stay on the index.html page and an error message will
be displayed.
10 Create a servlet demonstrating the use of session
creation and destruction. Also count the number of
times the user visited the page.
11 Develop a web application using Java Servlets, to
provide a set of question [3 Questions minimum] to
the user. Each question should have four options, from
which the user should be able to selection only one.
When the user clicks on button “Submit”, call a servlet
to compute and display the score of the user [number

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 1


of correct and incorrect answers].
12 Developed a servlet application to demonstate simple
validation. And If user left any filed empty [Username
or Password] then user will stay on the index.html
page and an error message will be displayed.
13 Create a servlet which authenticate with Database .

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 2


PRACTICAL No : 1
Aim: Create a servlet for a login page. If the username and password are
correct then it says message “Hello” else a message “login failed”.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet1">
Enter Username: <input type="text" name="u1">
<br><br>
Enter Password: <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet1.java
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;

@WebServlet(urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 3


{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (uname.equals("Tyit") && pass.equals("admin"))


{
out.println("<h1><center>Hello "+ uname +"</center></h1>");
}
else
{
out.println("<h1><center>Login Fails</center></h1>");
}
}
}
}

Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 4


PRACTICAL No : 2
Aim: Create a registration servlet in Java using JDBC. Accept the details such
as Username, Password, Email, and Country from the user using HTML Form
and store the registration details in the database.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration</h1>
<form method="Post" action="Servlet2">
Enter UserName: <input type="text" name="u1">
<br><br>
Enter Password: <input type="password" name="p1">
<br><br>
Enter Email: <input type="text" name="e1">
<br><br>
Enter Country: <input type="text" name="c1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet2.java
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 java.sql.*;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 5


@WebServlet(urlPatterns = {"/Servlet2"})
public class Servlet2 extends HttpServlet
{

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
String email=request.getParameter("e1");
String country=request.getParameter("c1");

try
{
Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENT VALUES
(?,?,?,?)");

ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,email);
ps.setString(4,country);

ps.executeUpdate();

out.println("<center><h1>Registration Successful</h1></center>");
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 6


Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 7


PRACTICAL No : 3
Aim: Write a servlet code to store the biodata details of a user into the
database. (Name, age, address, hobbies, gender, Qualification).
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>BioData</h1>
<form method="Post" action="Servlet3">
Enter Name: <input type="text" name="n1">
<br><br>
Enter Age: <input type="text" name="age">
<br><br>
Enter Address: <input type="text" name="a1">
<br><br>
Enter Hobbies: <input type="text" name="h1">
<br><br>
Select Gender:
<input type="radio" name="r1" value="Male">Male
<input type="radio" name="r1" value="Female">Female
<br><br>
Enter Qualification: <input type="text" name="q1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet3.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 8


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

import java.sql.*;

@WebServlet(urlPatterns = {"/Servlet3"})
public class Servlet3 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("age"));
String address=request.getParameter("a1");
String hobbies=request.getParameter("h1");
String gender=request.getParameter("r1");
String qual=request.getParameter("q1");

try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO Emp VALUES
(?,?,?,?,?,?)");

ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,address);
ps.setString(4,hobbies);
ps.setString(5,gender);
ps.setString(6,qual);

ps.executeUpdate();

out.println("<center><h2>Data Is Successfully Stored</h2></center>");


}
catch(SQLException e)
{
System.out.println(e);
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 9


}
}
}

Output:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 10


PRACTICAL No : 4
Aim: Write a Java Program using Servlet to authenticate user’s username and
password.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 4</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet4">
Enter Username: <input type="text" name="u1">
<br><br>
Enter Password: <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet4.java
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;

@WebServlet(urlPatterns = {"/Servlet4"})
public class Servlet4 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 11


try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (uname.equals("Tyit") && pass.equals("admin"))


{
out.println("<h1><center>Hello "+ uname +"</center></h1>");
}
else
{
out.println("<h1><center>Login Fails</center></h1>");
}
}
}
}

Output:

If user is Valid:

If user is Invalid:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 12


PRACTICAL No : 5
Aim: Develop servlet application for accepting user registration details and
displaying it on another page (At least 6 Values).
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 5</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Registration</h1>
<form method="Post" action="Servlet5">
Enter Name: <input type="text" name="n1">
<br><br>
Enter Age: <input type="text" name="age">
<br><br>
Enter Address: <input type="text" name="a1">
<br><br>
Enter Hobbies: <input type="text" name="h1">
<br><br>
Select Gender:
<input type="radio" name="r1" value="Male">Male
<input type="radio" name="r1" value="Female">Female
<br><br>
Enter Qualification: <input type="text" name="q1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet5.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 13


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Servlet5"})
public class Servlet5 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("age"));
String address=request.getParameter("a1");
String hobbies=request.getParameter("h1");
String gender=request.getParameter("r1");
String qual=request.getParameter("q1");

out.println("<center><h1>Details</h1>");
out.println("<h2>Name is : " + name);
out.println("<br>Age is : " + age);
out.println("<br>Address is : " + address);
out.println("<br>Hobbies is : " + hobbies);
out.println("<br>Gender is : " + gender);
out.println("<br>Qualification is : " + qual);
out.println("</h2></center>");

}
}
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 14


Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 15


PRACTICAL No : 6
Aim: Create a servlet that uses Cookies to store the number of times a user
has visited servlet.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 6</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="Post" action="Servlet6">
UserName <input type="text" name="u1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet6.java
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 javax.servlet.http.Cookie;

@WebServlet(urlPatterns = {"/Servlet6"})
public class Servlet6 extends HttpServlet
{
static int i=1;

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 16


response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
Cookie c=new Cookie("Visit",String.valueOf(i));
response.addCookie(c);
int j=Integer.parseInt(c.getValue());

if (j==1)
{
out.println("<center><h1>Welcome "+ uname + "</h1></center>");
}

else
{
out.println("<center><h1>You have Visited "+j+" Times </h1></center>" );
}
i++;
}
}
}

Output:

When you visited first time:

When you visited again and again:

PRACTICAL No : 7

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 17


Aim: Develop servlet application of basic calculator.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 7</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Basic Calculater</h1>
<form method="Post" action="Servlet7">
Enter First Number <input type="text" name="n1">
<br><br>
Enter Second Number <input type="text" name="n2">
<br><br>
Select Operator
<select name="op">
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
<option value="multi">Multiplication</option>
<option value="div">Division</option>
</select>
<br><br>
<input type="submit" value="Answer">
</form>
</center>
</body>
</html>

Servlet7.java
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 javax.servlet.RequestDispatcher;

@WebServlet(urlPatterns = {"/Servlet7"})

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 18


public class Servlet7 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
int num1=Integer.parseInt(request.getParameter("n1"));
int num2=Integer.parseInt(request.getParameter("n2"));

String operation=request.getParameter("op");

RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);

switch(operation)
{
case "add":
{
out.println("<center><h2>Addition is : "+ (num1+num2) +"</h2></center>");
break;
}
case "sub":
{
out.println("<center><h2>Subtraction is : "+ (num1-num2) +"</h2></center>");
break;
}
case "multi":
{
out.println("<center><h2>Multiplication is : "+ (num1*num2)
+"</h2></center>");
break;
}
case "div":
{
out.println("<center><h2>Division is : "+ (num1/num2) +"</h2></center>");
break;
}

default:
{
out.println("<center><h2>Invalid Inputs</h2></center>");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 19


break;
}
}
}
}
}

Output:

After performing Multiplication:

PRACTICAL No : 8

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 20


Aim: Develop servlet application to demonstrate the use of session
management.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 8</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="Servlet8">
UserName <input type="text" name="u1">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Servlet8.java
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 javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Servlet8"})
public class Servlet8 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String n=request.getParameter("u1");

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 21


out.println("Hello "+ n);

HttpSession session=request.getSession();
session.setAttribute("user",n);

out.println("<form method='Post' action='S8'><br><br>");


out.println("<input type='submit' value='Go to Next'>");
out.println("</form>");

}
}
}

S8.java
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 javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/S8"})
public class S8 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
HttpSession session=request.getSession(false);

String s=(String)session.getAttribute("user");
out.println("Welcome "+ s);
}
}
}
Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 22


Servlet1:

Servlet2:

PRACTICAL No : 9

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 23


Aim: Using Request Dispatcher Interface create a Servlet which will validate
the password entered by the user, if the user has entered "Servlet" as
password, then he will be forwarded to Welcome Servlet else the user will
stay on the index.html page and an error message will be displayed.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 9</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet9">
UserName <input type="text" name="u1">
<br><br>
Password <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 24


Servlet9.java
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 javax.servlet.RequestDispatcher;

@WebServlet(urlPatterns = {"/Servlet9"})
public class Servlet9 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (pass.equals("servlet"))
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}

else
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Invalid user</h2></center>");
}
}
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 25


If user is Valid:

If user is Invalid:

PRACTICAL No : 10

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 26


Aim: Create a servlet demonstrating the use of session creation and
destruction. Also count the number of times the user visited the page.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 10</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet10">
UserName <input type="text" name="u1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet10.java
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 javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Servlet10"})
public class Servlet10 extends HttpServlet
{
static int i=1;

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 27


response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String n=request.getParameter("u1");
HttpSession session=request.getSession(true);

out.println("<center><h2>Hello "+ n);


out.println("<br><br>You have Visited "+i+" Times</h2></center>" );

session.invalidate();
i++;
}
}
}

Output:

PRACTICAL No : 11

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 28


Aim: Develop a web application using Java Servlets, to provide a set of
question [3 Questions minimum] to the user. Each question should have four
options, from which the user should be able to selection only one. When the
user clicks on button “Submit”, call a servlet to compute and display the
score of the user [number of correct and incorrect answers].
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 11</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="Post" action="Servlet11">
<h4>Welcome to MCQ Quiz Game </h4>

1. Who invented Java Programming?


<br>
<input type="radio" name="r1" value="a">A) Steve Jobs
<br>
<input type="radio" name="r1" value="b">B) James Gosling
<br>
<input type="radio" name="r1" value="c">C) Dennis Ritchie
<br>
<input type="radio" name="r1" value="d">D) Guido van Rossum
<br><br>

2. JDK stand for________?


<br>
<input type="radio" name="r2" value="a">A) Java development kit;
<br>
<input type="radio" name="r2" value="b">B) Java deployment kit;
<br>
<input type="radio" name="r2" value="c">C) JavaScript deployment kit;
<br>
<input type="radio" name="r2" value="d">D) None of these;
<br><br>

3. Which component is used to compile, debug and execute the java programs?
<br>
<input type="radio" name="r3" value="a">A) JRE

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 29


<br>
<input type="radio" name="r3" value="b">B) JIT
<br>
<input type="radio" name="r3" value="c">C) JDK
<br>
<input type="radio" name="r3" value="d">D) JVM
<br><br>

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


</form>
</body>
</html>

Servlet11.java
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;

@WebServlet(urlPatterns = {"/Servlet11"})
public class Servlet11 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String q1=request.getParameter("r1");
String q2=request.getParameter("r2");
String q3=request.getParameter("r3");

int score=0;

if (q1 != null && q1.equals("b"))


{
score++;
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 30


if (q2 != null && q2.equals("a"))
{
score++;
}

if (q3 != null && q3.equals("c"))


{
score++;
}

out.println("<center><h2>Correct Answer is : " + score +"</br>");


out.println("Incorrect Answer is : " + (3-score)+ "</center></h2>");

}
}
}

Output:

PRACTICAL No : 12
Aim: Developed a servlet application to demonstate simple validation. And If
user left any filed empty [Username or Password] then user will stay on the
index.html page and an error message will be displayed.

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 31


Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 12</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet12">
UserName <input type="text" name="u1">
<br><br>
Password <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>

Servlet12.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 32


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

@WebServlet(urlPatterns = {"/S12"})
public class S12 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");

if (uname.equals("") && pass.equals(""))


{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Username and Password</h2></center>");
}

else if (uname.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Username</h2></center>");
}

else if (pass.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Password</h2></center>");
}

else
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 33


}
}
}

Output:

If Username or Password is Empty:

PRACTICAL No : 13
Aim: Create a servlet which authenticate with Database .
Index.html
<!DOCTYPE html>
<html>

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 34


<head>
<title>Practical 13</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Login</h2>
<form method="Post" action="Servlet13">
UserName <input type="text" name="u1">
<br><br>
Password <input type="password" name="p1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Servlet13.java
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 java.sql.*;

@WebServlet(urlPatterns = {"/Servlet13"})
public class Servlet13 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
try
{

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 35


Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT * FROM STUDENT WHERE
USERNAME= ? and PASSWORD= ?");

ps.setString(1,uname);
ps.setString(2,pass);

ResultSet rs= ps.executeQuery();

if (rs.next())
{
out.println("<center><h2>Valid User</h2></center>");
}

else
{
out.println("<center><h2>Invalid User</h2></center>");
}
rs.close();
ps.close()
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}

Output:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 36


If user is Valid:

If user is Invalid:

Database:

TYBSc(Information Technology) :- Belekar Harsh Anil pg. 37

You might also like