AJP Lab Manual
AJP Lab Manual
AJP Lab Manual
LAB MANUAL
ADVANCED JAVA PROGRAMMINGLAB
INDEX
Date Title of the Experiment
SI. No. Page No. Signature
08 TABLE HEADER
Course Objectives:
1.To enable the students to implement the simple programs using JSP, JAR.
1. Understand to the implement concepts of Java using HTML forms, JSP & JAR.
6. Write a program using JDBC for creating a table, Inserting, Deleting records and listout therecords.
8. Write a simple Servlet program to create a table of all the headers it receives along withtheir associatedvalues.
12. Program to send a text message to another system and receive the text message from the system (use socket programming).
1. WELCOME MESSAGE USING SERVLET
AIM:
PROGRAM:
MyServlet.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;
}
Index.html
<html> <head>
<meta charset="UTF-8">
</head>
</body> </html>
OUTPUT:
2. PURCHASE ORDER USING SERVLET
AIM:
ALGORITHM:
PROGRAM:
Index.html
<html>
<head>
<title>Purchase order</title>
</head>
<body>
<h1>
Please tell about product</h1>
<form action=”NewServlet”>
Purchase Order No:<input type=”text” name=”productno”> Product Name : <input type=”text” name=”productname”> Address:<input type=”text”
name=”Address”><br>
Phone no:<input type=”text” name=”phoneno”><br>
<p>
Items to be ordered:
<select name=”JeanType”>
<option value=”Lenin”>Lenin</option>
<option value=”Otto”>OTTO</option>
<option value=”S-Kumar”>S-Kumar</option>
<option value=”V-Star”>V-Star</option>
<option value=”Jockey”>Jockey</option>
<option value=”Peter”>Peter</option>
<option value=”Pantaloon”>Pantaloon</option>
</select>
<br>
Ordered Date:<input type=”date” name=”od”> Amount:<input type=”number” name=”amt”>
<input type=”submit”>
</form>
</body>
</html>
NewServlet
import java.io.IOException;
import java.io.printWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet
{
Public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
{
String productno=request.getParameter(“productno”);
String productname=request.getParameter(“productname”);
String address=request.getParameter(“address”);
String phoneno=request.getParameter(“phoneno”);
String JeanType=request.getParameter(“JeanType”);
String od=request.getParameter(“od”);
int amt=Integer.parseInt(request.getParameter(“amt”));
response.setContentType(“text/html”);
PrintWriter pw=response.getWriter();
pw.write(“<h2>Following data received successfully..</h2><br>”);
pw.write(“<h2>Product no:”+productno+”</h2><br>”);
pw.write(“<h2>Product name:”+productname+”</ h2><br>”); pw.write(“<h2>Address:”+address+”</h2><br>”); pw.write(“<h2>Phoneno”+phoneno+”</h2><br>”);
pw.write(“<h2>JeanType”+JeanType+”</h2><br>”);
pw.write(“<h2>Ordered date”+od+”</h2><br>”);
pw.write(“<h2>Amount”+amt+”</h2><br>”);
pw.write(“</h2><br>”);
}
}
3. STUDENT MARKLIST USING JSP
AIM:
To develop a program for calculating the percentage of marks of a student using jsp
ALGORITHM:
Step1: Open Netbeans IDE.
Step2: Select file – new project.
Step3: Select javaweb – web application.
Step4: Select next – give project name: Web page.
Step5: Click next – finish.
Step6: Right click on default packaged – new – servlet.
Step7: Give name for servlet and click next – finish.
Step8: Select add information. click finish.
PROGRAM:
Index.html
<html>
<head>
</head>
<body><center>
<h1>Student marks</h1>
<b><input type="submit"></b></form></center>
</body> </html>
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<%
int java=Integer.parseInt(request.getParameter("java"));
int AI=Integer.parseInt(request.getParameter("AI"));
int AOS=Integer.parseInt(request.getParameter("AOS"));
int PHP=Integer.parseInt(request.getParameter("PHP"));
int WS=Integer.parseInt(request.getParameter("WS"));
int c=java+AI+AOS+PHP+WS;
double avg=c/5;
if(avg > 90 )
{
out.println("your grade is B \n");
else
{ out.println("your grade is E
\n");
%>
</body></html>
4.PURCHASE ORDER USING JSP
AIM:
To design Purchase Order form using HTML form and jsp.
ALGORITHM:
PROGRAM:
Index.html
<html>
<body>
Purchase order no: <input type="text" name="pid"> Company Name:<input type="text" name="cname"> Company Address:<input type="text" name="cadd"><br>
<p>
Items to be ordered:
<select name="JeanType">
<option value="Lenin">Lenin</option>
<option value="Otto">Otto</option>
<option value="S-Kumar">S-Kumar</option>
<option value="V-Star">V-Star</option>
<option value="Jockey">Jockey</option>
<option value="Peter">Peter</option>
<option value="Pantaloon">Pantaloon</option>
</select>
<br>
<input type="submit">
</form>
</body> </html>
PUR.JSP
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<%
String cl = request.getParameter("cl");
%>
<h1>Hello, <%=pid%><%=cname%> <%=cadd%>!</h1> I ordered following items <%=JeanType%>. From <%=cl%>. I expect your response as soon as possible
</body>
</html>
5. EMPLOYEE PAY SLIP USING JSP
AIM:
ALGORITHM:
Step 4: select text and design a employee pay slip from using html.
Step 5: Right click on Project name and payslip -> new ->jsp.
Step 8: Display employee pay slip with according to the user input value.
PROGRAM:
Index.html
<html>
<head>
<title>Employee Salary Statement</title>
</head>
<body>
<h3>Enter Employee Details:</h3>
<form action="emp20.jsp" method="POST" >
<table>
<tr><td>Employee Name:</td><td><input type="text" name="ename" autofocus></td></tr>
<tr><td>Employee Number:</td><td><input type=”text” number=”eno”></td></tr>
<tr><td>Department:</td><td><input type="text" name="dept"></td></tr>
<tr><td>Designation:</td><td><input type="text" name="des"></td></tr>
<tr><td>Basic Salary:</td><td><input type="text" name="bsal"></td></tr>
<tr><td>TA(%):</td><td><input type="text" name="ta"></td></tr>
<tr><td>DA(%):</td><td><input type="text" name="da"></td></tr>
<tr><td>HRA(%):</td><td><input type="text" name="hra"></td></tr>
<tr><td>PF(%):</td><td><input type="text" name="pf"></td></tr>
<tr><td>LIC(%):</td><td><input type="text" name="lic"></td></tr>
<tr><td><input type="submit" value="Evaluate"></td>
<td><input type="reset" value="Reset"></td></tr>
</table>
</body>
</html>
Emp20.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ page import="java.sql.*" %>
</head>
<body bgcolor="beige">
<%
String ename=request.getParameter("ename");
String dept=request.getParameter("dept");
String des=request.getParameter("des");
double bsal=Double.valueOf(request.getParameter("bsal"));
double ta=Double.valueOf(request.getParameter("ta"));
double ta2=bsal*ta/100;
double da=Double.valueOf(request.getParameter("da"));
double da2=bsal*da/100;
double hra=Double.valueOf(request.getParameter("hra"));
double hra2=bsal*hra/100;
double pf=Double.valueOf(request.getParameter("pf"));
double pf2=bsal*pf/100;
double lic=Double.valueOf(request.getParameter("lic"));
double lic2=bsal*lic/100;
double allowance;
double deduction;
double gsal;
double netsal;
allowance = (bsal*ta)/100 + (bsal*da)/100 + (bsal*hra)/100; deduction = (bsal*pf)/100 + (bsal*lic)/100; gsal = bsal + allowance; netsal = gsal - deduction;
%>
<CENTER>
SALARY STATEMENT</font></h2></caption>
<tr><td>Department</td><td colspan=2><%=dept%></td></tr>
<tr><td>Designation</td><td colspan=2><%=des%></td></tr>
<tr><th>Allowances</th><th>Percentage</th><th>Amount</th></tr>
<tr align=center><td>TA</td><td><%=ta%></td><td><%=ta2%></td></tr>
<tr align=center><td>DA</td><td><%=da%></td><td><%=da2%></td></tr>
<tr align=center><td>HRA</td><td><%=hra%></td><td><%=hra2%></td></tr>
<tr><th>Deductions</th><th>Percentage</th><th>Amount</th></tr>
<tr align=center><td>PF</td><td><%=pf%></td><td><%=pf2%></td></tr>
<tr align=center><td>LIC</td><td><%=lic%></td><td><%=lic2%></td></tr>
</table></CENTER> </body></html>
6. JDBC WITH ORACLE
AIM:
To write a program using JDBC for creating a table, Inserting records and list out the records.
ALGORITHM:
Step 1: Setting Up Your MySQL User Account
Step 2: Create the Database
Step 3: Create a Java Project. File → New Project; a window will appear. Select Java from the Categories list and Java Application from the Projects list. Click Next.
Step 4: Add the Oracle JDBC Driver
Step 5: Creating the Application
Step 6: Execute the Application. Click Run→ Run Project, or press Ctrl+F11. This will compile and execute the project.
PROGRAM:
package OracleCon;
import java.sql.*;
Statement stmt=con.createStatement();
con.close();
}
OUTPUT:
Oracle Database current date is 2019-03-20 14:47:01.0 BUILD SUCCESSFUL (total time: 1 second)
6. HANDLING DATA USING SERVLET
AIM :
ALGORITHM:
Step 4: Click on the java web and then select web application.
Step 5: Enter the project name ,click on next-select the server name as Glassfish server and click finish.
Step 7: Click on the source packages .In that select default package.
Step 8: Create a new servlet by right clicking default packages then click new option. In the pop up menu select servlet option.
Step 9: Type the code for servlet.Run the project(Right click the project , click run option (or) click the tool bar icon
PROGRAM:
Index. html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<br>
<br>
<form action="MyServlet">
User Name: <input type="text" name="uname"> <br> Password: <input type="password" name="pass"> <br> Email : <input type="text" name="email"> <br>
Gender : <input type="radio" name="gender" value="male" checked> Male <input type="radio" name="gender" value="female">
Female <input type="radio" name="gender" value="other"> <br> Course: <input type="checkbox" name="course" value="Java"> Java <input type="checkbox"
</form>
</body> </html>
MyServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
{ pw.write(c+" ");
pw.write("</h3>");
}
7. TABLE HEADER
Aim:
To write a simple Servleprogram to create a table of all the headers it receives along with
ALGORITHM:
PROGRAM:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class NewServlet1 extends HttpServlet
IOException, ServletException
out.println("</TABLE>\n</BODY></HTML>");
}
9. SESSION OBJECT USING JSP
AIM:
To write a program in jsp by using session object.
ALGORITHM:
Step 4: Type to start a session, Create a jsp file (index. Jsp). In the form element use the action attribute to navigate the user to the firstpage.jsp file after enter the username
and password.
Step 5: Verification of user name and password in first page.jsp file. Once the user name and password are correct , then navigate to the second page.jsp, else prompt the
PROGRAM:
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
</font><br><br>
</form>
</body>
</html>
Firstpage.jsp
<html>
<head>
</head>
<body>
<%
else {
response.sendRedirect("index.jsp");
%>
</body>
</html>
Secondpage.jsp
<html>
<head>
</head>
<body>
<font size = 5>Hello <%= session.getAttribute("username") %> you are an authorized person</font>
</body>
</html>
10.CLIENT SERVER APPLICATION USING RMI
AIM:
ALGORITHM:
Step 5:Compile the implementation class and create the stub and skeleton object using the RMI tool.
PROGRAM:
AddRem.java
public interface AddRem extends Remote
{
public int addNum(int n1,int n2)throws RemoteException;
}
AddRemImpl.java
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class AddRemImpl extends UnicastRemoteObject implements AddRem
{
public AddRemImpl() throws RemoteException{} public int addNum(int a,int b)
{
return(a+b);
}
}
server.java
import java.rmi.*;
import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject; import java.rmi.RemoteException;
public class server extends UnicastRemoteObject implements AddRem{
public server() throws RemoteException
{
super();
}
public int addNum(int n1,int n2)throws RemoteException
{
return n1+n2;
} public static void main(String
args[]){ try{
Registry reg=LocateRegistry.createRegistry(9999); reg.rebind("hi server", new server()); System.out.println("Server is running");
}
catch(Exception e){System.out.println(e);
}
}
}
Client.java
public class client
{
public static void main(String args[]) throws RemoteException, NotBoundException
{ client c=new client(); c.connectRemote();
}
private void connectRemote()throws RemoteException, NotBoundException
{
try{
Registry reg=LocateRegistry.getRegistry("localhost", 9999); AddRem ad=(AddRem)reg.lookup("hi server"); System.out.println("Addition is :"+ad.addNum(34,4));
}
catch(RemoteException e){ System.out.println(e);
}
}
}
OUTPUT:
Addition is 38.
11. CALCULATOR USING APPLET
AIM:
To create an applet for a calculator application.
ALGORITHM:
Step 1: – Create a blank Java project and name it Java Calculator. Uncheck the Main class option from the project tab.
Step 2: – To add a Jframe, right click the project name in the Project window.
Step 3: – To the right of the Jframe, there is a Palette with a set of controls on it. Click the Text Field and apply it on the blank Field form.
Step 4: – Select the Button and drag it to the Jframe form. Change the text and the Variable Name as we did for the text field.
Step5: – Change the Variable Name to btnOne as we did for the text Field. Enter the value 1 for the text by selecting the properties. Do the same for all the buttons by
Step6: – Double click on any of the button. This will take the user to the source section where the major coding of the program is done.
SOURCE CODE:
package simplecalc;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Simplecalc extends Applet implements ActionListener {
public static final String[] TEXT = {"AC", "BSp", "%", "/", "7", "8", "9", "*", "4", "5", "6", "-",
"1", "2", "3", "+", "RSET", "0", ".", "="};
TextField t1;
TextField t2;
StringBuffer value = new StringBuffer(); public void init() {
Frame title = (Frame) this.getParent().getParent(); title.setTitle("Simple Calculator");
setLayout(null);
t1 = new TextField("", 4);
t1.setBounds(50, 10, 200, 50);
add(t1);
t2 = new TextField("", 4);
int k = 1;
Button b[] = new Button[20];
for (int i = 0; i < TEXT.length; i++) { b[i] = new Button("" + TEXT[i]);
b[i].setBounds(x * k, y, 50, 50);
if (k % 4 == 0) { x = 50;
y += 50;
k = 0;
} k++;
add(b[i]); b[i].addActionListener(this);
}
}
public void paint(Graphics g) {
}
public void actionPerformed(ActionEvent e) { String s = e.getActionCommand();
if (s.equalsIgnoreCase("=")) { System.out.println("generating result"); t1.setText(calculate(value)); value.setLength(0);
} else if (s.equalsIgnoreCase("AC")) { System.out.println("AC: Resetting Input"); value.setLength(0);
t2.setText("");
} else if (s.equalsIgnoreCase("BSp")) { System.out.println("BSp: Erasing a character"); value.setLength(value.length() - 1); t2.setText(value.toString());
} else if (s.equalsIgnoreCase("RSET")) { System.out.println("Reset: Resetting Input output "); value.setLength(0);
t1.setText("");
t2.setText("");
} else {
value.append(s); t2.setText(value.toString());
}
}
public String calculate(StringBuffer sb) { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); Object
result = null;
try {
result = engine.eval(sb.toString()); System.out.println(result);
} catch (ScriptException ex) {
System.out.println("Exception : Check again your operator sequence"); result = "Error";
}
return result.toString();
}
}
12.CLIENT AND SERVER (SOCKET)
AIM:
To write a program to send a text message to another system and receive the text message from the system (use socket programming)
ALGORITHM:
Step8: Make a connection using socket class and close the class.
PROGRAM:
import java.net.*;
try
}catch(Exception e){System.out.println(e);}
}
Myclient.java
import java.io.*;
import java.net.*;
//String msg;
catch(Exception e){System.out.println(e);}
OUTPUT: