You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Renato <we...@cienciapura.com.br> on 2001/10/09 00:19:35 UTC

Servlet problem

Hi all,

I have a servlet, which is more or less like this:

import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class test extends HttpServlet { 
private Connection con; 
private PrintWriter out; 

public void init(ServletConfig conf) throws ServletException { 
super.init(conf); 
try{ 
Class.forName("org.gjt.mm.mysql.Driver"); 
con = DriverManager.getConnection
("jdbc:mysql://localhost/user", "user", "pass"); 
} catch (Exception e) { 
System.err.println(e); 
} 
} 

public void service(HttpServletRequest req, HttpServletResponse res) throws 
ServletException, IOException { 
res.setContentType("text/html"); 

try { 
out = res.getWriter(); 
out.println("<html>"); 
out.println("<head>"); 
out.println("<title>Sample JDBC Servlet Demo</title>"); 
out.println("</head>"); 
out.println("<body>"); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from table"); 
out.println("<UL>"); 
while (rs.next()) { 
out.println("<LI>" + rs.getString("nome")); 
} 
out.println("</UL>"); 
rs.close(); 
stmt.close(); 
} catch (SQLException e) { 
out.println("An SQL Exception was thrown." + e); 
} catch (IOException e) { 
System.err.println("An IOException was thrown."); 
} 

out.println("</body>"); 
out.println("</html>"); 
out.close(); 
} 

public void destroy(){ 
try { 
con.close(); 
} catch (SQLException e) { 
; 
} 
} 
} 

And I have an HTML page that links to this servlet ( 
http://localhost:8080/servlet/test )

Well, it executes fine, but if I close the browser and opens again, 
clicking on the browser I have the message:

"An SQL Exception was thrown.java.sql.SQLException: Communication link 
failure: java.io.IOException "


If I change the servlet to:

import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class test extends HttpServlet { 
private Connection con; 
private PrintWriter out; 


public void service(HttpServletRequest req, HttpServletResponse res) throws 
ServletException, IOException { 
res.setContentType("text/html"); 

try{ 
Class.forName("org.gjt.mm.mysql.Driver"); 
con = DriverManager.getConnection
("jdbc:mysql://localhost/user", "user", "pass"); 
} catch (Exception e) { 
System.err.println(e); 
} 


try { 
out = res.getWriter(); 
out.println("<html>"); 
out.println("<head>"); 
out.println("<title>Sample JDBC Servlet Demo</title>"); 
out.println("</head>"); 
out.println("<body>"); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from table"); 
out.println("<UL>"); 
while (rs.next()) { 
out.println("<LI>" + rs.getString("nome")); 
} 
out.println("</UL>"); 
rs.close(); 
stmt.close(); 
} catch (SQLException e) { 
out.println("An SQL Exception was thrown." + e); 
} catch (IOException e) { 
System.err.println("An IOException was thrown."); 
} 

out.println("</body>"); 
out.println("</html>"); 
out.close(); 
} 

public void destroy(){ 
try { 
con.close(); 
} catch (SQLException e) { 
; 
} 
} 
} 

It works well in the situation. What am I doing wrong ?

Thanks !!
Renato.