0% found this document useful (0 votes)
2 views36 pages

Java Program 2024-2025

The document contains multiple Java Servlet and JSP examples, including a welcome message servlet, purchase order forms, student marks calculation, and employee pay slip generation. Each example demonstrates how to handle form data, process it, and display results in a web application context. The document also includes JDBC usage for database operations and error handling in form submissions.

Uploaded by

sujithhkkumarr2
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)
2 views36 pages

Java Program 2024-2025

The document contains multiple Java Servlet and JSP examples, including a welcome message servlet, purchase order forms, student marks calculation, and employee pay slip generation. Each example demonstrates how to handle form data, process it, and display results in a web application context. The document also includes JDBC usage for database operations and error handling in form submissions.

Uploaded by

sujithhkkumarr2
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

1.

Welcome Message using Servlet

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class hello extends HttpServlet
{
private static final long serialVersionUID = 1L;
public hello()
{
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException
{
[Link]("text/html");
PrintWriter out=[Link]();
[Link]("Welcome");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException
{
}

}
OUTPUT:
2. Purchase order form using HTML Form and Servlet

[Link]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Purchase Order Form</h2>
<form action="PurchaseOrderServlet" method="post">
<label for="productName">Product Name:</label>
<input type="text" id="productName" name="productName" required><br>

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" required><br>

<label for="unitPrice">Unit Price:</label>


<input type="number" id="unitPrice" name="unitPrice" required><br>

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


</form>
</body>
</html>
[Link]

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

public class PurchaseOrderServlet extends HttpServlet {


private static final long serialVersionUID = 1L;

public PurchaseOrderServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException
{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
String productName = [Link]("productName");
int quantity = [Link]([Link]("quantity"));
double unitPrice = [Link]([Link]("unitPrice"));
double totalCost = quantity * unitPrice;
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Purchase Order Details</h2>");
[Link]("<p>Product Name: " + productName + "</p>");
[Link]("<p>Quantity: " + quantity + "</p>");
[Link]("<p>Unit Price: $" + unitPrice + "</p>");
[Link]("<p>Total Cost: $" + totalCost + "</p>");
[Link]("</body></html>");
}

}
OUTPUT:
3. Percentage of marks of a student using JSP.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Mark Statement</title>
</head>
<body bgcolor=#12afff>
<center><h1>PAAVENDHAR COLLEGE OF ARTS & SCIENCE</h1></br>
<h2>DEPARTMENT OF COMPUTER APPLICATIONS</h2></br></center>
<p>Enter Marks</p>
<form action="[Link]" method="get">
<table border=1 bgcolor=#fbaaff> <tr><td> Enter Makrs in Advanced Java
:</td><td> <input type="text" name="java"> </td></tr>
<tr><td>Enter NMA Marks : </td><td><input type="text"
name="NMA"></td></tr>
<tr><td>Enter MCAD Marks :</td><td> <input type="text" name="MCAD">
<tr><td>Enter PPUD Marks : </td><td><input type="text" name="PPUD">
<tr><td>Enter Project Marks :</td><td> <input type="text" name="pro">
<tr><td><input type="submit"></td></tr>
</form>
</body>
</html>
[Link]

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center><h1>PAAVENDHAR COLLEGE OF ARTS & SCIENCE</h1></br>
<h2>DEPARTMENT OF COMPUTER APPLICATIONS</h2></br></center>
<%
int java=[Link]([Link]("java"));
int NMA=[Link]([Link]("NMA"));
int MCAD=[Link]([Link]("MCAD"));
int PPUD=[Link]([Link]("PPUD"));
int Project=[Link]([Link]("pro"));

int c=java+NMA+MCAD+PPUD+Project;
double avg=c/5;
[Link]("<br>Total="+c);
[Link]("<br>Average="+avg);
[Link]("<br>Your grade is ");
if(avg > 90 ){
[Link]("A");
}else if (avg >= 80) {
[Link]("B");
} else if (avg >= 70) {
[Link]("C");
} else if (avg >= 60) {
[Link]("D");
} else {
[Link]("E");
}
%>
</body>
</html>
OUTPUT:
4. Purchase Order form using Html form and JSP.

[Link]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Purchase Order Form</h2>
<form action="[Link]" method="post">
<label for="productName">Product Name:</label>
<input type="text" id="productName" name="productName" required><br>

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" required><br>

<label for="unitPrice">Unit Price:</label>


<input type="number" id="unitPrice" name="unitPrice" required><br>

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


</form>
</body>
</html>
[Link]

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Purchase Order Details</h2>

<%-- Retrieve form data from request --%>


<%
String productName = [Link]("productName");
int quantity = [Link]([Link]("quantity"));
double unitPrice = [Link]([Link]("unitPrice"));

// Calculate total cost


double totalCost = quantity * unitPrice;
%>

<p>Product Name: <%= productName %></p>


<p>Quantity: <%= quantity %></p>
<p>Unit Price: $<%= unitPrice %></p>
<p>Total Cost: $<%= totalCost %></p>
</body>
</html>
OUTPUT:
[Link] pay slip using JSP

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Employee Pay Slip</h2>
<form action="[Link]" method="post">
<label for="employeeName">Employee Name:</label>
<input type="text" id="employeeName" name="employeeName" required><br>
<label for="employeeId">Employee ID:</label>
<input type="text" id="employeeId" name="employeeId" required><br>
<label for="basicSalary">Basic Salary:</label>
<input type="number" id="basicSalary" name="basicSalary" required><br>
<label for="allowances">Allowances:</label>
<input type="number" id="allowances" name="allowances" required><br>
<label for="deductions">Deductions:</label>
<input type="number" id="deductions" name="deductions" required><br>
<input type="submit" value="Generate Pay Slip">
</form>
</body></html>
[Link]

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Generated Pay Slip</h2>
<%-- Retrieve form data from request --%>
<% String employeeName = [Link]("employeeName"); %>
<% String employeeId = [Link]("employeeId"); %>
<% double basicSalary = [Link]([Link]("basicSalary")); %>
<% double allowances = [Link]([Link]("allowances")); %>
<% double deductions = [Link]([Link]("deductions")); %>
<%-- Calculate total salary --%>
<% double totalSalary = basicSalary + allowances - deductions; %>
<p><strong>Employee Name:</strong> <%= employeeName %></p>
<p><strong>Employee ID:</strong> <%= employeeId %></p>
<p><strong>Basic Salary:</strong> $<%= basicSalary %></p>
<p><strong>Allowances:</strong> $<%= allowances %></p>
<p><strong>Deductions:</strong> $<%= deductions %></p>
<hr>
<p><strong>Total Salary:</strong> $<%= totalSalary %></p>
</body>
</html>
OUTPUT:
[Link] for Creating a Table, Inserting, Deleting Record

[Link];
[Link];
[Link];
[Link];
classstudentmarklist
{
public static void main(String []args)
{
try
{
[Link]("[Link]");
Connection con= [Link]("jdbc:odbc:bb","","");
Statement s=[Link]();
ResultSetrs=[Link]("select *from student");
[Link]("\n");
[Link]("STUDENT MARKLIST");
[Link]("********************");
[Link]("\n");
[Link]("sno name m1 m2 tot");
[Link]("*****************");
while([Link]())
{
[Link]([Link](1)+" "+[Link](2)+" "+
[Link](3)+" "+[Link](4)+" "+[Link](5));
}
[Link]();
}
catch(Exception e)
{
[Link]();
}
}
}
OUTPUT
[Link] using Java servlet to handle form data

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Sign Up Form</h2>
<form action="req" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword"
required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<input type="submit" value="Sign Up">
</form>
</body>
</html>
[Link]

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class req extends HttpServlet {
private static final long serialVersionUID = 1L;

public req() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html");
String username = [Link]("username");
String password = [Link]("password");
String confirmPassword = [Link]("confirmPassword");
String email = [Link]("email");
if (username == null || password == null || confirmPassword == null || email
== null ||
[Link]().isEmpty() || [Link]().isEmpty() ||
[Link]().isEmpty() || [Link]().isEmpty() ||
![Link](confirmPassword)) {

PrintWriter out = [Link]();


[Link]("<html><head><title>Sign Up Error</title></head><body>");
[Link]("<h2>Sign Up Error</h2>");
[Link]("<p>Invalid form data. Please fill in all fields and ensure passwords
match.</p>");
[Link]("<p><a href=\"[Link]\">Back to Sign Up Form</a></p>");
[Link]("</body></html>");
} else {
PrintWriter out = [Link]();
[Link]("<html><head><title>Sign Up
Confirmation</title></head><body>");
[Link]("<h2>Sign Up Confirmation</h2>");
[Link]("<p>Thank you, " + username + ", for signing up!</p>");
[Link]("<p><a href=\"[Link]\">Back to Sign Up Form</a></p>");
[Link]("</body></html>");
}
}}
OUTPUT:
[Link] Servlet program to create a table

[Link]

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ex8 extends HttpServlet {
private static final long serialVersionUID = 1L;
public ex8() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException
{
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><head><title>Header Table</title></head><body>");
[Link]("<h2>Header Table</h2>");
[Link]("<table border='1'><tr><th>Header Name</th><th>Header
Value</th></tr>");
Enumeration<String> headerNames = [Link]();
while ([Link]()) {
String headerName = [Link]();
Enumeration<String> headerValues = [Link](headerName);
while ([Link]()) {
String headerValue = [Link]();
[Link]("<tr><td>" + headerName + "</td><td>" + headerValue +
"</td></tr>");
}
}
[Link]("</table></body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

}
OUTPUT:
[Link] in JSP by using session object.

[Link]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Session Example</h2>

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


<label for="username">Enter your username:</label>
<input type="text" id="username" name="username" required>

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


</form>
</body>
</html>
[Link]

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<%@ page import="[Link].*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Process Form</h2>

<%-- Retrieve data from the request --%>


<%
String username = [Link]("username");

session = [Link](true);

[Link]("username", username);
%>

<p>Username <%= username %> stored in the session.</p>

<p><a href="[Link]">Display Session Data</a></p>

</body>
</html>
[Link]

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Display Session Data</h2>
<%-- Retrieve data from the session --%>
<%
String username = (String) [Link]("username");
if (username != null)
{
%>
<p>Username <%= username %> retrieved from the session.</p>
<%
}
Else
{
%>
<p>No username found in the session.</p>
<%
}
%>
<p><a href="[Link]">Go back to Home Page</a></p>
</body>
</html>
OUTPUT:
10 .Client Server application using RMI

RMI Interface

import [Link].*;
import [Link].*;
[Link].*;
public interface rmiinterface extends Remote
{
public String display(String m)throws RemoteException;
}

RMI Implementing Class

import [Link].*;
[Link].*;
classrmiimplementingclass extends UnicastRemoteObject implements rmiinterface
{
publicrmiimplementingclass() throws RemoteException
{
}
public String display(String m) throws RemoteException
{
return "message :"+m;
}
public static void main(String args[])
{
try
{
rmiimplementingclassrm=new rmiimplementingclass();
[Link]("messageserver",rm);
}
catch(Exception e)
{
[Link]("Error!"+e);
}
}
}
RMI Client

import [Link].*;
import [Link].*;
classrmiclient
{
public static void main(String a[]) throws IOException
{
try
{
String url="rmi://"+a[0]+"/messageserver";
rmiinterface r=(rmiinterface)[Link](url);
[Link]("Enter a message:");
DataInputStream dim=new DataInputStream([Link]);
String m=[Link]();
String s=[Link](m);
[Link](s);
}
catch(Exception e)
{
[Link]("Error="+e);
}
}
}
OUTPUT

Server Command Shell (RMI Implementing Class)


Client Command Shell(RMI Client)
11. Applet for a calculator application

import [Link].*;
import [Link].*;
import [Link].*;
public class Calculator1 extends Applet implements ActionListener
{
TextField inp;
//Function to add features to the frame
public void init()
{
setBackground([Link]);
setLayout(null);
int i;
inp = new TextField();
[Link](150,100,270,50);
[Link](inp);
Button button[] = new Button[10];
for(i=0;i<10;i++)
{
button[i] = new Button([Link](9-i));
button[i].setBounds(150+((i%3)*50),150+((i/3)*50),50,50);
[Link](button[i]);
button[i].addActionListener(this);
}
Button dec=new Button(".");
[Link](200,300,50,50);
[Link](dec);
[Link](this);

Button clr=new Button("C");


[Link](250,300,50,50);
[Link](clr);
[Link](this);

Button operator[] = new Button[5];


operator[0]=new Button("/");
operator[1]=new Button("*");
operator[2]=new Button("-");
operator[3]=new Button("+");
operator[4]=new Button("=");
for(i=0;i<4;i++)
{
operator[i].setBounds(300,150+(i*50),50,50);
[Link](operator[i]);
operator[i].addActionListener(this);
}
operator[4].setBounds(350,300,70,50);
[Link](operator[4]);
operator[4].addActionListener(this);
}
String num1="";
String op="";
String num2="";
//Function to calculate the expression
public void actionPerformed(ActionEvent e)
{
String button = [Link]();
char ch = [Link](0);
if(ch>='0' && ch<='9'|| ch=='.')
{
if (![Link](""))
num2 = num2 + button;
else
num1 = num1 + button;
[Link](num1+op+num2);
}
else if(ch=='C')
{
num1 = op = num2 = "";
[Link]("");
}
else if (ch =='=')
{
if(![Link]("") && ![Link](""))
{
double temp;
double n1=[Link](num1);
double n2=[Link](num2);
if(n2==0 && [Link]("/"))
{
[Link](num1+op+num2+" = Zero Division Error");
num1 = op = num2 = "";
}
else
{
if ([Link]("+"))
temp = n1 + n2;
else if ([Link]("-"))
temp = n1 - n2;
else if ([Link]("/"))
temp = n1/n2;
else
temp = n1*n2;
[Link](num1+op+num2+" = "+temp);
num1 = [Link](temp);
op = num2 = "";
}
}
else
{
num1 = op = num2 = "";
[Link]("");
}
}
else
{
if ([Link]("") || [Link](""))
op = button;
else
{
double temp;
double n1=[Link](num1);
double n2=[Link](num2);
if(n2==0 && [Link]("/"))
{
[Link](num1+op+num2+" = Zero Division Error");
num1 = op = num2 = "";
}
else
{
if ([Link]("+"))
temp = n1 + n2;
else if ([Link]("-"))
temp = n1 - n2;
else if ([Link]("/"))
temp = n1/n2;
else
temp = n1*n2;
num1 = [Link](temp);
op = button;
num2 = "";
}
}
[Link](num1+op+num2);
}
}
}

/*
<applet code = [Link] width=600 height=600>
</applet>
*/
OUTPUT:
[Link] programming

NETWORK PROGRAMMING USING STRING

import [Link].*;
import [Link].*;
class ServerDemo2
{
public static void main(String args[])
{
try
{
int port=[Link](args[0]);
ServerSocketss=new ServerSocket(port);
Socket s=[Link]();
OutputStreamos=[Link]();
DataOutputStream dos=new DataOutputStream(os);
[Link]("Enter a string");
InputStreamReader r=new InputStreamReader([Link]);
BufferedReader in=new BufferedReader(r);
String s1=[Link]();
[Link](s1);
[Link]();
}
catch(Exception e)
{
[Link]("Exception :"+e);
}
}
}
Client Command Shell

import [Link].*;
import [Link].*;
class ClientDemo2
{
public static void main(String args[])
{
try
{
String server=args[0];
int port=[Link](args[1]);
Socket s=new Socket(server,port);
InputStream is=[Link]();
DataInputStream dis=new DataInputStream(is);
String s1=[Link]();
[Link]("Received string is:"+s1);
[Link]();
}
catch(Exception e)
{
[Link]("exception e:"+e);
}
}
}
Output:

Server Command Shell

Output:

Client Command Shell

You might also like