The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet to servlet communication

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
jtiendra taunk

Posts: 1
Nickname: jitendra
Registered: Sep, 2002

Applet to servlet communication Posted: Sep 7, 2002 4:33 AM
Reply to this message Reply
Advertisement
Hi,
we are trying to communicate between an applet and a servlet.
but the servlet is not able to process the data posted by the applet.
what could be the reason.
please help :((
--jits

####### query applet code
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class QueryApplet extends Applet implements ActionListener
{
TextField t_query ;
TextArea t_results;
Button btn_execute;

public void init()
{
Panel dbpanel = new Panel();
dbpanel.setLayout(new FlowLayout(FlowLayout.LEFT));
dbpanel.add(new Label("Query String:"));
t_query = new TextField("hello",50);
dbpanel.add(t_query);
btn_execute = new Button("Execute Query");
btn_execute.addActionListener(this);
dbpanel.add(btn_execute);
add("No rth", dbpanel);
t_results = new TextArea(10,80);
add("Center",t_results);
}
public void exec_query()
{
String qryString = t_query.getText();
try
{
URL url = new URL("http://localhost:8080/servlet/QueryServlet");
String query = URLEncoder.encode("query") + "=" + URLEncoder.encode(qryString);
URLConnection ucon = url.openConnection();
ucon.setDoOutput(true);
ucon.setDoInput(true);
ucon.setUs eCaches(false);
ucon.setRequestProperty("Content-Type","application/x-www-form-u rlencoded");
DataOutputStream outs = new DataOutputStream(ucon.getOutputStream());
outs.writeBytes(query);
outs.flush();
outs.close();
InputStreamReader ins = new InputStreamReader(ucon.getInputStream());
int chr = ins.read();
while(chr!=-1)
{
t_results.append(String.valueOf((char)chr));
chr = ins.read();
}
ins.close();
}
catch(MalformedURLException e )
{
t_results.setText(e.toString());
}

catch(IOException e )
{
t_results.setText(e.toString());
}
catch (Exception e)
{
System.out.println(e);
}
}

public void actionPerformed(ActionEvent ae)
{
exec_query();
}
}





######## Query Servlet code
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.io.*;

public class QueryServlet extends HttpServlet
{
Connection dbCon = null;
public void init() throws ServletException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon = DriverManager.getConnection("jdbc:odbc:Logins", "" , "" );

}
catch (Exception e)
{
System.out.println(e);
}
}
public void doPost(HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException
{
PrintWriter out = res.getWriter();
//res.setContentType("text/html");

try
{
String query = req.getParameter("query");;
out.println("param = "+query);
Statement s = dbCon.createStatement();
ResultSet rs = s.executeQuery(query);
while(rs.next())
{
out.println(rs.getString(1) + " - " + rs.getString(2));
out.println("");
}
}
catch (SQLException e)
{
System.out.println(e.toString());
return;
}

out.println();
out.close();
}
public void destroy()
{
try
{
dbCon.close();
}
catch (Exception e )
{
System.out.println(e);
}
}

}

Topic: I get null value when I send form to be processed Previous Topic   Next Topic Topic: layouts..

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use