Wednesday, 24 January 2018

Advancedjava-Slip8



Slip8
Q1. Write a Multithreading program using Runnable interface to blink Text on the frame. 

import java.awt.*;
import java.awt.event.*;
public class BlinkText extends Frame implements Runnable
{
            Thread t;
            Label l1;
            int f;
            public BlinkText()
            {
                        t=new Thread(this);
                        t.start();
                        setLayout(null);
                        l1=new Label("Hi");
                        l1.setBounds(100,100,100,40);
                        add(l1);
                        setSize(300,300);
                        setVisible(true);
                        f=0;
            }
            public void run()
            {
                        try
                        {
                                    if(f==0)
                                    {
                                                t.sleep(200);
                                                l1.setText("");
                                                f=1;
                                    }
                                    if(f==1)
                                    {
                                                t.sleep(200);
                                                l1.setText("Hello Java");
                                                f=0;
                                    }
                        }catch(Exception e)
                        {
                                    System.out.println(e);
                        }
                        run();
            }
            public static void main(String args[])
            {
                        new BlinkText();
            }
}

  Q2. Write a SERVLET application to accept username and password, search them into database, if found then display appropriate message on the browser otherwise display error message. 


<html>
<head>
<title>New Page 1</title>
</head>
<body>

<form method="POST"  action="http://localhost:8080/loginslip8">



  <p>Enter Username: <input type="text"   name="uname" size="20"></p>
  <p>Enter  Password: <input type="text" name="pass" size="20"></p>
  <p>  <input type="submit" value="Submit" name="B1"></p>
</form>

</body>

</html>

loginslip8.java

import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class loginslip8 extends HttpServlet
{

  public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
  response.setContentType("text/html");

  PrintWriter pw = response.getWriter();

 
  Connection conn;
  try
{
  String uname = request.getParameter("uname");
  String pass = request.getParameter("pass");
  //pw.println( modelno);
  //pw.println(mobilename);


   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 conn = DriverManager.getConnection("jdbc:odbc:mydsn.dsn");

  //PreparedStatement pst = conn.prepareStatement  ("insert into mobile values(?,?)");
  String query = "select * from login where username='"+uname+"' and password='"+pass+"'";
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(query);
 
  if(rs.next())
  {
            pw.println("<br>Welcome ..."+uname); 
  } else {
              pw.println("<br>Sorry, Please enter correct credentials");
  }

  }
  catch (Exception e){
  pw.println(e);
  }
  }