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

PRACTICAL - 1 Advanced Java

The document outlines practical assignments for a TY BSc-IT Advanced Java course, including the implementation of a simple calculator servlet, a login servlet, and a registration servlet. It provides source code for each servlet and corresponding HTML forms for user interaction. Additionally, it includes example outputs and error handling for the servlet operations.

Uploaded by

nupur10905
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)
4 views9 pages

PRACTICAL - 1 Advanced Java

The document outlines practical assignments for a TY BSc-IT Advanced Java course, including the implementation of a simple calculator servlet, a login servlet, and a registration servlet. It provides source code for each servlet and corresponding HTML forms for user interaction. Additionally, it includes example outputs and error handling for the servlet operations.

Uploaded by

nupur10905
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

[Link]-IT Sem V ROLL NO.

:- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________

PRACTICAL - 1

AIM:- Implement the following Simple Servlet applications.

(A).Create a simple calculator application using servlet.


SOURCE CODE:-

Html Code:

<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 method="post" action="Cal">

NO-1 <input type="text" name="t1">

NO-2 <input type="text" name="t2"><br><br>

<input type="submit" value="+"name="btn">

<input type="submit" value="-"name="btn">

<input type="submit" value="*" name="btn">

<input type="submit" value="/" name="btn">

</form>

</body>

</html>

____________________________________________________________________________
1 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

import [Link];

import [Link];

public class Cal extends HttpServlet {

​ protected void processRequest(HttpServletRequest request, HttpServletResponse


response)

throws ServletException, IOException {

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

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

int a=[Link]([Link]("t1"));

int b=[Link]([Link]("t2"));

int c=0;

String op=[Link]("btn");

if([Link]("+"))

c=a+b;

else if([Link]("-"))

c=a-b;

else if([Link]("*"))

c=a*b;

____________________________________________________________________________
2 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________
else if([Link]("/"))

c=a/b;

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

[Link]("<h1><b>"+a+op+b+"="+c+"<b></h1>");

}​

​ } ​

OUTPUT:-

____________________________________________________________________________
3 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________

(D). Create a servlet for a login page. If the username and password are correct then
it says message “Hello ” else a message “login failed”.

Servlet

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class NewServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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

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

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

String Pass=[Link]("pa");

/* TODO output your page here. You may use following sample code. */

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

[Link]("<html>");

[Link]("<head>");

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

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

____________________________________________________________________________
4 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________
[Link]("<body>");

if([Link]("Diksha/")&& [Link]("servlet"))

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

else

[Link]("<h1>wrong user name and password</h1>");

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

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

​ }


INDEX

<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="NewServlet" method="post">

____________________________________________________________________________
5 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________
​ <br>

​ Enter your Name<input type="text" name="t1">

​ Enter your Password<input type="password" name="pa">

​ <input type="Submit" value="Click"></br>

​ </form>

​ </body>

</html>

OUTPUT:-

____________________________________________________________________________
6 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________

REGISTRATION ​

[Link]
import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/RegServlet"})
public class RegServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
(PrintWriter out = [Link]());
String Uname=[Link]("Username");
String Passwd=[Link]("Password");
String Email=[Link]("emailId");
String Country=[Link]("Country");
try{
[Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost:3306/mysql","root","12345");
PreparedStatement [Link]("insert into user values (?,?,?,?)");
[Link](1,Uname);
[Link](2,Passwd);
[Link](3,Email);
[Link](4,Country);
[Link]();
}
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Registration</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("Data inserted successfully!!!");

____________________________________________________________________________
7 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________
[Link]("</body>");
[Link]("</html>");
}
catch(Exception e){[Link](e);}
[Link]("<b>"+"<b");
}

HTML CODE:-
<!DOCTYPE html>
<html>
<body>
<form action="RegisterServlet" method="post">
User name :<input type="text" name="uname"><br><br>
Password :<input type="password" name="pw"><br><br>
Email ID :<input type="text" name="email"><br><br>
Country :<select name="coun">
<option>select...
<option> India
<option> Bangladesh
<option> Bhutan
<option> Canada
</select><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>

O/P:-

____________________________________________________________________________
8 Teacher Name:- Trupti Rongare
[Link]-IT Sem V ROLL NO. :- 06
ADVANCED JAVA NAME:- Nupur Bhoir
____________________________________________________________________________

Request Dispatcher

HTML code:-

<html>
<body>
<form method = “post” action=”ValidateServlet”>
UserName:<input type=”text”name=”un”><br>
Password:<input type = “submit”value=”Login”>
</form>
</body>
</html>

____________________________________________________________________________
9 Teacher Name:- Trupti Rongare

You might also like