Hi,
 I have a applet and servlet in browser.
 When I click applet's OK button, I want to send info from applet to
 servlet and also reload my servlet.
 When I tried, it executing the code but not refreshing.
 It it will be great if u tell ans.
 If u know anyother way or URL let me know.
 I included the code also.
 Thanx.

-----------------------------------------------------------
MainRefresh.html

<html>
<head><title>Main Page </title></head>
<frameset rows="28%,72%">
    <frame src="TestRefreshApplet.html" name="index" marginwidth="1"
marginheight="1" scrolling="no">
    <frame src="startRefreshServlet.html" name="index" marginwidth="1"

marginheight="1">
    <noframes>
 To view this web page, you must use Internet Explorer 3.0 or greater.
    </noframes>
</frameset></html>
---------------------------------------------------------------
TestRefreshApplet.html
<HTML><HEAD>
<TITLE>HTML Test Page</TITLE></HEAD>
<BODY>
<APPLET
  CODE     = "TestRefreshApplet.class"
  CODEBASE = "."   WIDTH=400 HEIGHT=300  HSPACE=0 VSPACE=0 ALIGN=middle>
</APPLET></BODY></HTML>
---------------------------------------------------------------------
startRefreshServlet.html
<HTML><HEAD><TITLE>Login Area</TITLE></HEAD>
<BODY BGCOLOR="#FFCC99">
<center>
<h1>Login</h1>
<form name="frm_Login" method="get" action="http://mymachine/servlet/RefreshServlet">
UserId: <Input type="text" name="userId" size="20"><br>
User name:<Input type="text" name="userName" size="20" ><br>
<input type="submit" name="btn_Submit" value="Submit">
</form>
</center></Body></html>
-------------------------------------------------------------------------
RefreshServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class RefreshServlet extends HttpServlet {
  private static int pageNum = 0;
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws 
ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = new PrintWriter(res.getOutputStream());
    out.println("<html>");
    out.println("<head><title> doGet </title><head>");
    out.println("<body>");
    pageNum++;
    out.println("U r visiting page number " + pageNum);
    out.println("<br> Thanx ...");
    out.println("</body> </html>");
    out.close();
    System.out.println("RefreshServlet: doGet() pagenum="+pageNum);
  } // doGet()

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = new PrintWriter (response.getOutputStream());
    System.out.println("RefreshServlet: doPost");
    try {
      ObjectInputStream in = new ObjectInputStream(request.getInputStream());
      String str = (String)in.readObject();
      System.out.println("doPost: response from applet="+str);
      in.close();
    } catch(Exception e) { System.out.println("Exception ="+e); }
//    response.sendRedirect("http://95.33.13.30/servlet/RefreshServlet);
//    doGet(request, response);
    out.println("<html>");
    out.println("<head><title> doPost </title><head>");
    out.println("<body>");
    pageNum++;
    out.println("U r visiting page number " + pageNum);
    out.println("<br> Thanx ...");
    out.println("</body> </html>");
    System.out.println("Redirected");
    out.close();
  } // doPost()
}
-----------------------------------------------------------------------
TestRefreshApplet.html
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class TestRefreshApplet extends Applet implements Runnable {
  public static boolean staticVar = false;
  Thread thread;
  Button ok;
  //Construct the applet
  public TestRefreshApplet() {
    System.out.println("TestRefreshApplet: constructor");
    this.setLayout(new BorderLayout());
    ok = new Button("OK");
    ok.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        System.out.println("ok.");
        staticVar = true;
      }
    });
    this.add(ok, BorderLayout.NORTH);
    show();
  } // constructor

   public void start() {
    System.out.println("TestRefreshApplet: start()");
    if ( thread == null ) {
      thread = new Thread(this);
      thread.start();
    } // if
  } // start()

  public void stop() {
    if ( thread != null ) {
      thread.stop();
      thread = null;
    } // if
  } // stop()

  public void run() {
   System.out.println("TestRefreshApplet:  Run()");
    while ( true ) {
    try {
      if ( staticVar == true ) {
        System.out.println("TestRefreshApplet: var is true. so start refresh");
        String urlStr = "http://mymachine:80/servlet/RefreshServlet";
        URL servletURL = new URL(urlStr);
        System.out.println("File="+servletURL.getFile());
        System.out.println("Host="+servletURL.getHost());
        System.out.println("Port="+servletURL.getPort());
        System.out.println("Protocol"+servletURL.getProtocol());
        String str = 
servletURL.getProtocol()+"://"+servletURL.getHost()+":"+servletURL.getPort()+servletURL.getFile();
        System.out.println("URL="+str);
        URL u = new URL(str);
        URLConnection servletConn = u.openConnection();
        servletConn.setDoOutput(true);
        servletConn.setDoInput(true);
        servletConn.setUseCaches(false);
        servletConn.setDefaultUseCaches(false);
        servletConn.setRequestProperty("Content-Type", "application/octet-stream");
        ObjectOutputStream out = new ObjectOutputStream(servletConn.getOutputStream());
        out.writeObject("Hai from applet to servlet");
        out.flush();
        out.close();
        staticVar = false;
        System.out.println("TestRefreshApplet: Sent to Servlet");
      } // if
      } catch(MalformedURLException me) {
        System.out.println("Ex="+me.getMessage()+"\ne="+me);
        staticVar = false;}
      catch(IOException ioe) {
        System.out.println("Ex=="+ioe.getMessage()+"\n e=="+ioe);
        staticVar = false; }
      try { Thread.sleep(2000); } catch(InterruptedException io) { }
    } // while
  } // run()
} // End Of Class TestRefreshApplet
------------------------------------------------------------------------

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to