Wednesday, 24 January 2018

Advancedjava-Slip18



Slip18

 Q1. Write a java program using multithreading to execute the threads sequentially.

      (Use Synchronized Method)

                                                                                              

class DemoT extends Thread

 {

            DemoT1 t;

    String name;  

    public DemoT(String s,DemoT1 t1)

    {

            name=s;

            start();

            t=new DemoT1();

            t1=t;

    }

            public void run()

            {

                        t.Display(name);

            }

 

}

class DemoT1

{

            synchronized void Display(String name1)

            {

                        try

                        {

                      

                        for(int i=1;i<=5;i++)

                                    {

                                              

                                                System.out.println(name1+"  "+i);

                                    }

                        }catch(Exception e)

                                    {

                                    }

            }

          

}

public class DemoThread

{

            public static void main(String[] args)

    {

            DemoT1 td=new DemoT1();

       DemoT  d=new DemoT("FIRST ",td);

       DemoT d1=new DemoT("SECOND",td);

       DemoT d2=new DemoT("THIRD ",td);

    }

}

 

Q2.  Write a SERVLET program that provides information about a HTTP request from a client, such as IP address and browser type. The servlet also provides information about the server on which the servlet is running, such as the operating system type, and the names of currently loaded servlets.      
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.*;

  public class info_check extends HttpServlet

 {

    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException

    {

      try

       { 

        res.setContentType("text/html");

        PrintWriter out = res.getWriter();

        out.println("<html>");       

        out.println("<body>");

        java.util.Properties p=System.getProperties();

        out.println("Server Name :"+req.getServerName()+"<br>");       

        out.println("Operating System Name :"+p.getProperty("Linux")+"<br>");

        out.println("IP Address :"+req.getRemoteAddr()+"<br>");       

        out.println("Remote User:"+req.getRemoteUser()+"<br>");

        out.println("Remote Host:"+req.getRemoteHost()+"<br>");

        out.println("Local Name :"+req.getLocalName()+"<br>");        

        out.println("Server Port:"+req.getServerPort()+"<br>");       

        out.println("Servlet Name :"+this.getServletName()+"<br>");

        out.println("Protocol Name :"+req.getProtocol()+"<br>");    

        out.println("</html>");                  

        out.println("</body>");

       }

       catch(Exception e)

       {

         e.printStackTrace();

        }

    }

 }