1.
DISPLAY A WELCOME MESSAGE USING SERVLET
[Link]
<html>
<head>
<title>welcome</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="gopi" method="GET">
<h1><center>Application Form</center></h1>
<label><b>Enter Your Name:</b></label>
<input type="text" name="name">
<input type="submit" value="submit">
<input type="reset" value="clear">
</form>
</body>
</html>
Servlet file
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet gopi</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Hello " + [Link]("name") + "Welcome to our
website</h1>");
[Link]("</body>");
[Link]("</html>");
}
}
[Link] A PURCHASE ORDER USING HTML FORM AND SERVLET
[Link]
<html>
<head>
<title>WebApplication1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body bgcolor="grey">
<form action="gopi1" method="get">
<center>
<h1><b>Purchase order</b></h1>
<label>Supplier name:</label>
<input type="text" name="a"><br><br>
<label>Item name:</label>
<input type="text" name="b"><br><br>
<label>Item Number:</label>
<input type="number" name="c"><br><br>
<label>Quantity:</label>
<input type="number" name="d"><br><br><!-- comment -->
<label>Price:</label>
<input type="number" name="e"><br><br>
<input type="submit" value="place the order">
<input type="reset" value="clear">
</center>
</form>
</body>
</html>
Servlet file
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String a =[Link]("a");
String b =[Link]("b");
int c =[Link]([Link]("c"));
int d =[Link]([Link]("d"));
double e =[Link]([Link]("e"));
double total =d * e;
[Link]("text/html");
[Link]().println("<h1>Order of Summary</h1>");
[Link]().println("supplier name:"+a +"<br/>");
[Link]().println("Item Name:"+b +"<br/>");
[Link]().println("Item Number:"+c +"<br/>");
[Link]().println("Quantity:"+d +"<br/>");
[Link]().println("Price:"+e +"<br/>");
[Link]().println("Total:"+total +"<br/>"
}
[Link] a program for calculating the percentage of marks of a student using
JSP.
Index html
<html>
<head>
<title>Percentage of mark</title>
</head>
<body>
<p>Enter the marks</p>
<form action="[Link]" method="post">
enter name:<input type="text" name="name"><br><br>
enter mark1:<input type="number" name="mark1"><br><br>
enter mark2:<input type="number" name="mark2"><br><br>
enter mark3:<input type="number" name="mark3"><br><br>
enter mark4:<input type="number" name="mark4"><br><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
JSP file
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String name=[Link]("name");
int mark1=[Link]([Link]("mark1"));
int mark2=[Link]([Link]("mark2"));
int mark3=[Link]([Link]("mark3"));
int mark4=[Link]([Link]("mark4"));
double total=mark1+mark2+mark3+mark4;
double percentage=total/4;
%>
<!DOCTYPE html>
<html>
<body bgcolor="pink">
<center><h1>percentage calculation</h1>
<p>enter name:<%= name%></p>
<p>total marks:<%= total%></p>
<p>percentage:<%= [Link]("%.2f", percentage)%></p></center>
</body>
</html>
[Link] ORDER USING HTML FORM AND JSP
Index 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]" method="post">
<center>Purchase Order<br><br>
Customer: <input Name="customer"> <br><br>
Item: <input name="item"><br><br>
Qty: <input type="number" name="qty"><br><br>
Price: <input type="number" step="0.01" name="price"><br><br>
<input type="submit" value="submit"></center>
</form>
</body>
</html>
JSP file
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String customer=[Link]("customer");
String item=[Link]("item");
int qty= [Link]([Link]("qty"));
double price=[Link]([Link]("price"));
double total=qty*price;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p>customer:<%=customer%></p>
<p>item:<%=item%></p>
<p>quantity:<%=qty%></p>
<p>total:Rs.<%=total %></p>
</body>
</html>
5. PREPARE A EMPLOYEE PAY SLIP USING JSP.
Index html
<html>
<head>
<title>Employee Pill</title>
</head>
<body bgcolor="pink"><center>
<h1>Employee Pay Slip</h1><!-- comment -->
<form action="[Link]" method="get">
Emp name:<input type="text" name="empname"><br><br><!-- comment -->
Emp no:<input type="number" name="empno"><br><br>
Basic salary:<input type="number" name="bs"><br><br>
MA:<input type="number" name="ma"><br><br>
DA:<input type="number" name="da"><br><br>
HRA:<input type="number" name="hra"><br><br>
<input type="submit" value="calculate salary">
<input type="reset" value="cancel"></center>
</form>
</body>
</html>
JSP file
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String empname=[Link]("empname");
int empno=[Link]([Link]("empno"));
double bs=[Link]([Link]("bs"));
double ma=[Link]([Link]("ma"));
double da=[Link]([Link]("da"));
double hra=[Link]([Link]("hra"));
double grosspay=bs+ma+da+hra;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="cyan">
<center><h1>Employee Pay Slip</h1>
<p>Emp name:<%= empname %></p>
<p>Emp no:<%= empno %></p>
<p>Basic Salary:<%= bs%></p>
<p>DA:<%= da %></p>
<p>HRA:<%= hra %></p>
<p>Gross Pay:<%= grosspay%></p></center>
</body>
</html>
[Link] USING JDBC FOR CREATING A TABLE, INSERTING, DELETING
RECORDS AND LIST OUT THE RECORDS.
Servlet file
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class UserServlet extends HttpServlet {
private static final String DB_URL =
"jdbc:ucanaccess://C:\\Users\\ADMIN\\Documents\\[Link]";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String action = [Link]("action");
String name = [Link]("name");
String pwd = [Link]("pwd");
String mail = [Link]("mail");
String country = [Link]("country");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
[Link]("[Link]");
con = [Link](DB_URL);
[Link]("<html><body>");
// ================= INSERT =================
if ("insert".equals(action)) {
String sql = "INSERT INTO usertab VALUES(?,?,?,?)";
ps = [Link](sql);
[Link](1, name);
[Link](2, pwd);
[Link](3, mail);
[Link](4, country);
[Link]();
[Link]("<h2>Successfully Registered</h2>");
}
// ================= UPDATE =================
else if ("update".equals(action)) {
String sql =
"UPDATE usertab SET name=?, pwd=?, country=? WHERE mail=?";
ps = [Link](sql);
[Link](1, name);
[Link](2, pwd);
[Link](3, country);
[Link](4, mail);
int r = [Link]();
if (r > 0)
[Link]("<h2>Updated Successfully</h2>");
else
[Link]("<h2>User Not Found</h2>");
}
// ================= DELETE =================
else if ("delete".equals(action)) {
String sql = "DELETE FROM usertab WHERE mail=?";
ps = [Link](sql);
[Link](1, mail);
int r = [Link]();
if (r > 0)
[Link]("<h2>Deleted Successfully</h2>");
else
[Link]("<h2>User Not Found</h2>");
}
// ================= LIST =================
else if ("list".equals(action)) {
Statement st = [Link]();
rs = [Link]("SELECT * FROM usertab");
[Link]("<h2>User Report</h2>");
[Link]("<table border='1' cellpadding='10'>");
[Link]("<tr>");
[Link]("<th>Name</th>");
[Link]("<th>Password</th>");
[Link]("<th>Email</th>");
[Link]("<th>Country</th>");
[Link]("</tr>");
while ([Link]()) {
[Link]("<tr>");
[Link]("<td>" + [Link](1) + "</td>");
[Link]("<td>" + [Link](2) + "</td>");
[Link]("<td>" + [Link](3) + "</td>");
[Link]("<td>" + [Link](4) + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
}
[Link]("<br><a href='[Link]'>Back</a>");
[Link]("</body></html>");
} catch (Exception e) {
[Link]("Error : " + e);
} finally {
try { if (rs != null) [Link](); } catch (Exception ex) {}
try { if (ps != null) [Link](); } catch (Exception ex) {}
try { if (con != null) [Link](); } catch (Exception ex) {}
}
}
}
[Link]
<html>
<body>
<h2>User Form</h2>
<form action="UserServlet" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="password" name="pwd"><br>
Email: <input type="text" name="mail"><br>
Country: <input type="text" name="country"><br><br>
<button name="action" value="insert">Insert</button>
<button name="action" value="update">Update</button>
<button name="action" value="delete">Delete</button>
<button name="action" value="list">List</button>
</form>
</body>
</html>
7. PROGRAM USING JAVA SERVLET TO HANDLE FORM DATA.
[Link]:
<html>
<head>
<title>Student Details Form</title>
</head>
<body>
<h2>Student Details Form</h2>
<form action="StudentServlet" method="post">
<h3>Personal Details</h3>
Name: <input type="text" name="name"><br><br>
Father Name: <input type="text" name="father"><br><br>
Age: <input type="number" name="age"><br><br>
Date of Birth: <input type="date" name="dob"><br><br>
Address: <textarea name="address"></textarea><br><br>
Email: <input type="email" name="email"><br><br>
Phone: <input type="text" name="phone"><br><br>
<h3>Course Preferred</h3>
<input type="radio" name="course" value="MSc CS"> MSc CS<br>
<input type="radio" name="course" value="MSc CHE"> MSc CHE<br>
<input type="radio" name="course" value="MSc HSC"> MSc HSC<br>
<input type="radio" name="course" value="MCom"> MCom<br><br>
<h3>Educational Qualification</h3>
10th School Name: <input type="text" name="school10">
Mark: <input type="text" name="mark10"><br><br>
12th School Name: <input type="text" name="school12">
Mark: <input type="text" name="mark12"><br><br>
UG College Name: <input type="text" name="college">
Mark / CGPA: <input type="text" name="ugmark"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Servlet file:
import [Link].*;
import [Link].*;
import [Link].*;
public class StudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
// Personal details
String name = [Link]("name");
String father = [Link]("father");
String age = [Link]("age");
String dob = [Link]("dob");
String address = [Link]("address");
String email = [Link]("email");
String phone = [Link]("phone");
// Course
String course = [Link]("course");
// Educational details
String school10 = [Link]("school10");
String mark10 = [Link]("mark10");
String school12 = [Link]("school12");
String mark12 = [Link]("mark12");
String college = [Link]("college");
String ugmark = [Link]("ugmark");
[Link]("<html><body>");
[Link]("<h2>Student Application Details</h2>");
// Personal Information
[Link]("<table border='1' cellpadding='10'>");
[Link]("<tr><th colspan='2'>Personal Information</th></tr>");
[Link]("<tr><td>Name</td><td>" + name + "</td></tr>");
[Link]("<tr><td>Father Name</td><td>" + father + "</td></tr>");
[Link]("<tr><td>Age</td><td>" + age + "</td></tr>");
[Link]("<tr><td>DOB</td><td>" + dob + "</td></tr>");
[Link]("<tr><td>Address</td><td>" + address + "</td></tr>");
[Link]("<tr><td>Email</td><td>" + email + "</td></tr>");
[Link]("<tr><td>Phone</td><td>" + phone + "</td></tr>");
[Link]("<tr><td>Course Preferred</td><td>" + course + "</td></tr>");
[Link]("</table><br>");
// Educational Qualification
[Link]("<table border='1' cellpadding='10'>");
[Link]("<tr><th>Qualification</th><th>Institution</th><th>Marks</th></tr>");
[Link]("<tr><td>10th</td><td>" + school10 + "</td><td>" + mark10 +
"</td></tr>");
[Link]("<tr><td>12th</td><td>" + school12 + "</td><td>" + mark12 +
"</td></tr>");
[Link]("<tr><td>UG</td><td>" + college + "</td><td>" + ugmark + "</td></tr>");
[Link]("</table>");
[Link]("</body></html>");
}
}
8. simple Servlet program to create a table of all the headers it receives along with their
associated values.
[Link] IN JSP BY USING SESSION OBJECT.
[Link]:
<html>
<head>
<title>Student Form</title>
</head>
<body>
<h2>Student Application Form</h2>
<form action="[Link]" method="post">
Name: <input type="text" name="name"><br><br>
Father Name: <input type="text" name="father"><br><br>
Age: <input type="number" name="age"><br><br>
Email: <input type="email" name="email"><br><br>
Phone: <input type="text" name="phone"><br><br>
<h3>Course Preferred</h3>
<input type="radio" name="course" value="MSc CS"> MSc CS
<input type="radio" name="course" value="MSc CHE"> MSc CHE
<input type="radio" name="course" value="MSc HSC"> MSc HSC
<input type="radio" name="course" value="MCom"> MCom
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Jsp file:
<%@ page language="java" %>
<%
String name = [Link]("name");
String father = [Link]("father");
String age = [Link]("age");
String email = [Link]("email");
String phone = [Link]("phone");
String course = [Link]("course");
[Link]("name", name);
[Link]("father", father);
[Link]("age", age);
[Link]("email", email);
[Link]("phone", phone);
[Link]("course", course);
%>
<html>
<head>
<title>Student Details</title>
</head>
<body>
<h2>Student Details (Using Session)</h2>
<table border="1" cellpadding="10">
<tr><td>Name</td><td><%= [Link]("name") %></td></tr>
<tr><td>Father Name</td><td><%= [Link]("father") %></td></tr>
<tr><td>Age</td><td><%= [Link]("age") %></td></tr>
<tr><td>Email</td><td><%= [Link]("email") %></td></tr>
<tr><td>Phone</td><td><%= [Link]("phone") %></td></tr>
<tr><td>Course</td><td><%= [Link]("course") %></td></tr>
</table><br><a href="[Link]">Back</a></body></html>
10. PROGRAM TO BUILD A SIMPLE CLIENT SERVER APPLICATION USING RMI
INTERFACE:
import [Link];
import [Link];
public interface Computr extends Remote {
int add(int a, int b) throws RemoteException;
int sub(int a, int b) throws RemoteException;
int mul(int a, int b) throws RemoteException;
int div(int a, int b) throws RemoteException;
}
SERVER IMPLEMENTATION
import [Link];
import [Link];
public class ComputeImpl extends UnicastRemoteObject implements Computr {
protected ComputeImpl() throws RemoteException { super(); }
@Override
public int add(int a, int b) { return a + b; }
@Override
public int sub(int a, int b) { return a - b; }
@Override
public int mul(int a, int b) { return a * b; }
@Override
public int div(int a, int b) { return a / b; }
}
SERVER:
import [Link];
import [Link];
public class RMIServer {
public static void main(String[] args) throws Exception {
ComputeImpl obj;
obj = new ComputeImpl();
Registry registry = [Link](1099);
[Link]("ComputeService", obj);
[Link]("RMI Server ready");
}
}
CLIENT
import [Link];
import [Link];
import [Link];
public class RMIClient {
public static void main(String[] args) throws Exception {
Registry registry = [Link]("localhost", 1099);
Computr stub = (Computr) [Link]("ComputeService");
Scanner sc = new Scanner([Link]);
[Link]("Enter value of a: ");
int a = [Link]();
[Link]("Enter value of b: ");
int b = [Link]();
[Link]("ADDITION OF " + a + " + " + b + " = " + [Link](a, b));
[Link]("SUBTRACTION OF " +a + " - " + b + " = " + [Link](a, b));
[Link]("PRODUCT OF "+a + " * " + b + " = " + [Link](a, b));
if (b != 0) {
[Link]("DIVISION OF" +a + " / " + b + " = " + [Link](a, b));
} else {
[Link]("Division by zero is not allowed");
}
[Link]();
}
}
11. Create an applet for a calculator application
import [Link];
import [Link].*;
import [Link].*;
public class Calculator extends Applet implements ActionListener {
TextField display;
Button b[] = new Button[10];
Button add, sub, mul, div, eq, clr;
int num1 = 0, num2 = 0;
char op;
public void init() {
setLayout(new BorderLayout());
display = new TextField();
[Link](false);
add(display, [Link]);
Panel p = new Panel();
[Link](new GridLayout(4, 4));
for (int i = 0; i <= 9; i++) {
b[i] = new Button([Link](i));
b[i].addActionListener(this);
}
add = new Button("+");
sub = new Button("-");
mul = new Button("*");
div = new Button("/");
eq = new Button("=");
clr = new Button("C");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](b[7]); [Link](b[8]); [Link](b[9]); [Link](add);
[Link](b[4]); [Link](b[5]); [Link](b[6]); [Link](sub);
[Link](b[1]); [Link](b[2]); [Link](b[3]); [Link](mul);
[Link](b[0]); [Link](clr); [Link](eq); [Link](div);
add(p, [Link]);
}
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i <= 9; i++) {
if ([Link]() == b[i]) {
[Link]([Link]() + i);
}
}
if ([Link]() == add ||
[Link]() == sub ||
[Link]() == mul ||
[Link]() == div) {
num1 = [Link]([Link]());
[Link]("");
if ([Link]() == add) op = '+';
if ([Link]() == sub) op = '-';
if ([Link]() == mul) op = '*';
if ([Link]() == div) op = '/';
}
if ([Link]() == eq) {
num2 = [Link]([Link]());
int result = 0;
switch (op) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
}
[Link]([Link](result));
}
if ([Link]() == clr) {
[Link]("");
}
}
}
12. Program to send a text message to another system and receive the text message from the
system (use socket programming).
Server
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9999);
[Link]("Server listening on port 9999...");
Socket socket = [Link]();
[Link]("Client connected.");
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
String msg;
while ((msg = [Link]()) != null) {
[Link]("Client: " + msg);
if ("bye".equalsIgnoreCase([Link]())) break;
// echo back
[Link]("Server received: " + msg);
}
[Link]();
[Link]();
}
}
Client
import [Link].*;
import [Link].*;
import [Link];
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 9999); // replace with server IP for remote
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
Scanner scanner = new Scanner([Link]);
[Link]("Connected to server. Type messages (type 'bye' to quit).");
while (true) {
String line = [Link]();
[Link](line);
String response = [Link]();
[Link]("Server: " + response);
if ("bye".equalsIgnoreCase([Link]())) break;
}
[Link]();
}
}