Advance Lab
Advance Lab
CODE:
import java.io.*;
import java.net.*;
Thread t;
ThreadDemo(String str)
t.start();
System.out.println(Thread.currentThread().getName() + "Executing.");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"Finished Executing.");
OUTPUT:
2 . Write a set of two JAVA programs for communicating between them using
socket & datagram programming.
CODE:
import java.io.*;
import java.net.*;
while(true)
import java.io.*;
import java.net.*;
while(true)
System.out.println(receiveMessage);
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
OUTPUT:
3.Write a JAVA Servlet Program to implement and demonstrate get()
and post() methods(Using HTTP Servlet Class).
CODE:
FOR GET
<html>
<body>
<form action="login" method="get">
<table>
<tr>
<td>User</td>
<td><input name="user" /></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" /></td>
</tr>
</table>
<input type="submit" />
</form>
</body>
</html>
package com.edu4java.servlets;
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;
FOR POST
<html>
<body>
<form action="login" method="post">
<table>
<tr>
<td>User</td>
<td><input name="user" /></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" /></td>
</tr>
</table>
<input type="submit" />
</form>
</body>
</html>
package com.edu4java.servlets;
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;
Login page
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
Sample login Example (try with username as "admin" and password as "admin" without quart ) <br> <br>
</form>
</body>
</html>
web.xml mapping
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.candidjava.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>
</web-app>
Login controller
package com.candidjava;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*/
String un=request.getParameter("username");
String pw=request.getParameter("password");
response.sendRedirect("success.html");
return;
else
response.sendRedirect("error.html");
return;
}
success page
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
Login success
WELCOME
</body>
</html>
error page
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
</body>
</html>
OUTPUT:
5 . Write a JDBC Program to insert data into Student DATABASE and
retrieve info based on particular queries.
CODE:
Create a Table:
CREATE TABLE studid(
fullname varchar2(50),
email varchar2(50)
);
// Establishing Connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
if (con != null)
System.out.println("Connected");
else
System.out.println("Not Connected");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
Connected
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
Successfully Registered
// Updating database
String q1 = "UPDATE studid set pwd = '" + newPwd +
"' WHERE id = '" +id+ "' AND pwd = '" + pwd + "'";
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("Password Successfully Updated");
else
System.out.println("ERROR OCCURED :(");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
Password Successfully Updated
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("One User Successfully Deleted");
else
System.out.println("ERROR OCCURED :(");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
One User Successfully Deleted
// SELECT query
String q1 = "select * from studid WHERE id = '" + id +
"' AND pwd = '" + pwd + "'";
ResultSet rs = stmt.executeQuery(q1);
if (rs.next())
{
System.out.println("User-Id : " + rs.getString(1));
System.out.println("Full Name :" + rs.getString(3));
System.out.println("E-mail :" + rs.getString(4));
}
else
{
System.out.println("No such user id is already registered");
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
User-Id : id1
E-mail :[email protected]
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("studid");
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
<tr>
</tr>
<tr bgcolor="#A52A2A">
<td><b>id</b></td>
<td><b>user_id</b></td>
<td><b>Password</b></td>
<td><b>Name</b></td>
<td><b>Email</b></td>
</tr>
<%
try{
statement=connection.createStatement();
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr bgcolor="#DEB887">
<td><%=resultSet.getString("id") %></td>
<td><%=resultSet.getString("user_id") %></td>
<td><%=resultSet.getString("password") %></td>
<td><%=resultSet.getString("name") %></td>
<td><%=resultSet.getString("email") %></td>
</tr>
<%
} catch (Exception e) {
e.printStackTrace();
%>
</table>
OUTPUT:
import java.rmi.*;
public interface rint extends Remote // rint is the interface name
{
double fact(double x) throws RemoteException; // fact() is the method declaration only
}
import java.rmi.*;
import java.rmi.server.*;
public class rimp extends UnicastRemoteObject implements rint // rimp is implementation name
{
{}
import java.rmi.*;
import java.net.*;
try {
Naming.rebind("rser", ri);
} catch (Exception e) {
System.out.println(e);
import java.rmi.*;
try {
double s = rr.fact(5);
System.out.println(e);
OUTPUT:
8 . Develop a JAVA SWING program to design a calculator.
CODE:
package ptunes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
//import java.util.ActionEvent;
public gui() {
Container c = j.getContentPane();
//c.setLayout(new BorderLayout());
p1.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(4,4,4,4));
t.setFont(myFontSize);
c.add(t,BorderLayout.NORTH);
n1.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n2.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
}
});
n3.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n4.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n5.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
global = global.concat(num1);
t.setText(global);
});
n7.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n8.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n9.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n10.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n11.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n12.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n13.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n14.addActionListener(new ActionListener(){
global = global.concat(num1);
t.setText(global);
});
n15.addActionListener(new ActionListener(){
//global = global.concat(num1);
ScriptEngineManager mgr = new ScriptEngineManager();
try {
String s = engine.eval(global).toString();
t.setText(s);
e1.printStackTrace();
});
n16.addActionListener(new ActionListener(){
global = null;
t.setText(global);
});
p1.add(n1);
p1.add(n2);
p1.add(n3);
p1.add(n4);
p1.add(n5);
p1.add(n6);
p1.add(n7);
p1.add(n8);
p1.add(n9);
p1.add(n10);
p1.add(n11);
p1.add(n12);
p1.add(n13);
p1.add(n14);
p1.add(n15);
p1.add(n16);
c.add(p1,BorderLayout.CENTER);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(400,400);
j.setVisible(true);
OUTPUT:
<html>
<head>
<body bgcolor="cyan">
<br>
<br>
</center>
</form>
</body>
</html>
exam.jsp
<html>
<head>
<title>Examination Panel</title>
</head>
<body bgcolor="cyan">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
st = cn.createStatement();
rs = st.executeQuery( sSql );
String s1,s2,s3,s4;
int i=1;
while(rs.next())
s1 = rs.getString(2);
s2 = rs.getString(3);
s3 = rs.getString(4);
s4 = rs.getString(5);
i ;
/*int n = rsmd.getColumnCount();
while( rs.next() )
out.println( "</tr><tr>" );
finally {
%>
</body>
</html>
report.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exam Report</title>
</head>
<body bgcolor="cyan">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
st = cn.createStatement();
rs = st.executeQuery( sSql );
String s1,s2,s3,s4;
int i=1;
int correct=0,incorrect=0,total=0;
while(rs.next())
total ;
s1 = rs.getString(1);
s2 = request.getParameter("opt" i);
s3 = rs.getString(6);
if(s2.equals(s3))
s4="Correct";
correct ;
else
s4="Incorrect";
incorrect ;
i ;
finally {
%>
</body>
</html>
OUTPUT: