• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a servlet from an applet

 
Srinivasa Dhamodharan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! All,
I am trying to load a servlet and run it from an applet,
the servlet has got a connection to an oracle database. Whenever
I click the submit button in the applet which invokes the servlet , I am getting an exception which is as follows
java.net.exception connection refused with no further information
I dont what is this problem?.Can anyone help me solve this?.
Thanks in Advance.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exception means that the applet was unable to open a connection to the requested machine and port. Make sure that the URL used in your applet is the same as the URL of the servlet, especially the machine name and port sections, and that the server software is actually running on the server machine when you try to connect.
 
Srinivasa Dhamodharan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Frank, But I need to throw some more details
in this , I tried the URL which I use to load other servlets for the web browser, In detail I am using Apache Web Server/Jserver with oracle.
So I am storing all my servlets in
c:\programfiles\apachegroup\jserv\servlets
If I need to load any servlet from the browser, for example ,
the IsItWorking servlet from Jserv package , I give the URL
//localhost/servlets/IsItWorking
But if I give the same URL in the URL for opening the servlet from the applet, It gives me file not found DbServlet (My servlet name), I am I doing the right thing here?. Please clarify...
Thanks
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are your browser and server on the same machine? "localhost" implies the applet is running on the same machine as the server.
Have you tested the DbServlet from a browser manually using that URL?
 
Srinivasa Dhamodharan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
You are correct the browser and the server are in the same machine, I havent tried this particular servlet loading manually from the browser because this connects to database, executes
the query sent by the applet and returns the result set to the
applet, I dont know whether I will be able to run this manually
from the browser, But as I told you earlier I have tried loading manually other servlets which is stored in the same directory as this, and it works.
Thanks.
 
Brett Knapik
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code it might help maybe. its probably in the applet or servlet code i bet.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Are you running the applet off the webserver i.e. using a http://localhost .... URL ?
If you are using a file URL for the HTML file containing the applet, then you get this kind of an error.
Hope this helps.
Ashwin.
 
Srinivasa Dhamodharan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett,
This is my code
// This is my HTML which invokes the applet DbApplet.java
// All the 3 files I mean //dbApplet.html,DbApplet.class,DbServlet.class are stored
// in the same directory

<HTML>
<HEAD>
<TITLE>Database Query Interface</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>Database Query Interface</H1>
<HR>
<BR>
<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.0 -->
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = "600" HEIGHT = "300" NAME = "DbApplet" codebase="/">
<PARAM NAME = CODE VALUE = "DbApplet.class" >
<PARAM NAME = NAME VALUE = "DbApplet" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.1">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.1" java_CODE = "DbApplet.class" NAME = "DbApplet" WIDTH = "600" HEIGHT = "300" pluginspage="http://java.sun.com/products/plugin/1.1.1/plugin-install.html">
<NOEMBED></COMMENT>
</NOEMBED></EMBED>
</OBJECT>
<!--
<APPLET CODE = "DbApplet.class" WIDTH = "600" HEIGHT = "300" NAME = "DbApplet" >
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</CENTER>
</BODY>
</HTML>
This is my Applet which calls the DbServlet.class in turn which accesses the database

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class DbApplet extends Applet implements ActionListener {
TextField tfQuery;
TextArea taResults;
Button btnExecute;
public void init() {
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(new Label("Query String:"));
tfQuery = new TextField("", 50);
p1.add(tfQuery);
btnExecute = new Button("Execute Query");
btnExecute.addActionListener(this);
p1.add(btnExecute);
add("North", p1);
taResults = new TextArea(10, 80);
add("Center", taResults);
}
public void executeQuery() {
/* This is part of the code I added in my trial and error but please dont count this
String webServerStr, protocol;
String hostName="localhost";
int port = 8080;

URL hostURL = getCodeBase();
protocol=hostURL.getProtocol();
hostName = hostURL.getHost();
port = hostURL.getPort();
if (port == -1)
{
port = 80;
}
End of trial and error code*/
String qryString = tfQuery.getText();
try {
webServerStr = "http://localhost/servlets/DbServlet";

URL url = new URL(webServerStr);
String qry = URLEncoder.encode("qry") + "=" +
URLEncoder.encode(qryString);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");

DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
dos.close();
InputStreamReader in = new InputStreamReader(uc.getInputStream());

int chr = in.read();
while (chr != -1) {
taResults.append(String.valueOf((char) chr));
chr = in.read();
}
in.close();
} catch(MalformedURLException e) {
taResults.setText(e.toString());
} catch(IOException e) {
taResults.setText(e.toString());

}
catch(Exception e) {
taResults.setText(e.toString());

}
}
public void actionPerformed(ActionEvent ae) {
executeQuery();
}
}
//This is my Servlet Program
//Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;
//Import Java Libraries
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbServlet extends HttpServlet {
Connection dbCon;
public void init() throws ServletException {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

dbCon = DriverManager.getConnection("jdbc racle:thin:@localhost:1521 rcl","javauser","javauser");

} catch (Exception e) {
System.out.println("Database connect failed (init)");
System.out.println(e.toString());
return;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
String qry = req.getParameter("qry");
try {
Statement s = dbCon.createStatement();
ResultSet rs = s.executeQuery(qry);
while (rs.next()) {
out.println(rs.getString(1) + " - " + rs.getString(2));
out.println(rs.getString(3));
out.println ("");
}
} catch (SQLException e) {
System.out.println(e.toString());
return;
}
out.println();
out.close();
}
public void destroy() {
/* Close database connection */
try {
dbCon.close();
} catch (Exception e) {
System.out.println("Error closing database (destroy)");
System.out.println(e.toString());
}
}
}
 
Srinivasa Dhamodharan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin,
I am calling this html from a browser using ctrl+0 and then selecting the directory and file name.

Thanks
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivas,
If i am not mistaken, If you use CTRL-O and select the file, the browser by default uses the file:// URL. This is a problem here.
assuming your .html file is test.html and is reachable in the root of the webserver on your machine.
type http://localhost/test.html to access this file AND NOT CTRL-O
Hope this helps.
Ashwin.
 
Brett Knapik
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gee this code looks familar. if no solutions work from here go and download the source from wrox.com they have made corrections to the code I heard.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic