Wednesday, 24 January 2018

Advancedjava-Slip2



Slip2
Q1. Write a multithreading program in java to display all the vowels from a given String.(Use Thread Class)           

import java.io.*;

class Vowel extends Thread
{
DataInputStream dis=new DataInputStream(System.in);
public void run()
{
try
{
System.out.println("Please enter a word :");
String s=dis.readLine();

int len=s.length();
System.out.println("Vowels are as follows:");

for(int i=0;i<len;i++)
{
if(s.charAt(i)=='A' || s.charAt(i)=='a' ||s.charAt(i)=='E' ||s.charAt(i)=='e' ||s.charAt(i)=='I' ||s.charAt(i)=='i' ||s.charAt(i)=='o' ||s.charAt(i)=='O' ||s.charAt(i)=='U' ||s.charAt(i)=='u')
System.out.println(s.charAt(i));
}
}

catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
Vowel p=new Vowel();
p.start();
}
}
                                                                                                           
Q2. Write a SERVLET program which counts how many times a user has visited a web page. If user is visiting the page for the first time, display a welcome message. If the user is revisiting the page, display the number of times visited. (Use Cookie)  


import javax.servlet.*;
import javax.servlet.http.*;
 
public class HitCountServlet extends HttpServlet{
               public void doGet(HttpServletRequest req, HttpServletResponse res)
               throws ServletException,IOException{
                               res.setContentType("text/html");
 
                               PrintWriter out = res.getWriter();
 
                               int count=1;
 
                               Cookie c[] = req.getCookies();
 
                               if(c==null)
                               {
                                              Cookie newCookie = new Cookie("count","1");
                                              res.addCookie(newCookie);
                                              
                                              out.print("<h1>Welcome Servlet</h1>");
                               }
                               else
                               {
                                              count = Integer.parseInt(c[0].getValue())+1;
                                              c[0].setValue(Integer.toString(count));
                                              res.addCookie(c[0]);
                               }
 
                               out.print("Hit Count:"+count);
               }
}

          

Another way

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HitCountServlet extends HttpServlet 
{
    static int i=1;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String k=String.valueOf(i);
        Cookie c = new Cookie("visit",k);
        response.addCookie(c);
        int j=Integer.parseInt(c.getValue());
        if(j==1)
        {
            out.println("Welcome");
        }
        else
        {
            out.println("You visited "+i+" times");
        }
                i++;                        
    }
}