Wednesday, 24 January 2018

Advancedjava-Slip16




Slip16
Q1. Write a JSP program which accept UserName in a TextBox and greets the user
according to the time on server machine.   



greet.html
<html>
<body>
<form action="greets.jsp" method="get">
username: <input type="text" name="uname" />
<br>
<input type="submit" />
</form>
</body>
<html>

greet.jsp
<%@ page language="java" import="java.util.*" %>
<html>
<body>
<%String uname=request.getParameter("uname"); %>
<%="Welcome "+uname+"<br>" %>
<%="Server date/time:"+new java.util.Date() %>
</body>
</html>
                                                      
                                                                       
Q2. Write a program in java which will show lifecycle (creation, sleep, and dead) of a thread. Program should print randomly the name of thread and value of sleep time. The name of the thread should be hard coded through constructor. The sleep time of a thread will be a random integer in the range 0 to 4999.                                                                                          
class MyThread extends Thread{

            public MyThread(String s){
                        super(s);
            }

            public void run(){
                        System.out.println(getName()+" thread created.");
                        while(true){
                                    System.out.println(this);
                                    int s = (int)(Math.random()*5000);
                                    System.out.println(getName()+" is sleeping for "+s+"msec");
                                    try{
                                                Thread.sleep(s);
                                    }catch(Exception e){}
                        }
            }
}

class ThreadLifeCycle{
            public static void main(String args[]){
                        MyThread t1 = new MyThread("archana"),
                                    t2 = new MyThread("adhira");
                        t1.start();
                        t2.start();

                        try{
                                    t1.join();
                                    t2.join();
                        }catch(Exception e){}

                        System.out.println(t1.getName()+" thread dead.");
                        System.out.println(t2.getName()+" thread dead.");
            }
}