0

I am designing a small app that accepts a DB username and password, connects to an Oracle database, accepts a record, and puts it into the database, using servlets.

I have one servlet which accepts the username and password, makes the DB connection and then redirects to another page. On this page, the record values are accepted. Here, I want to insert into the DB, but I don't have the connection object. Is there some way I can pass the connection object to this new servlet?

Or should I use a different approach, like authenticate the username and password, save them using setAttribute and then make the connection in the second servlet?

I am completely new to web programming and servlets, so any help would be great, thanks!

4
  • Save your connection object in ServletContext as an attribute. It will be visible to every servlet . Commented Jan 16, 2014 at 6:42
  • Don't make new connections. Re-use the same connection.Write your ContextListener. Create a connection there. Store the connection in ServletContext attributes. Commented Jan 16, 2014 at 6:46
  • Not an issue. You can write an answer to this question. There might be someone else looking for an answer to the same question :) Commented Jan 16, 2014 at 7:09
  • I would rather use one user to connect to the database and then manage and authenticate application users which you store within that one database. Then you can create a datasource in the server with a pool and simply fetch the connections from that in stead of having to create them yourself and pass them along. Commented Jan 16, 2014 at 8:50

1 Answer 1

1

An HTTP redirect looks to the server like a new page request. The way to maintain continuity between page requests from the same user is to use a session. In Java, you can get the session object from the servlet request object that is passed to the servlet.

To do exactly what you asked for, you could store the database connection in the session. However, since the redirect may not actually happen for a variety of reasons, this risks leaving the database connection open indefinitely, which is a bad idea.

A better idea would be to store the database credentials in the session, instead of opening a database connection. Then when the redirect comes in, the servlet handling the redirect can open a new database connection, store the desired values, commit and close the connection.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I thought of that too. But then, I cannot authenticate the credentials in the first servlet. Unless I authenticate it, close the connection, store the credentials, then in the second servlet open a new connection. Is that a good idea though?
If you really want to authenticate the credentials in the first servlet, that is a reasonable way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.