Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Servlet + Sql + Connection

843842Sep 27 2009 — edited Sep 28 2009
Hi!

I am trying to make Java servlets connect to a Mysql database. My first servlet connects to the MySQL database. This works fine. When I proceed to the next servlet my database connection is NULL, and I get a NULLPOINTER Exception in Tomcat. I think the problem is that I am not correctly "forwarding" the db connection correctly from the previous servlet.

The code for my first Servlet looks like this:

public class EnterServlet extends HttpServlet {
private DatabaseDezign db;


public void init() {
String user = getInitParameter("User");
String password = getInitParameter("Password");
db = new DatabaseDezign();
db.openConnection(user, password);

}

public void destroy() {
db.closeConnection();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(db.getConnection()!=null) {
response.sendRedirect("/krusty/cannotConnect.html");
return;
}

/
--- change 'db.userExists' below to the call that you ---
--- make in your implementation of Database in order ---
--- to check if a user exists in the database ---
/


HttpSession session = request.getSession(true);
session.setAttribute("db", db);
//session.setAttribute("userId", userId);

String mainURL = response.encodeRedirectURL("/krusty/Main");
// don't know if encodeRedirectURL is necessary
response.sendRedirect(mainURL);
}
}

And this is the code for my second servlet which I am redirected to from above servlet:

public class MainServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);

if (session == null) {
response.sendRedirect("/krusty/lostSession.html");
return;
}

DatabaseDezign db = (DatabaseDezign) session.getAttribute("db");
//db.openConnection("usman", "944knoim");




response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Dezign</title></head>");
out.println("<body BGCOLOR=#82CAFF>");
out.println("<h1 align=center>Dezign</h1>");
out.println("<h2 align=left>"+db.getConnection()+"</h2>"); // **THIS RETURNS NULL**


out.println("<p>");




out.print("<form method=post action=\"");
out.print(response.encodeURL("/krusty/Product"));
out.println("\">");

Now the question is; Why is the connection NULL on the second servlet and how do I "forward" the connection. Maybe it has something to do with the Session attribute, that is where I am "sending" the Database object. Anyone please help me with this, I have tried to solve this without any luck.

Look forward for response!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2009
Added on Sep 27 2009
2 comments
407 views