Hello, Can anyone help
I have a Pentium Pro with Windows NT running Apache HTTP server 1.3.9 and
Apache JServ 1.0.
Using JDK1.2 I am trying to get an Applet to access a Servlet which just
returns some strings.
I am using the example provided in the 'Professional Java Server
Programming' book from wrox.
Currently, my Applet can open a connection to the Servlet via an
.openConnection() call, but when I
use the DataOutputStream.writeBytes(), I get a 'connection refused,
IOException'
I think it is something to do with allowing certain permissions in the
web-server, but I am not sure.
Can anyone help
The code that fails me is DBApplet.java 's executeQuery() method.
Thanx
David
---------------------------------------------------
// DBServlet.java
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.util.* ;
import java.sql.* ;
import java.io.* ;
public class DBServlet extends HttpServlet {
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() ) {
for( int j = 1 ; j <=
rs.getMetaData().getColumnCount() ; j++ ) {
out.println( rs.getObject( j ) + "\t" ) ;
}
out.println( "\n" ) ;
}
} catch( SQLException e ) {
System.out.println( "SQLException" ) ;
System.out.println( e.toString() ) ;
}
}
public void destroy() {
try{
dbCon.close() ;
} catch( Exception e ) {
System.out.println( "Error closing database" ) ;
System.out.println( e.toString() ) ;
}
}
public void init() throws ServletException {
try {
Class.forName( "com.inet.tds.TdsDriver" ).newInstance() ;
// JDK,
Netscape, IE
String dbURL = "jdbc:inetdae:localhost:1433" ;
String dbID = "guest" ;
String dbPassword = "guest" ;
DriverManager.setLoginTimeout( 10 );
dbCon = DriverManager.getConnection( dbURL, dbID, dbPassword )
;
dbCon.setCatalog( "Pubs" ) ;
} catch( Exception e ) {
System.out.println( "Database connect failed (init)" ) ;
System.out.println( e.toString() ) ;
}
}
}
---------------------------------------------------
//DBApplet.java
import java.awt.event.* ;
import java.io.* ;
import java.net.* ;
public class DBApplet extends Applet implements ActionListener {
TextField tfQuery ;
TextField tfURL ;
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( "select * from authors", 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 ) ;
tfURL = new TextField( "http://localhost:8080/servlets/DBServlet", 50
) ;
add( "South", tfURL ) ;
}
public void executeQuery() {
URL url = new URL( "http://localhost:8080/servlets/DBServlet" ) ;
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" ) ;
// Problem lies here, I can create dos
DataOutputStream dos = new DataOutputStream( uc.getOutputStream() ) ;
//But applet creates exception trying to execute this line
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.append( "MALFORMEDURL:" + e.toString() + "\n" ) ;
} catch( IOException e ) {
taResults.append( "IOEXCEPTION" + e.toString() + "\n" ) ;
}
}
public void actionPerformed( ActionEvent ae ) {
executeQuery() ;
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html