RequestDispatcher and getServletContext() don't work when using init()

2003-10-15 Thread Luke Vanderfluit
Hi, 

I found that using the init() method to set up a database connection
then trying to instantiate a ServletContext object and forward to a jsp
page didn't work. 

If placed the database connection code in the doPost() then it DID,

The 2 code sets are attached,

can anyone tell me why?

thanks,
kind regards,
Luke

-- 

"when my computer smiles, I'm happy"
===.~ ~,
Luke Vanderfluit   |'/']
Mobile: 0421 276 282\~/`

/* A servlet to display the contents Bookmarks database */

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

public class CodeSet1 extends HttpServlet 
{
	Connection dbcon;  // Connection 
   public String getServletInfo()
   {
  return "Servlet connects to PostgreSQL";
   }
   // "init" sets up a database connection
   public void init(ServletConfig config) throws ServletException
   {
String loginUser = "luke";
String loginPasswd = "";
String loginUrl = "jdbc:postgresql:bookmarks";

// Load the PostgreSQL driver
   try 
		{
  Class.forName("org.postgresql.Driver");
  dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
  }
   catch (ClassNotFoundException ex)
  {
  System.err.println("ClassNotFoundException: " + ex.getMessage());
  throw new ServletException("Class not found Error");
  }
   catch (SQLException ex)
  {
  System.err.println("SQLException: " + ex.getMessage());
  }
   } // end init()

// == POST ==

   public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
   {
String loginUser = "luke";
String loginPasswd = "";
String loginUrl = "jdbc:postgresql:bookmarks";

/*// Load the PostgreSQL driver
   try
  {
  Class.forName("org.postgresql.Driver");
  dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
  }
   catch (ClassNotFoundException ex)
  {
  System.err.println("ClassNotFoundException: " + ex.getMessage());
  throw new ServletException("Class not found Error");
  }
   catch (SQLException ex)
  {
  System.err.println("SQLException: " + ex.getMessage());
  }
*/
	String title =	request.getParameter("title");
	String url = request.getParameter("url");
	String descr = request.getParameter("descr");
   try
		{
  // Declare statement
  Statement statement = dbcon.createStatement();
  String query = "insert into bookmark values('" 
+ title + "', '" + url + "', '" + descr + "')";
  // Perform update
  statement.executeUpdate(query);
		statement.close();
		}
	catch(Exception ex){}
	try{ dbcon.close(); } catch(SQLException ignored) {} 
	ServletContext context = getServletContext();
	RequestDispatcher rd = context.getRequestDispatcher("/marksMain.jsp");
	rd.forward(request, response);
	} //end doPost
}


/* A servlet to display the contents Bookmarks database */

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

public class CodeSet2 extends HttpServlet 
{
	Connection dbcon;  // Connection 
   public String getServletInfo()
   {
  return "Servlet connects to PostgreSQL";
   }
/*   // "init" sets up a database connection
   public void init(ServletConfig config) throws ServletException
   {
String loginUser = "luke";
String loginPasswd = "";
String loginUrl = "jdbc:postgresql:bookmarks";

// Load the PostgreSQL driver
   try 
		{
  Class.forName("org.postgresql.Driver");
  dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
  }
   catch (ClassNotFoundException ex)
  {
  System.err.println("ClassNotFoundException: " + ex.getMessage());
  throw new ServletException("Class not found Error");
  }
   catch (SQLException ex)
  {
  System.err.println("SQLException: " + ex.getMessage());
  }
   } // end init()

*/// == POST ==

   public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
   {
String loginUser = "luke";
String loginPasswd = "";
String loginUrl = "jdbc:postgresql:bookmarks";

// Load the PostgreSQL driver
   try
  {
  Class.forName("org.postgresql.Driver");
  dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
  }
   catch (ClassNotFoundException ex)
  {
  System.err.println("ClassNotFoundException: " + ex.getMessage());
  throw new ServletException("Class not found Error");
  }
   catch (SQLException ex)
  {
  System.err.println("SQLException: " + ex.getMessage());
  }

	String title =	request.getParameter("title");
	String url = request.getParameter("url");
	String

Re: RequestDispatcher and getServletContext() don't work when using init()

2003-10-15 Thread Tim Funk
You must call super.init(...) in your init method.

-Tim

Luke Vanderfluit wrote:
Hi, 

I found that using the init() method to set up a database connection
then trying to instantiate a ServletContext object and forward to a jsp
page didn't work. 

If placed the database connection code in the doPost() then it DID,

The 2 code sets are attached,

can anyone tell me why?

thanks,
kind regards,
Luke



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]