dcsimg
Applet to Servlet Communication??
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Chitra_Venugopal
Posted On:   Wednesday, March 21, 2001 03:15 AM

How to make my applet communicate to the servlet, I used Frame in the applet and i have 3 TextFields which gets the values from the user and i'm suppose to call my servlet wherein i have my database connection, which should go and update my database, and my back-end is mysql, I herewith provide my java source files of my applet and servlet. ******* Applet ********* import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.*; import java.sql.*; import java.awt.TextComponent.*; import chatModule.*; public class add_a_friends extends JFrame implements ActionListener {    More>>

How to make my applet communicate to the servlet,
I used Frame in the applet and i have 3 TextFields which gets the values from the user and i'm suppose to call my servlet wherein i have my database connection, which should go and update my database, and my back-end is mysql,
I herewith provide my java source files of my applet and servlet.


******* Applet *********
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import java.sql.*;
import java.awt.TextComponent.*;
import chatModule.*;

public class add_a_friends extends JFrame implements ActionListener
{
static JTextField jf, jf1, jf2, jf3, jf4, jf5, jf6, jf7, jf8, jf9, jf10, jf11, jf12;
static Connection con = null;
static ResultSet rs = null;
static PreparedStatement psmt = null;
static PreparedStatement psmt1 = null;
static String friendsname = null;
static String friendsid = null;
static String message = null;
static String userid;
user_tracking ut = new user_tracking();

public add_a_friends()
{
setSize(300, 300);

jf1 = new JTextField(15);
jf2 = new JTextField(15);
jf3 = new JTextField(15);
jf4 = new JTextField();
jf5 = new JTextField();
jf6 = new JTextField();
jf7 = new JTextField();
jf8 = new JTextField();
jf9 = new JTextField();
jf10 = new JTextField();
jf11 = new JTextField();
jf12 = new JTextField();


Container con = this.getContentPane();
con.setLayout(new GridLayout(13,13));
setForeground(Color.lightGray);

con.add(new JLabel("Friend's Name"));
con.add(jf1);

con.add(new JLabel("Friend's Id"));
con.add(jf2);

con.add(new JLabel("Message"));
con.add(jf3);

JButton submit = new JButton("Submit");
submit.addActionListener(this);
con.add(submit);


con.add(new JLabel(""));
con.add(jf4);
jf4.setVisible(false);

con.add(new JLabel(""));
con.add(jf5);
jf5.setVisible(false);

con.add(new JLabel(""));
con.add(jf6);
jf6.setVisible(false);

con.add(new JLabel(""));
con.add(jf7);
jf7.setVisible(false);

con.add(new JLabel(""));
con.add(jf8);
jf8.setVisible(false);

con.add(new JLabel(""));
con.add(jf9);
jf9.setVisible(false);

con.add(new JLabel(""));
con.add(jf10);
jf10.setVisible(false);

con.add(new JLabel(""));
con.add(jf11);
jf11.setVisible(false);

con.add(new JLabel(""));
con.add(jf12);
jf12.setVisible(false);

setSize(300, 300);
setVisible(true);
show();
}

public void actionPerformed(ActionEvent ae)
{
friendsname = jf1.getText();
friendsid = jf2.getText();
message = jf3.getText();
userid = ut.userid1;
System.out.println(friendsname);
System.out.println(friendsid);
System.out.println(message);
System.out.println(userid);

String str = ae.getActionCommand();

try
{
if(str.equals("Submit"));
{
System.out.println("a");
URL url = new URL("http://evereadi:8080/jetspeed/servlet/add_friend_servlet");
System.out.println("b");
URLConnection uc = url.openConnection();
System.out.println("c");
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
System.out.println("d");
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
String temp = userid+":"+friendsid+":"+message;
String qry = "insert into friends_list values('"+userid+"', '"+friendsid+"', '"+message+"')";
System.out.println("e");
dos.writeBytes(temp);
dos.flush();
dos.close();
System.out.println("f");
InputStreamReader in = new InputStreamReader(uc.getInputStream());
System.out.println("g");
int chr = in.read();
while(chr != -1)
{
chr = in.read();
}
in.close();
}
}
System.out.println("kk");


catch(Exception e)
{
System.out.println("Error @ invoking actionPerformed():"+ e);
}
}

}

******* Servlet *******

//import servlet libraries
import javax.servlet.*;
import javax.servlet.http.*;

//import java libraries
import java.io.*;
import java.util.*;
import java.sql.*;

//import user definied package
import chatModule.*;

public class add_friend_servlet extends HttpServlet
{
Connection con = null;
ResultSet rs = null;
PreparedStatement psmt = null;
user_tracking ut = new user_tracking();
static String userid;


public void init() throws ServletException
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://evereadi/chat?user=root");
}
catch(Exception e)
{
System.out.println("Database connection failed");
}
}

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
try
{
userid = ut.userid1;

PrintWriter pw = res.getWriter();
res.setContentType("text/html");

String query = req.getParameter("qry");
psmt = con.prepareStatement(query);
int i = psmt.executeUpdate();
if (i>0)
pw.println("yes");
else
pw.println("no");
}
catch(Exception e)
{
System.out.println("Error :" + e);
}
}

public void destroy()
{
//close the database connection
try
{
con.close();
}
catch(Exception e)
{
System.out.println("Connection Closed");
}
}
}

Very kind of u, if u could solve my problem as regarding updation of database,

Sincerely,

Chitra

   <<Less

Re: Applet to Servlet Communication??

Posted By:   Felker_Robert  
Posted On:   Friday, June 8, 2001 02:14 AM

The better way is to use Socket and ObjectSream but the ServerSocket have to be on the same machine that serve the Applet
About | Sitemap | Contact