servlet cookies

Discussions

Web tier: servlets, JSP, Web frameworks: servlet cookies

  1. servlet cookies (1 messages)

    1.how to get all the names at one time using cookies or HttpSession?
    2.the search dialog box present in a different frame does not get refreshed each time the search is over.how to overcome this?
    3.the part of the frame which displays the name also does not get refreshed.what to do?
    ------------------------------------------------------------

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

    public class dnscook extends HttpServlet
    {
      PrintWriter out;
      String vhost_name;
      String ext;
      boolean correct = false;
      String name;
      int i;
      public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
      {
       try
        {
          res.setContentType("text/html");
          out = res.getWriter();
          out.println("<html>");
          out.println("<body>");
          out.println("<form action='http://127.0.0.1:8080/servlet/dnscook'method=POST>");
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con = DriverManager.getConnection("jdbc:odbc:dc");
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from dns");
           vhost_name = req.getParameter("vhost_name");
           ext = req.getParameter("extension");
          while(rs.next())
          {
           if(vhost_name.equals(rs.getString(1)) && ext.equals(rs.getString(2)))
            {
              correct = true;
              break;
             }
           }
            if(correct)
              {
               out.println(vhost_name+ext+" "+"already exists");
               }
             else
              {
               // out.println(" your name "+" "+dname+" "+ext+" "+"is avialable");
                Cookie cookie = new Cookie("Names",vhost_name);
                res.addCookie(cookie);
                out.println(vhost_name+" "+ext);
                correct = false;
                out.flush();
                con.close();
              }
        }catch(Exception e){}
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
     }
    }

    ------------------------------------------------------------
    the source code for html file
    ------------------------------------------------------------

    <html>
    <body>
    <form action= "http://127.0.0.1:8080/servlet/dnscook" name=whois method=POST target=lowerright>
    <center>
    <table border='0' width='66%' bgcolor='pink' cellpadding='0' cellspacing='0'>
    <tr>
       <td width='20%'>
          <p align='center'>
            <font face='CG Times' color='black'size=3>
              <strong>Check Available domain name of your choice</strong></font></td></tr>
             <tr bgcolor='#ffffe2'>
           <td width='50%'><table border='0' bgcolor='#c0c0fff' >
      <tr>
          
            <td width='18%'>
              <p align='right'><font color='#FFFFFF'>
                <strong>www</strong>.</font>
            </td>
         <td width='32%'>
           <INPUT type='text' name=vhost_name size=20>
         </td>
       <td width='25%'>
          <select name='extension' size='1'>
             <option value='All'>All </option>
               <option value='.com'>.com </option>
                <option value='.net'>.net </option>
                <option value='.org'>.org </option>
          </select>
      </td>
         <td width='25%'>
            <INPUT type=submit value='Check it' ></INPUT>
         </td>
     </tr>

    </center>
    </form>
    </body>
    </html>
    ------------------------------------------------------------
    the source code for main main containing frame


    <Frameset cols = "20%%,*" border NORESIZE SCROLLING="NO">
        <Frame name="TOP" src=ex1.html >
      <Frameset rows = "70%,*">
       <Frame name="lowerright" src="b.html">
      <Frame name="lowerright1" src=dosample.html>
      
    </Frameset>
      </Frameset>
    ------------------------------------------------------------

    Threaded Messages (1)

  2. servlet cookies[ Go to top ]

    My friend.

    The error is with the instance variable "correct".
    You better modify your servlet code like the following..

    if(vhost_name.equalsIgnoreCase(rs.getString(1).trim()) && ext.equalsIgnoreCase(rs.getString(2).trim()))
    {
    correct = true;
    break;
    }
    else
       correct = false;
    }


    Don't think that the servlet instance variables are re-initialized every time the servlet is called.

    Servlet will have only one instance during its life cycle.

    Enjoy.

    Thanks.
    rockmania.
    (rockmania hacks java for food)
    (rockmania is a Sun certified Java Programmer)
    (rockmania is a Sun certified Java Developer)
    (rockmania is an IBM certified Solution Developer for WebSphere Advanced Version)
    (rockmania is BEA certified Weblogic server developer)