Pages

Monday, 7 March 2011

Sem 2 JAVA Servlet


/***************************************************************
NAME     :
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :

Write a servlet which accepts the user name from an HTML page and returns a message which greets the user.
***************************************************************/

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

 class seta1 extends HttpServlet
 {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      try
       {
        PrintWriter out = res.getWriter();
        String s1 =req.getParameter("txtname");
        out.println("<center>"+s1);      
        out.println("<center> WELCOME TO SERVLET PROGRAMING <br>");
      
       }
       catch(Exception e)
       {
         e.printStackTrace();

       }
    }
 }
****************************************************************************************

/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Design a servlet 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.*;

 class seta2 extends HttpServlet
 {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      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();

       }
    }
 }
***********************************************************************************************


/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Write a servlet which counts how many times a user has visited a web page. If the user is visiting the page for the first time, display a welcome message. If the user is re- visiting the page, display the number of times visited. (Use cookies)
***************************************************************/

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


 class firstcook extends HttpServlet {


    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      
        PrintWriter out = null;
      try
       {   
        out = res.getWriter();
         
        Cookie c[]=req.getCookies();
        int i;
       //to search the cookie in the existig cookie
 
       for( i=0;i<c.length;i++)
       {
         String s=c[i].getName();
         if(s.equals("count1"))
        {
         String s1=c[i].getValue();
         int n=Integer.parseInt(s1)+1;
         out.println("page is visited for"+n);
         Cookie c2=new Cookie("count",Integer.toString(n));
          res.addCookie(c2);
          break;
        }
       }
  
       //if cookie not found
        
        if(i==c.length)
       {
        Cookie c1=new Cookie("count","1");
         res.addCookie(c1);
         out.println("welcome");
       }
       }
       catch(Exception e)
       {
         e.printStackTrace();

       }
    }
 }
*************************************************************************************
/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Create an HTML page with text boxes for login id and password. Pass these to the servlet. If the login name = “BSc” and password = “CS”, display welcome.html else display an error.html  (hint: Return the <a href=http://server- p:8080/Welcome.html> tag to the client machine)
***************************************************************/

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


 class seta4 extends HttpServlet
 {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      try
       {  
        PrintWriter out = res.getWriter();
        String s1=req.getParameter("txtname");        
        String s2=req.getParameter("txtpass"); 
         if(s1=="BSC" && s2=="CS")
        {
          //out.println("<a href=http://localhost:8080/dipa/welcome.html>next </a>");
         res.sendRedirect("welcome.html");
        }
         else
        {
         //out.println("<a href=http://localhost:8080/dipa/error.html>next </a>");
        res.sendRedirect("error.html");  
        }
        }catch(Exception e)
       {
         e.printStackTrace();

       }
    }
 }
*********************************************************************************************

/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Write a program  to  create  a hopping  mall. User  must be allowed to do purchase from two pages. Each page should have a page total. The third page should display a bill, which consists of a page total of what ever the purchase has been done and print the total. (Use HttpSession)
***************************************************************/

//form1.html

                                            
<html>
<body>
<form action="http://localhost:8080/nitish/p1" method=get>
 <table border=1>
        <tr><th>Iteam name </th><th>Price</th><th>qty </th></tr>
          <tr>
          <td><input type=text name=txtname value=Pond's /></td>
          <td><input type=text name=txtunit value=70 /></td>
          <td><input type=text name=txtqty /></td>
         </tr>
          <tr>
         <td><input type=text name=txtname value=Shampo /></td>
         <td><input type=text name=txtunit value=80 /></td>
          <td><input type=text name=txtqty /></td>
         </tr>
         <tr>
          <td><input type=text name=txtname value=Camera /></td>
          <td><input type=text name=txtunit value=102 /></td>
          <td><input type=text name=txtqty /></td>
          </tr>
          </table>
         <br><input type=submit name=NEXT />
         <br><input type=reset name=RESET />
          </form>
</body>
</html>                                


//page1.java

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


 class page1 extends HttpServlet
 {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
       
        PrintWriter out = null; 
        try
       { 
        out=res.getWriter();
         HttpSession htpses=req.getSession(true);
         String name[]=req.getParameterValues("txtname");
         String price[]=req.getParameterValues("txtunit");
         String qty[]=req.getParameterValues("txtqty");
         int sum=0;
        for(int i=0;i<name.length;i++)
        {                             
         int cost=Integer.parseInt(price[i])*Integer.parseInt(qty[i]);
           sum+=cost;
         } 
         out.println("Last Page Total="+sum);
         htpses.setAttribute("page1",Integer.toString(sum));
         //second html page
          res.setContentType("text/html");
          out.println("<form method=get action='http://localhost:8080/dipa/p2' >");
          out.println("<table border=1>");
          out.println("<tr><th>Iteam name </th><th>Price</th><th>qty </th></tr>");
          out.println("<tr>");
          out.println("<td><input type=text name=txtname value=Toothbrush /></td>");
          out.println("<td><input type=text name=txtunit value=10 /></td>");
          out.println("<td><input type=text name=txtqty /></td>");
          out.println("</tr>");
          out.println("<tr>");
          out.println("<td><input type=text name=txtname value=Towel /></td>");
          out.println("<td><input type=text name=txtunit value=70 /></td>");
          out.println("<td><input type=text name=txtqty /></td>");
          out.println("</tr>");
          out.println("<tr>");
          out.println("<td><input type=text name=txtname value=Shoes /></td>");
          out.println("<td><input type=text name=txtunit value=500 /></td>");
          out.println("<td><input type=text name=txtqty /></td>");
          out.println("</tr>");
          out.println("</table>");
          out.println("<br><input type=submit name=NEXT />");
          out.println("<br><input type=reset name=RESET />"); 
          out.println("</form>");
                          
        }catch(Exception e)                      
       {
         out.println("error="+e);

       }
     }
   }



//page2.java

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


 class page2 extends HttpServlet
 {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
       
        PrintWriter out = null; 
        try
       { 
        out=res.getWriter();
         HttpSession htpses=req.getSession(true);
         String name[]=req.getParameterValues("txtname");
         String price[]=req.getParameterValues("txtunit");
         String qty[]=req.getParameterValues("txtqty");
         int sum=0;
        for(int i=0;i<name.length;i++)
        {
         int cost=Integer.parseInt(price[i])*Integer.parseInt(qty[i]);
         htpses.setAttribute(name[i],Integer.toString(cost));  
          sum+=cost;
         } 
          String s=(String)htpses.getAttribute("page1");
          int total=sum+Integer.parseInt(s);
         out.println("Total Bill="+total);
          
        }catch(Exception e)
       {
         out.println("error="+e);

       }
     }
   }


//Setb1.xml

  <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <web-app>
- <servlet>
  <servlet-name>SETB1</servlet-name> 
  <servlet-class>page1</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>SETB1</servlet-name> 
  <url-pattern>/p1</url-pattern> 
  </servlet-mapping>
- <servlet>
  <servlet-name>SETB11</servlet-name> 
  <servlet-class>page2</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>SETB11</servlet-name> 
  <url-pattern>/p2</url-pattern> 
  </servlet-mapping>
  </web-app>
*****************************************************************************************

/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :

Design an HTML page containing 4 option buttons (Painting, Drawing, Singing and swimming) and 2 buttons reset and submit. When the user clicks submit, the server responds by adding a cookie containing the selected hobby and sends a message back to 
the client. Program should not allow duplicate cookies to be written.
***************************************************************/

//Setb2.html

<html>
<body>
<form method=get action="http://localhost:8080/dipa/choice">
select your hobbie:<br>
<input type=radio name=ch value=painting>PAINTING<br>
<input type=radio name=ch value=drawing>DRAWING<br>
<input type=radio name=ch value=swiming>SWIMING<br>
<input type=radio name=ch value=singing>SINGING<br>
<br><input type=submit value=select>
<br><input type=reset>
</form>
</body>
</html>    


//Setb21.java

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


 class setb21 extends HttpServlet {


    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      
        PrintWriter out = null;
      try
       {   
         out=res.getWriter();
         String hb=req.getParameter("ch");
         Cookie c1=new Cookie("hobby",hb);
         out.println("hobby are:"+hb);
         //res.addCookie(c1);
       }
       catch(Exception e)
       {
         e.printStackTrace();

       }
    }
 }



//Setb2n.java

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


 class setb2 extends HttpServlet {


    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
    {
      
        PrintWriter out = null;
      try
       {   
        out=res.getWriter();
        Cookie c[]=req.getCookies();
        for(int i=0;i<c.length;i++)
        {
         String s1=c[i].getName();
         out.println("selected hobby are :"+s1);  
        }
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
    }
 }


//Setb2.xml

  <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <web-app>
- <servlet>
  <servlet-name>SETB2</servlet-name> 
  <servlet-class>setb2</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>SETB2</servlet-name> 
  <url-pattern>/choice</url-pattern> 
  </servlet-mapping>
  </web-app>


9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  5. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
    python training in tambaram
    python training in annanagar
    python training in jayanagar

    ReplyDelete
  6. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete