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();
}
}
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 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++;
}
}