web page.
// countaccess.html
<html>
<body>
<form action="http://localhost:8080/examples/servlet/countAccess"
method="POST">
<input type="hidden"value='<%=new java.util.Date()%>' name="clientDate">
<input type=submit value= submit>
</form>
</body>
</html>
countAccess.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
publicclass countAccess extends HttpServlet
{
privateint accessCount = 0;
}
catch(Exception e)
{
System.out.println(e);
}
}
}
res.setContentType("text/html");
Date serverDate=new Date();
String clientDateStr = req.getParameter("clientDate");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Write a Java program to read, update & delete records from Student(studno,
name, phone) Database using command line arguments [R-Read, U-Update, D-
Delete]
Eg:- java program name U
This will update & show the contents of the student tables.
import java.sql.*;
public class mysql4
{
public static void main(String[] args) throws Exception
{
Connection con;
ResultSet rs;
Statement t;
PreparedStatement pr;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/chemical","root","")
;
char ch=args[0].charAt(0);
switch(ch)
{
case 'R':
t=con.createStatement();
rs=t.executeQuery("select * from element");
while(rs.next())
{
System.out.println("name="+rs.getString(2));
System.out.println("automic wt="+rs.getInt(1));
System.out.println("symbol="+rs.getString(3));
}
break;
case 'D':
String name=args[1];
t=con.createStatement();
t.executeUpdate("delete from element where name='"+name+"'");
break;
case 'U':
name=args[1];
int awt=Integer.parseInt(args[2]);
String sm=args[3];
t=con.createStatement();
t.executeUpdate("update element set
automicwt="+awt+",chemical_sym='"+sm+"' where name='"+name+"'");
break;
}
}
}
Write a java program to accept the details of Teacher (TId, Name, Address)
from the user and insert it into the Database.(use Awt).
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Employee extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b;
Connection cn;
Statement st;
ResultSet rs;
public Teacher()
{
setLayout(null);
l1=new Label("Tid");
l2=new Label("Name");
l3=new Label("Address");
t1=new TextField();
t2=new TextField();
t3=new TextField();
b=new Button("Save");
l1.setBounds(50,50,100,30);
t1.setBounds(160,50,100,30);
l2.setBounds(50,90,100,30);
t2.setBounds(160,90,100,30);
l3.setBounds(50,130,100,30);
t3.setBounds(160,130,100,30);
b.setBounds(50,170,100,30);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent oe)
{
String str=oe.getActionCommand();
if(str.equals("Save"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:Emp","","");
st =cn.createStatement();
int Tid=Integer.parseInt(t1.getText());
String name=t2.getText();
int address=Integer.parseInt(t3.getText());
String strr="insert into teacher values(" + Tid + " ,'" + name + "'," +
adderss + ")";
int k=st.executeUpdate(strr);
if(k>0)
{
JOptionPane.showMessageDialog(null,"Record Is Added");
}
}
catch(Exception er)
{
System.out.println("Error");
}
}
}
public static void main(String args[])
{
new Employee().show();
}
Write a JSP program that accepts the Mobile phone details from user and
display the details on the next page.
Write a Java program using JSP which accepts string from user and display
only vowel characters on a next page.
import java.io.*;
class q5vowels
{
public static void main(String args[]) throws IOException
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
}
}
Output:
Write a programs that create 2 threads each displaying a message (Pass the
message as a parameter to the constructor). The threads should display the
messages continuously till the user presses ctrl-c. Also display the thread
information as it is running
import java.io.*;
class Ass_seta1 extends Thread
{
String msg="";
Ass_seta1(String msg)
{
this.msg=msg;
}
public void run()
{
try
{ while(true)
{
System.out.println(msg);
Thread.sleep(200);
}
}
catch(Exception e){}
}
}
public class seta1
{
public static void main(String a[])
{
Ass_seta1 t1=new Ass_seta1("Hello............");
t1.start();
}
}
/******************************output***************************
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Create a JSP page, which accepts user name in a text box and greets the user
according to the system time. Example: User name : XYZ
Good morning xyz (if it is morning)
// user.html
<form method='post' action='user.jsp'>
User Name:<input type='text' name='uname'><br>
<input type='submit'><input type='reset'>
</form>
// user.jsp
<%@page import="java.util.*"%>
<%
String name = request.getParameter("uname");
Date d = new Date();
if(d.getHours()<12)
{
%>
Good Morning <%=name%>
<%
}
else if(d.getHours()<16)
{
%>
Good Afternoon <%=name%>
<%
}
else
{
%>
Good Evening <%=name%>
<%
}
%>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(prSt != null) prSt.close();
if(con != null) con.close();
} catch(Exception ex){}
}
}
}
Write a Java program using Servlet which accept color name from user and
change the background color of page.
Change background color using servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
if(c.equals("blue"))
pw.println("<body BGCOLOR=blue>");
if(c.equals("yellow"))
pw.println("<body BGCOLOR=yellow>");
if(c.equals("black"))
pw.println("<body BGCOLOR=black>");
pw.println("<center><h2>The selected color is:"+c+"</h2></center>");;
pw.close();
}
}
HTML code
<HTML>
<HEAD>
<TITLE>
Color Change
</TITLE>
</HEAD>
<BODY>
<FORM method=get
action="http://127.0.0.1:8080/examples/servlets/servlet/ColorServlet">
<CENTER>
Select Color :
<select name="color" id="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="yellow">yellow</option>
<option value="black">Black</option>
</Select>
<input type="submit" value="Submit">
</FORM>
</BODY>
</HTML>
Write a Java Socket program that runs on a server & echo back all the strings
sent by a client. If the client sends a string Exit the server program should
terminate.
import java.sql.*;
import java.io.*;
import javax.sql.*;
class MenuDriven
{
public static void main(String args[])
{
Connection con;
Statement state;
ResultSet rs;
int ch;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
con=DriverManager.getConnection("jdbc:odbc:zzz");
System.out.println("Statement object created");
do
{
System.out.println("\n");
System.out.println("Menu:");
System.out.println("1.Insert Record into the Table");
System.out.println("2.Update The Existing Record.");
System.out.println("3.Display all the Records from the Table");
System.out.println("4.Exit");
System.out.println("Enter your choice: ");
case 2:
System.out.println("Enter Customer Number for the record you wish
to Update: ");
no=Integer.parseInt(br.readLine());
System.out.println("Enter new Name for Existing Customer: ");
name=br.readLine();
System.out.println("Enter new address: ");
add=br.readLine();
sql="update employee set custname=?, address=? where custno=?";
p=con.prepareStatement(sql);
p.setString(1,name);
p.setString(2,add);
p.setInt(3,no);
p.executeUpdate();
System.out.println("Record Updated");
//p.close();
//con.close();
break;
case 3:
state=con.createStatement();
sql="select * from customer";
rs=state.executeQuery(sql);
while(rs.next())
{
System.out.println("\n");
System.out.print("\t" +rs.getInt(1));
System.out.print("\t" +rs.getString(2));
System.out.print("\t" +rs.getInt(3));
}
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid Choice");
break;
}
}
}while(ch!=4);
}catch(Exception e)
{
System.out.println(e);
}
}
}
Write a SDK program to create menu like this: File Edit View Font. Display
proper message on clicking particular menu item
void MyForm::InitializeMenuBar()
{
auto self = As<MyForm>();
auto fileNewMenuItem = make_component<TextMenuItem>(self,L"&New");
auto fileOpenMenuItem = make_component<TextMenuItem>(self,L"&Open");
auto fileSaveMenuItem = make_component<TextMenuItem>(self,L"&Save");
auto fileSeparator = make_component<SeparatorMenuItem>(self);
auto fileExitMenuItem = make_component<TextMenuItem>(self,L"E&xit");
fileSubMenu->Add(fileNewMenuItem);
fileSubMenu->Add(fileOpenMenuItem);
fileSubMenu->Add(fileSaveMenuItem);
fileSubMenu->Add(fileSeparator);
fileSubMenu->Add(fileExitMenuItem);
menuBar->Add(fileSubMenu);
menuBar->Add(editSubMenu);
menuBar->Add(viewSubMenu);
SetMenu(menuBar);
}
Write a client-server program which displays the server machines date and
time on the client machine.
DateClient.java
import java.io.*;
import java.net.*;
class DateClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket(InetAddress.getLocalHost(),5217);
BufferedReader in=new BufferedReader(new
InputStreamReader(soc.getInputStream() ));
System.out.println(in.readLine());
}
}
DateServer.java
import java.net.*;
import java.io.*;
import java.util.*;
class DateServer
{
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(5217);
while(true)
{
System.out.println("Waiting For Connection ...");
Socket soc=s.accept();
DataOutputStream out=new DataOutputStream(soc.getOutputStream());
out.writeBytes("Server Date: " + (new Date()).toString() + "\n");
out.close();
soc.close();
}
}
}
Write a JAVA program which will generate following threads
To display 10 terms of Fibonacci series.
To display 1 to 20 in reverse order
import java.io.*;
class fib extends Thread
{
public void run()
{
try
int a=0,b=1,c=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Limit");
int n=Integer.parseInt(br.readLine());
System.out.println("Fibonacci series:");
while(n>0)
{
System.out.println(c);
a=b;
b=c;
c=a+b;
n=n-1;
}
}
catch(Exception e)
{
}
}
}
class rev extends Thread
{
public void run()
{
try
{
System.out.println("Reverse is");
for(int i=20;i>=1;i--)
System.out.println(i);
}
catch(Exception e)
{
}
}
}
class s16
{
public static void main(String args[])
{
try
{
fib t1=new fib();
t1.start();
t1.sleep(5000);
rev t2=new rev();
t2.start();
}
catch(Exception e)
{
}
}
}
Create an Html page that contains 4 option buttons Java, UNIX, DDBMS,
OOSE and 2 buttons Submit and Reset. When the user clicks on Submit
button the server responds by adding cookie containing the selected Subject
and sends the html page to the client. Program should not allow duplicate
cookie to be written.
index.html
<!doctype html>
<head>
<title>CookiesExample</title>
</head>
<body>
<form method='post' action='servlet/cookies'>
<fieldset style="width:14%; background-color:#ccffcc">
<h2>Select Course</h2> <hr>
<input type='radio' name='course' value='Java'>Java<br>
<input type='radio' name='course' value='UNIX'>UNIX<br>
<input type='radio' name='course' value='MCA'>DBMS<br>
<input type='radio' name='course' value='OOSE '>OOSE<br><br>
<input type='submit'> <input type='reset'><br>
</fieldset>
</form>
</body>
</html>
AddCookie.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AddCookie extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Cookie []c = req.getCookies();
int id=1;
if(c!=null) id = c.length+1;
String value = req.getParameter("course");
Cookie newCookie = new Cookie("course:"+id,value);
res.addCookie(newCookie);
pw.println("<h4>Cookie added with value "+value+"</h4>");
}
web.xml
<web-app>
<servlet>
<servlet-name>AddCookie</servlet-name>
<servlet-class>AddCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddCookie</servlet-name>
<url-pattern>/servlet/cookies</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Write a JSP program to design a Login Screen which checks the username
and password. If valid then the user is allowed to login otherwise it should
display some error message
Login Form :
========================
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Login Page</h1>
<center>
<h2>Signup Details</h2>
<form action="LoginCheck.jsp" method="post">
<br/>Username:<input type="text" name="username">
<br/>Password:<input type="password" name="password">
<br/><input type="submit" value="Submit">
</form>
</center>
</body>
</html>
LoginCheck.jsp
=========================
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
Write a java program to display the Records of Doctor (DNo, DName, Salary)
from the database and display on screen(use AWT).
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;
JFrame f;
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2;
one()
{
f=new JFrame("slp5");
l1=new JLabel("dno");
l2=new JLabel("dname");
l3=new JLabel("salary");
t1=new JTextField(15);
t2=new JTextField(15);
t3=new JTextField(15);
b1=new JButton("Insert ");
b2=new JButton("clear ");
f.setLayout(new FlowLayout());
f.setVisible(true);
f.setSize(300,300);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(b1);
f.add(b2);
}
String name=t2.getText();
int eno=Integer.parseInt(t1.getText());
int sal=Integer.parseInt(t3.getText());
String sql="insert into emp values(";
sql=sql+dno+",'"+name+"',"+sal+")";
System.out.println(sql);
int i=stmt.executeUpdate(sql);
if(i>0)
{
System.out.println("record added ");
}
con.close();
}//try close
catch(Exception a)
{
}
}
if(e.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}//action performed close
}//one close
class slp1
{
public static void main(String args[])
{
one o=new one();
}
}
Write a Servlet program that accepts the age and name from and display
message whether user is eligible for voting or not.
//VoterId.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;
} else {
out.print("<fontcolor='red'> <h1>Sorry "+name+" you have to wait for upto 18
age</h1></font>");
}
out.print("<align=right><img src='images/HARISHKUMAR.jpg' width=100
height=100 >");
}
//web.xml
<web-app>
<servlet>
<servlet-name>hai</servlet-name>
<servlet-class>com.nareshit.voterid.VoterId</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hai</servlet-name>
<url-pattern>/hai</url-pattern>
</servlet-mapping>
</web-app>
//home.html
<!DOCTYPE html>
<html>
<head>
<title>Voter Id Verification</title>
</head>
<body>
<center>
<table>
<form action="./hai" method="get">
<tr>
<td>Name :</td>
<td><input type="text" name="vname" /></td>
</tr>
<tr>
<td>Age :</td>
<td><input type="text" name="vage" /></td>
<tr><td><center><input type="submit" value="Submit"></center></td></tr>
</form>
</table>
<center>
</body>
</html>
Write a JAVA program to accept the details of Doctor (Dno, DName, Salary)
from the user and insert it into the database. (Use PreparedStatement class).
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class cre
{
public static void main(String []args)
{
Frame f=new Frame();
Label label1=new Label("D No:");
Label label2=new Label("DName:");
Label label3=new Label("Salary:");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:my1dsn.dsn");
String sql="insert into doctor(dno,dname,salary)values(?,?,?)";
PreparedStatement pst=con.prepareStatement(sql);
pst.setInt(1,v1);
pst.setString(2,v2);
pst.setInt(3,v3);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Data is inserted successfully");
}
catch(Exception ex)
{ System.out.println(ex);
}}});
Panel p=new Panel(new GridLayout(5,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(label3);
p.add(text3);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
ICONDEMO.C
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
switch (message)
{
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
cxIcon = GetSystemMetrics (SM_CXICON) ;
cyIcon = GetSystemMetrics (SM_CYICON) ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
Write a Java Socket program that runs on a server & echoe back all the
strings sent by a client. If the client sends a string Exit the server program
should terminate.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
Client java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
import java.lang.String.*;
Write a JAVA program which will create two child threads by implementing
Runnable interface, one thread will print even nos. from 1 to 50 and other
display odd nos.
import java.io.*;
import java.util.*;
t2=new Thread(this,"Odd");
System.out.println("ODD NUMBER'S ARE");
t2.start();
}
public void run()
{
try
{
for(i=2;i<=50;i=i+2)
{
System.out.print(" " + "\t" +i);
}
System.out.println();
for(j=1;j<=50;j=j+2)
{
System.out.print(" " + "\t" +j);
}
System.out.println();
}
catch(Exception oo)
{
System.out.println(oo);
}
}
}
class OddEven
{
public static void main(String args[])
{
try
{
EvenOdd s=new EvenOdd();
}
catch(Exception obj)
{
System.out.println(obj);
}
Write a java program to accept the details of Product (Pno, PName, Price,
qty) from the user and insert it into the Database.(use Awt).
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public Product()
{
setLayout(null);
l1=new Label("P_No");
l2=new Label("P_Name");
l3=new Label("P_Price");
l4=new Label("P_Qty");
t1=new TextField();
t2=new TextField();
t3=new TextField();
t4=new TextField();
b=new Button("Save");
l1.setBounds(50,50,100,30);
t1.setBounds(160,50,100,30);
l2.setBounds(50,90,100,30);
t2.setBounds(160,90,100,30);
l3.setBounds(50,130,100,30);
t3.setBounds(160,130,100,30);
l4.setBounds(50,170,100,30);
t4.setBounds(160,170,100,30);
b.setBounds(50,210,100,30);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
int p_no=Integer.parseInt(t1.getText());
String p_name=t2.getText();
int p_price=Integer.parseInt(t3.getText());
int p_qty=Integer.parseInt(t4.getText());
String strr="insert into emp values(" + p_no + " ,'" + p_name + "'," +
p_price + " , " + p_qty + ")";
int k=st.executeUpdate(strr);
if(k>0)
{
JOptionPane.showMessageDialog(null,"Record Is Added");
}
}
catch(Exception er)
{
System.out.println("Error");
}
}
}
public static void main(String args[])
{
new Product().show();
}
}
Write a JSP program that accepts the registration details from user and
display the details on the next page.
ACTION:
Write a client-server program which displays the server machines date and
time on the client machine.
DateClient.java
import java.io.*;
import java.net.*;
class DateClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket(InetAddress.getLocalHost(),5217);
BufferedReader in=new BufferedReader(new
InputStreamReader(soc.getInputStream() ));
System.out.println(in.readLine());
}
}
DateServer.java
import java.net.*;
import java.io.*;
import java.util.*;
class DateServer
{
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(5217);
while(true)
{
System.out.println("Waiting For Connection ...");
Socket soc=s.accept();
DataOutputStream out=new
DataOutputStream(soc.getOutputStream());
out.writeBytes("Server Date: " + (new Date()).toString() + "\n");
out.close();
soc.close();
}
}
}
Output:
First compile the client code on console and then compile the server code on
different console.
Run the server code, after that client code. Now the client console shows the
time and date of server machine.
Server Date: Fri Apr 16 17:05:42 IST 2017
Write a Java program to display Movie(moviename,actorname,release-year)
details specified by the user using jsp
ACTION:
ACTION:
int no=Integer.parseInt(request.getParameter("no"));
if ( no < 0 )
out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= no ; c++ )
fact = fact*c;
ACTION:
switch(digit){
case 0: out.print("zero "); break;
case 1: out.print("one "); break;
case 2: out.print("two "); break;
case 3: out.print("three "); break;
case 4: out.print("four "); break;
case 5: out.print("five "); break;
case 6: out.print("six "); break;
case 7: out.print("seven "); break;
case 8: out.print("eight "); break;
case 9: out.print("nine "); break;
}
}
%>
</body>
</html>