Sorry last time i could not send u codes for servlet and applet.
I am trying to access a servlet (named SevletApplet1.java) from an applet (named AppletServlet.java).
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
I am now sending u code of both applet and servlet at the end
of mail.
please helpme.
code for Applet --- AppletServlet.java
---------------------------------------------------------------
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()
{ objList = new List(6,true);//create a list
objButton = new Button("Add Movies");// create a new button
//add items to the objList list.
objList.add("The Runaway Bride");
objList.add("Casablanca");
objList.add("Gone With the Wind");
objList.add("Ben Hur");
add(objList); //add list to the window.
add(objButton); //add the button to the window
objButton.addActionListener(this);//add action listener
}//init()
public void actionPerformed(ActionEvent EvtAction)
{ String strMovie;
byte[] baOutBuffer = new byte[1024]; String argString;//Properties
args
String strActCmd = EvtAction.getActionCommand();
try{
if(strActCmd.equals("Add Movies"))//button pressed
{ System.out.println("\nB4 connecting to ServletApplet1");
URL objURL = new URL("http://144.12.16.8:9090/servlet/ServletApplet1");
URLConnection objURLConn = objURL.openConnection();
objURLConn.setDefaultUseCaches(false); /
objURLConn.setUseCaches(false); //for this one
objURLConn.setDoInput(true);
objURLConn.setDoOutput(true);
strMovie = objList.getSelectedItem();
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(
objURLConn.getOutputStream()));
outStream.writeBytes(strMovie);//write to the output stream.
outStream.flush();
System.out.println("\n Data sent to servlet" + strMovie);
outStream.close();
}// button pressed
}//try()
catch(Exception excep)
{ System.out.println(excep); }
}//actionPerformed()
}//ServletApplet class
----------------------------------------------------------------------------------------------
code for servlet--- ServletApplet1.java
-----------------------------------------------------------------------------------------------
import javax.servlet.*;import javax.servlet.http.*;
import java.util.*;import java.io.*;
public class ServletApplet extends HttpServlet
{ ServletConfig objConfig ;
public void init(ServletConfig objConfig)
{ this.objConfig = objConfig; }//init()
public void doGet(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{ OutputStream outStream; DataInputStream inStream;
String strRead; int iNum;
inStream = new DataInputStream(new
BufferedInputStream(request.getInputStream()));
strRead = inStream.readUTF(); // read the data sent by applet.
outStream = response.getOutputStream();
response.setContentType("text/html");
System.out.println("Response from Servlet");
outStream.println("<html>");
outStream.println("<head><title>Session
Servlet</title></head>");
outStream.println("<body>");
outStream.println("<H2>You have selected
</H2>");
outStream.println(strRead+"<BR>");
outStream.println("</body></html>");
outStream.close();
}//doGet()
public void doPost(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{doGet(request,response);}//doPost()
}// ServletApplet
-----------------------------------------------------------------------
