0% found this document useful (0 votes)
4 views45 pages

Java Servlet Applications for Login and Calculator

Uploaded by

salunkhehome105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views45 pages

Java Servlet Applications for Login and Calculator

Uploaded by

salunkhehome105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical 1 : Implement the following Simple Servlet applications.

A) Aim : Create a simple calculator application using servlet.

[Link]

<html>

<head>

<title>Calculator</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

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

<br><h1>------------Calculator------------</h1><br>

Enter First Number <input type="text" name="t1"><br>

Enter Second Number <input type="text" name="t2"><br>

Select an Operation

<input type="radio" name="opr" value="+">ADDITION

<input type="radio" name="opr" value="-">SUBTRACTION

<input type="radio" name="opr" value="*">MULTIPLY

<input type="radio" name="opr" value="/">DIVIDE <br>

<input type="reset">

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

</form>

</body>

</html>

[Link]

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

public class CalculatorServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet CalculatorServlet</title>");

[Link]("</head>");

[Link]("<body>");

double n1 = [Link]([Link]("t1"));

double n2 = [Link]([Link]("t2"));

double result =0;

String opr=[Link]("opr");

if([Link]("+"))

result=n1+n2;

if([Link]("-"))

result=n1-n2;

if([Link]("*"))

result=n1*n2;

if([Link]("/"))

result=n1/n2;

[Link]("<h1> Result = " +result);

[Link]("</body>");

[Link]("</html>");

}
}

Output
B) 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”

[Link]

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="LoginServlet" method="get">
<p>1B. Create a servlet for a login page. If the username and password are
correct then it says
message “Hello username” else a message “login failed” </p><br>
UserName : <input type="text" name="t1"><br>
Password : <input type="password" name="t2"><br>
<h6>username= abhi<br>password= 1234</h6><br>
<input type="reset">
<input type="Submit">
</form>
</body>
</html>

[Link]

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class LoginServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet LoginServlet</title>");
[Link]("</head>");
[Link]("<body>");
String uname = [Link]("t1");
String pass = [Link]("t2");
if([Link]("abhi") && [Link]("1234"))
{
[Link]("<h1>Hello " +uname);
}
else
{
[Link]("Login Failed");
}
[Link]("</body>");
[Link]("</html>");
}
}

[Link]
class method
{
}

Output
Practical 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.

[Link] -

<!DOCTYPE html>

<html>

<head>

<title>Registration form</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="RegServlet">

<h1>Welcome to Registration Page</h1>

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

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

Enter Email id: <input type="text" name="e1"><br>

Enter Country Name: <input type="text" name="c1"><br>

<input type="submit" value="Register"> <input type="reset">

</form>

</body>

</html>

[Link] -

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

public class RegServlet extends HttpServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet RegServlet</title>");

[Link]("</head>");

[Link]("<body>");

String id = [Link]("t1");

String ps = [Link]("p1");

String em = [Link]("e1");

String co = [Link]("c1");

try

[Link]("[Link]").newInstance();

Connection con=(Connection)
[Link]("jdbc:mysql://localhost:3306/db10?
characterEncoding=latin1","root","root");

PreparedStatement pst = (PreparedStatement) [Link]("insert into


mytab values (?,?,?,?)");

[Link](1,id);
[Link](2,ps);

[Link](3,em);

[Link](4,co);

int row = [Link]();

[Link]("<h1>"+row+ " Inserted Successfullyy..");

catch(Exception e)

[Link](e);

[Link]("</body>");

[Link]("</html>");

Output:
Practical 3 : 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 [Link] page and an error
message will be displayed.

[Link]

<html>
<head>
<title>Login Page Information</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="validate" method="post">
<br>
<h1>------Login-------</h1><br>
Name: <input type="text" name="txt1"></br>
Password: <input type="password" name="txt2"><h6>username=
abhi<br>password= servlet</h6></br>
<input type="reset" value="reset">
<input type="submit" value="Submit">
</form>
</body>
</html>

[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class validate extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {


[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head><title>Servlet validate</title></head>");
[Link]("<body>");
String pass = [Link]("txt2");
if ([Link]("servlet")) {
RequestDispatcher rd = [Link]("welcome");
[Link](request, response);
} else {
[Link]("<font color='red'><b> You Have Entered Wrong
Password</b></font><br>");
RequestDispatcher rd = [Link]("[Link]");
[Link](request, response);
}
[Link]("</body>");
[Link]("</html>");
}
}
}

[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class welcome extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet welcome</title>");
[Link]("</head>");
[Link]("<body>");
String name = [Link]("txt1");
[Link]("<h1>Welcome >>>> "+name+"</h1>");
[Link]("</body>");
[Link]("</html>");
}
}
}

Output
Practical 4 A) Aim : Create a servlet that uses Cookies to store the number of
times a user has visited servlet.

[Link]
<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="Page1">

Enter your name: <input type="text" name="txtName"><br>

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

</form>

</body>

</html>

[Link]

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Page1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");
[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page1</title>");

[Link]("</head>");

[Link]("<body bgcolor=yellow>");

String uname = [Link]("txtName");

[Link]("<h1> Welcome "+uname+"</h1>");

Cookie ck1 = new Cookie("Username",uname);

Cookie ck2 = new Cookie("Visit","1");

[Link](ck1);

[Link](ck2);

[Link]("<h1> <a href=Page2>Visit Page2</a></h1>");

[Link]("</body>");

[Link]("</html>");

[Link]

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Page1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");
[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page1</title>");

[Link]("</head>");

[Link]("<body bgcolor=yellow>");

String uname = [Link]("txtName");

[Link]("<h1> Welcome "+uname+"</h1>");

Cookie ck1 = new Cookie("Username",uname);

Cookie ck2 = new Cookie("Visit","1");

[Link](ck1);

[Link](ck2);

[Link]("<h1> <a href=Page2>Visit Page2</a></h1>");

[Link]("</body>");

[Link]("</html>");

[Link]

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Page3 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");
[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page3</title>");

[Link]("</head>");

[Link]("<body bgcolor=lime>");

Cookie [] ck = [Link]();

for(int i=0;i<[Link];i++)

if(ck[i].getName().equals("Visit"))

int count = [Link](ck[i].getValue())+1;

[Link]("<h1>Visit No: "+count+"</h1>");

ck[i] = new Cookie("Visit",count+"");

[Link](ck[i]);

else

[Link](ck[i].getName()+" = "+ck[i].getValue());

[Link]("<h1><a href=Page2>Visit Page2</a></h1>");

[Link]("<h1><a href=Page4>Visit Page4</a></h1>");

[Link]("</body>");

[Link]("</html>");

[Link]

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

public class Page4 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page4</title>");

[Link]("</head>");

[Link]("<body bgcolor=pink>");

Cookie [] ck = [Link]();

for(int i=0;i<[Link];i++)

if(ck[i].getName().equals("Visit"))

int count = [Link](ck[i].getValue())+1;

[Link]("<h1>Visit No: "+count+"</h1>");

ck[i] = new Cookie("Visit",count+"");

[Link](ck[i]);

else

[Link](ck[i].getName()+" = "+ck[i].getValue());

[Link]("<h1><a href=Page2>Visit Page2</a></h1>");

[Link]("<h1><a href=Page3>Visit Page3</a></h1>");


[Link]("</body>");

[Link]("</html>");

Output
B)_ Aim : Create a servlet demonstrating the use of session creation and destruction.
Also check whether the user has visited this page first time or has visited earlier also using
sessions.

[Link]
<html>
<head>
<title>Session Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Page1" method="get">
Enter User ID <input type="text" name="t1"><br>
<input type="reset"><input type="submit">
</form>

</body>
</html>

[Link]
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class LogoutServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet LogoutServlet</title>");

[Link]("</head>");

[Link]("<body>");

[Link] hs = [Link]();

if(hs!=null)

[Link]();
[Link]("<h1>You are Loggged out now....</h1>");

[Link]("</body>");

[Link]("</html>");

[Link]

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Page1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page1</title>");

[Link]("</head>");

[Link]("<body>");

HttpSession hs = [Link](true);

if([Link]())

{
[Link]("<body bgcolor = yellow>");

String name = [Link]("t1");

[Link]("uname",name);

[Link]("visit", "1");

[Link]("<h1>Welcome First Time </h1>");

else

[Link]("<h1>Welcome Again</h1>");

int visit = [Link]((String)

[Link]("visit"))+1;

[Link]("<h1> You Visited"+visit+"Times</h1>");

[Link]("visit","" +visit);

[Link]("<h1>Your Session ID" +[Link]()+"</h1>");

[Link]("<h1> You Logged in at"+new


[Link]([Link]())+"</h1>");

[Link]("<h1><a href=Page2> Click For Page 2 </a></h1>");

[Link]("<h1><a href=LogoutServlet>Click to Terminate Session</a></h1>");

[Link]("</body>");

[Link]("</html>");

[Link]

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

public class Page2 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

try (PrintWriter out = [Link]()) {

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet Page2</title>");

[Link]("</head>");

[Link]("<body>");

HttpSession hs = [Link](false);

[Link]("<h1>welcome again on page no. 2</h1>");

int visit = [Link]((String)

[Link]("visit"))+1;

[Link]("<h1> You Visited"+visit+"Times</h1>");

[Link]("visit","" +visit);

[Link]("<h1>Your Session ID" +[Link]()+"</h1>");

[Link]("<h1> You Logged in at"+new


[Link]([Link]())+"</h1>");

[Link]("<h1><a href=Page1> Click For Page 1 </a></h1>");

[Link]("<h1><a href=LogoutServlet>Click to Terminate Session</a></h1>");

[Link]("</body>");

[Link]("</html>");

}
}

Output
Practical 5 : Implement the Servlet IO and File applications.

Aim : Create a Servlet application to upload and download a file.

upload

[Link]
<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="fileuploadservlet" method="post" enctype="multipart/form-data">

select File to upload:<input type="file" name="file"></br>

Destination<input type="text" value="/tmp" name="destination"></br>

<input type="submit" value="upload file" name="upload" id="upload">

</form>

</body>

</html>

[Link]
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

@MultipartConfig

public class fileuploadservlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet fileuploadservlet</title>");

[Link]("</head>");

[Link]("<body>");

String path=[Link]("destination");

Part filePart= [Link]("file");

String filename=[Link]();

[Link]("<br><br><br> File name: "+filename);

OutputStream os = null;

InputStream is = null;

try

os=new FileOutputStream(new File(path+[Link]+filename));

is=[Link]();

int read=0;

while((read=[Link]())!=-1)

[Link](read);

[Link]("<br> File uploaded Successfully!!!");

catch(FileNotFoundException e)

[Link](e);

[Link]("</body>");
[Link]("</html>");

Output

Download

[Link]

<html>

<head>

<title>TODO supply a title</title>


<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<h1>File Download Application</h1>

Click <a href="DownloadServlet?filename=Climate [Link]">CLimate


Change</a>

<br/><br/>

Click <a href="DownloadServlet?filename=New DOC [Link]">New Doc</a>

</body>

</html>

[Link]
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class DownloadServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

String filename = [Link]("filename");

int len=0;

try(ServletOutputStream out = [Link]())

{
ServletContext context =getServletConfig().getServletContext();
[Link](([Link](filename)!=null)?
[Link](filename):"application/pdf");

[Link]("COntent-Disposition","attachment; filename= filename=\""+ filename + "/");

InputStream is = [Link]("/ "+ filename);

byte[] b= new byte[1024];

while((is!=null)&&((len=[Link](b))!= -1))

[Link](b,0,len);

[Link]();

[Link]();

[Link]();

Output

Aim : Develop Simple Servlet Question Answer Application using Database.


[Link] –

<!DOCTYPE html>

<html>

<head>

<title>TODO supply a title</title>


<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

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

<label>Number of Primitive data types in Java are ?</label><br>

<input type ="radio" name="q1" value="a">8<br>

<input type ="radio" name="q1" value="b">6<br>

<input type ="radio" name="q1" value="c">4<br>

<input type ="radio" name="q1" value="d">10<br><br><br>

<label>Output of [Link](3,6) ?</label><br>

<input type ="radio" name="q2" value="a">3<br>

<input type ="radio" name="q2" value="b">4<br>

<input type ="radio" name="q2" value="c">3.0<br>

<input type ="radio" name="q2" value="d">4.0<br><br><br>

<label>What is the variable declared in a class for the use of all methods of the class
called ?</label><br>

<input type ="radio" name="q3" value="a">Object<br>

<input type ="radio" name="q3" value="b">Instance Variable<br>

<input type ="radio" name="q3" value="c">Reference Variable<br>

<input type ="radio" name="q3" value="d">None<br><br><br>

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

<input type="reset" value="Reset">

</form>

</body>

</html>
[Link] –

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class QuizServlet extends HttpServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();

[Link]("<!DOCTYPE html>");

[Link]("<html>");

[Link]("<head>");

[Link]("<title>Servlet QuizServlet</title>");

[Link]("</head>");

[Link]("<body>");

String paramName, paramValue[];

int cnt = 0;

String ans = "";


Enumeration paramNames = [Link]();

try

[Link]("[Link]");

Connection con =
[Link]("jdbc:mysql://localhost:3306/quiz10?
characterEncoding=latin1","root","root");

Statement stmt = (Statement) [Link]();

ResultSet rs = [Link]("Select answer from mytab");

while([Link]() && [Link]())

String s = [Link](1);

paramName = (String)[Link]();

paramValue = [Link](paramName);

for(int i=0; i<[Link]; i++)

ans = paramValue[i];

if([Link](ans))

cnt++;

[Link]("<h1>You have scored "+ cnt +" points out of 3.</h1>");

catch(Exception e)

[Link]("Sorry ! Try Again..");

[Link]("</body>");
[Link]("</html>");

private String paramValue(int i)

throw new UnsupportedOperationException("Not supported yet.");

Output :

Practical 6 Aim : Create simple Servlet application to demonstrate Non-Blocking


Read Operation.
[Link] –

import [Link].*;

import [Link].*;

import [Link].*;

public class index


{

public static void main(String[] args) throws IOException

FileInputStream input = new FileInputStream("D:\\TYIT10\\JAVA\\[Link]");

ReadableByteChannel source = [Link]();

FileOutputStream output = new FileOutputStream("D:\\TYIT10\\JAVA\\[Link]");

WritableByteChannel destination = [Link]();

copyData(source,destination);

[Link]();

[Link]();

private static void copyData(ReadableByteChannel src, WritableByteChannel dest) throws


IOException

ByteBuffer buffer = [Link](20 * 1024);

while([Link](buffer) != -1)

[Link]();

while([Link]())

[Link](buffer);

[Link]();

}
Practical 7 : Implement the following JSP applications.

A) Aim : Develop a simple JSP application to display values obtained from the use of
intrinsic objects of various types.

[Link]
<%@page import="[Link]"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>Use of Intrinsic Objects in JSP</h1>

<h2>1. Request Object</h2>

Query String <%=[Link]() %><br>

Contest Path <%=[Link]() %><br>

Remote Host <%=[Link]() %><br>

<h2>2. Response Object</h2>

Character Encoding Type <%=[Link]() %><br>

Content Type <%=[Link]() %><br>

Locale <%=[Link]() %><br>

<h2>3. Session Object</h2>

ID <%=[Link]() %><br>

Creation Time<%=new [Link]([Link]()) %><br>

Last Access Time<%=new [Link]([Link]()) %><br>

<h2>4. Application Object</h2>

<% [Link]("appName", "JSP Demo App"); %>

<p>Application Name: <%= [Link]("appName") %></p>

<p>Server Info: <%= [Link]() %></p>

<h2>5. Out Object</h2>

<% [Link]("Current Time: " + new Date()); %>

<h2>6. Config Object</h2>

<p>Servlet Name: <%= [Link]() %></p>

<h2>7. PageContext Object</h2>

<p>Request Locale: <%= [Link]().getLocale() %></p>

<h2>8. Page Object</h2>


<p>Page Object Class: <%= [Link]().getName() %></p>

</body>

</html>

Output

Develop Simple Servlet Question Answer Application


using Database.

B) Aim : Develop a simple JSP application to pass values from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).

[Link]
<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<center>

<form action="[Link]" method="post">

<table border="1" cellpadding="8" cellspacing="0">

<tr>

<td>Enter your Name:</td>

<td><input type="text" name="name"></td>

</tr>

<tr>

<td>Enter your Age:</td>

<td><input type="text" name="age"></td>

</tr>

<tr>

<td>Enter your Email ID:</td>

<td><input type="text" name="email"></td>

</tr>

<tr>

<td>Select your Hobbies:</td>

<td>

<input type="checkbox" name="hob" value="Singing">Singing

<input type="checkbox" name="hob" value="Reading">Reading Books

<input type="checkbox" name="hob" value="Badminton">Playing Badminton

</td>

</tr>

<tr>

<td>Select Gender:</td>

<td>

<input type="radio" name="gen" value="male">Male

<input type="radio" name="gen" value="female">Female

<input type="radio" name="gen" value="other">Other


</td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="hidden" name="error" value="">

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

</td>

</tr>

</table>

</form>

</center>

</body>

</html>

[Link]

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<center>

<div>

<h2>VALIDATE PAGE</h2>

<%

String name = [Link]("name");

String agestr = [Link]("age");

String email = [Link]("email");


String gender = [Link]("gen");

String hobbies[] = [Link]("hob");

String errmsg = "";

if(name == null || [Link]().equals(""))

errmsg += "Please provide your name.<br>";

if(agestr == null || [Link]().equals(""))

errmsg += "Please provide your age.<br>";

else

try

int age = [Link](agestr);

if(age <= 0 || age > 120)

errmsg += "Age must be between 1 to 120.<br>";

catch(Exception e)

errmsg += "Invalid age format.<br>";

if(email == null || [Link]().equals(""))

errmsg += "Invalid email format.<br>";

}
if(hobbies == null || [Link]==0)

errmsg += "Please select at least one of the hobbies.<br>";

if(gender == null || [Link]().equals(""))

errmsg += "Please select your gender.<br>";

if(![Link]().equals(""))

[Link]("<p style='color:red;'>"+errmsg+"</p>");

[Link]("<a href='[Link]'>Go Back</a>");

else

[Link]("<p><strong>Name: </strong>"+name+"</p>");

[Link]("<p><strong>Age: </strong>"+agestr+"</p>");

[Link]("<p><strong>Hobbies: </strong></p>");

for(String hob : hobbies)

[Link]("<p>"+hob+"</p>");

[Link]("<p><strong>Email: </strong>"+email+"</p>");

[Link]("<p><strong>Gender: </strong>"+gender+"</p>");

[Link]("<a href='[Link]'>Go back to Form</a>");

%>

</div>

</center>

</body>
</html>

Output

Develop Simple Servlet Question Answer Application using Develop Simple Servlet Question
Database. Answer Application using Database.

Develop Simple Servlet Question Answer Application using Develop Simple Servlet Question Answer
Database. Application using Database.

Practical 8 Aim : Create a registration and login JSP application to register and
authenticate the user based on username and password using JDBC.

[Link] –
<!DOCTYPE html>

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="[Link]">

Enter Employee Number: <input type="text" name="txtno"><br>

Enter Name: <input type="text" name="txtname"><br>

Enter Age: <input type="text" name="txtage"><br>

Enter Salary: <input type="text" name="txtsal"><br>

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

<input type="reset" value="RESET">

</form>

</body>

</html>

[Link] –
<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page import="[Link]"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

String eno = [Link]("txtno");

String name = [Link]("txtname");


String age = [Link]("txtage");

String sal = [Link]("txtsal");

try

[Link]("[Link]");

Connection con =
[Link]("jdbc:mysql://localhost:3306/dbemp10?
characterEncoding=latin1","root", "root");

PreparedStatement stmt = [Link]("Select * from emp where


empid=?");

[Link](1, eno);

ResultSet rs = [Link]();

if([Link]())

PreparedStatement pst1 = [Link]("update emp set salary=?


where empid=?");

PreparedStatement pst2 = [Link]("update emp set age=?


where empid=?");

PreparedStatement pst3 = [Link]("update emp set ename=?


where empid=?");

[Link]("<h1>Employee "+ eno+ " Record Updated Successfully</h1>");

[Link](1, sal);

[Link](2, eno);

[Link](1, age);

[Link](2, eno);

[Link](1, name);

[Link](2, eno);

[Link]();

[Link]();

[Link]();

}
else

[Link]("<h1>Employee Record does not exists..!!!</h1>");

catch(Exception e)

[Link](e);

%>

</body>

</html>

Output :

You might also like