Hi,
I am trying to get a applet to communicate with my servlet. I am able to receive from the servlet. But the message I am trying to send is not reaching the servlet.
Can someone help? Am I doing something wrong?
I call the applet's Refresh() function.
This is the (applet) code for the Refresh function
-----------------
public void Refresh()
throws MalformedURLException, IOException
{
String cr = "\r\n";
URL url = new URL(getCodeBase(), "/servlet/ExcelServlet");
genExceptions = url.toString();
URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoInput(true );
con.setDoOutput (true);
con.connect();
try
{
OutputStream out = con.getOutputStream();
InputStream in = con.getInputStream();
DataOutputStream outputStream;
outputStream = new DataOutputStream( out );
outputStream.writeUTF( "POST /servlet/ExcelServlet HTTP/1.0" + cr );
outputStream.writeUTF( "Content-Length: 15" + cr + cr );
outputStream.writeUTF( "Hi there How are you" + cr );
DataInputStream textStream;
textStream = new DataInputStream(in);
dispString = textStream.readLine();
}
catch( Exception exception)
{
genExceptions = exception.toString();
}
repaint();
}
-----------------
This is the servlet code snippet
-----------
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
System.out.println( "Get" );
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println( "Myfile.xls");
BufferedReader in = request.getReader();
Enumeration enu = request.getHeaderNames();
while ( enu.hasMoreElements() )
{
String Head = (String) enu.nextElement();
System.out.println( Head );
}
System.out.println( "Excel Servlet Length : " + request.getContentLength() );
}
-----------
-
Applet Servlet Communication... (2 messages)
- Posted by: Manoj Joseph
- Posted on: October 29 2002 10:15 EST
Threaded Messages (2)
- Applet Servlet Communication... by brian chan on October 29 2002 11:50 EST
- Applet Servlet Communication... by Uma Maheswar Kalluru on October 30 2002 11:23 EST
-
Applet Servlet Communication...[ Go to top ]
- Posted by: brian chan
- Posted on: October 29 2002 11:50 EST
- in response to Manoj Joseph
Trap your request in the doGet method of your servlet. Try this code for HTTP POST:
DataOutputStream printout;
BufferedReader rdr;
String u = <your Page>;
URL url = new URL(u);
URLConnection urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new DataOutputStream(urlConn.getOutputStream());
String P = "P1=" + URLEncoder.encode(<val1>) + "&P2=" + URLEncoder.encode(<val2>);
printout.writeBytes(P);
printout.flush();
printout.close();
rdr = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
retVal = "";
while (null != ((str = rdr.readLine()))) {
retVal = retVal + str;
}
rdr.close();
-
Applet Servlet Communication...[ Go to top ]
- Posted by: Uma Maheswar Kalluru
- Posted on: October 30 2002 11:23 EST
- in response to Manoj Joseph