hello,

I am trying to access a servlet (named SevletApplet1) from an applet
(named AppletServlet).
I am using  Java Web Server 2.0 and its add. is 144.12.16.8:9090

When i hit at button AddMovies of applet servlet should come in browser
My web server is on my machine only( OS is WindowsNT WS)
Also for development iam using jdk1.2 and jsdk2.0

please helpme.








code for Applet  ---  AppletServlet.java
---------------------------------------------------------------
/*Java Servlet Applet*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;


public class AppletServlet extends Applet implements ActionListener
{

 URLConnection connection;
 List objList;
 Button objButton;

 public void init()
 {
  //create a list
  objList = new List(6,true);

  // create a new button
  objButton = new Button("Add Movies");

  //add items to the objList list.
  objList.add("The Runaway Bride");
  objList.add("Casablanca");
  objList.add("Gone With the Wind");
  objList.add("Ben Hur");
  objList.add("Lawrence of Arabia");
  objList.add("The Mummy");


  //add list to the window.
  add(objList);

  //add the button to the window
  add(objButton);

  //add  action listener to the button
  objButton.addActionListener(this);


 }//init()

 public void actionPerformed(ActionEvent EvtAction)
 {
  //String[] arrMovies;
  String strMovie;
  byte[] baOutBuffer = new byte[1024];

  //Properties args;
  String argString;

  String strActCmd = EvtAction.getActionCommand();

  try
  {
   //button pressed
   if(strActCmd.equals("Add Movies"))
   {

    System.out.println("\nB4 connecting to ServletApplet1");


    //create the URL.
    URL objURL = new
URL("http://144.12.16.8:9090/servlet/ServletApplet1");

    URLConnection objURLConn = objURL.openConnection();

    System.out.println("\n Connection opened to url " + objURL);
    System.out.println("\n Connection opened to url connection " +
objURLConn);



    objURLConn.setDefaultUseCaches(false); //for future connections
    objURLConn.setUseCaches(false); //for this one
    objURLConn.setDoInput(true);
    objURLConn.setDoOutput(true);


    // store the movies selected in a variable.

    //arrMovies = objList.getSelectedItems();

    System.out.println("\n B4 selecting Movie");


    strMovie = objList.getSelectedItem();

    //create an output stream.
    DataOutputStream outStream = new DataOutputStream(new
BufferedOutputStream(
               objURLConn.getOutputStream()));


    System.out.println("\n After creating output stream");


    //convert string to a byte array
    //baOutBuffer = arrMovies[iCnt].getBytes();
    //baOutBuffer = strMovie.getBytes();

    //write to the output stream.
    outStream.writeBytes(strMovie);

    // flush
    outStream.flush();

    System.out.println("\n Data sent to servlet" + strMovie);



    //close the output stream.
    //outStream.close();

   }// button pressed
  }//try()
  catch(Exception excep)
  {
    //ptint the stack trace
    System.out.println(excep);
  }

 }//actionPerformed()



}//ServletApplet class

------------------------------------------------------

code for servlet---  ServletApplet.java
--------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;



public class ServletApplet extends HttpServlet
{

 ServletConfig objConfig ;

 // initialize the applet
 public void init(ServletConfig objConfig)
 {
    // store the servlet config in a local variable.
    this.objConfig = objConfig;

 }//init()


 public void doGet(ServletRequest request, ServletResponse response)
      throws ServletException, IOException
 {

  OutputStream outStream;
  DataInputStream inStream;

  //byte[] baReadBuffer = new byte[1024];
  String strRead;
  int iNum;

  //create the inputstream.
  inStream = new DataInputStream(new
BufferedInputStream(request.getInputStream()));

  // read the data sent by applet.
  strRead = inStream.readUTF();

  /*while((iNum = inStream.read()) != -1)
  {

   baReadBuffer =  inStream.read();
   baReadBuffer++;
  }*/

  //strRead = String(baReadBuffer);

  outStream = response.getOutputStream();

  response.setContentType("text/html");

  System.out.println("Response from Servlet");

  System.out.println(strRead);


  /*outStream.println("<html>");
     outStream.println("<head><title>Session Servlet</title></head>");
     outStream.println("<body>");

     outStream.println("<H2>Response from Servlet </H2>");*/

    //print all the movies selected
    //for(int iCnt=0; iCnt<strMovies.length; iCnt++)
    //{
   //outStream.println(strRead);
    //}

    //outStream.println("</body></html>");
    //outStream.close();
 }//doGet()

 public void doPost(ServletRequest request, ServletResponse response)
      throws ServletException, IOException
 {

  //handle the post request in the same way as get method
  doGet(request,response);
 }//doPost()

}// ServletApplet
-----------------------------------------------------------------------

___________________________________________________________________________
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