Reputation: 2108
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!
Upvotes: 0
Views: 1409
Reputation: 8928
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.
Upvotes: 1