My problem is about getting parameter in web.xml file to connect to mysql database. In order to get convenient, I set the parameter of dbURL, dbUsername, dbPassword in web.xml file, then I have a DataManager class in my model to manage getting data from my database. Everything is ok when I call the method of this DataManager class directly from .jsp file without getting the parameter of database (dbURL, dbUsername, dbPassword). However, when I called the method of DataManager from the servlet in the same way that I did in .jsp file, it didn't work. Finally, I recognize that it didn't apply the parameter that I used to declare in web.xml fiile. So is .jsp file applies the values of these parameters automatically? But in servlet, we have to use some method to get these parameters? Any one know about this problem, please answer me. I will greatly appreciate your help. Thanks a lot!
I think it's better if we can have a look at your web.xml, that JSP, and the new servlet code. Basically, we use the getInitParameter(String) method in ServletContext or ServletConfig for getting the 'parameters' from web.xml
Thank you very much for you quick reply. I finally found the problem. Sorry that I'm new to jsp so I have some stupid questions. Let me tell my real problem. I have a servlet for managing online bookstore. In order to connect to database, I put the value of dbURL, dbUsername, dbPassword in web.xml like this:
I have a DataManager object, which is the main class in the model to manage interacting to database such as getting data, insert data. It has the method of getConnection, closeConnection with dbURL, dbUserName, dbPassword are its attributes (Beside that, it has some other methods to contact to the other objects to work with database). From that, I can connect the BookServlet to database by creating an instance of dataManager in init method of servlet:
BookServlet will control the web-app in doPost method based on the action value that it receives. For example,
Everything seems work well when I don't need to get data directly from the servlet. Most of the data I get from the .jsp file. But when I build the login system, I have to verify user directly from the servlet, if it's valid then navigate to another .jsp, otherwise if the password is incorrect, it still stays at the login page with an error message. So my problem is that I cannot get data directly from servlet when using the parameter in web.xml. After many days for searching, but I haven't solved it. Any body has some suggestions? I would greatly appreciate your help. Thanks a lot!
I still not understand your problem.
Did you really find that config.getInitParameter("dbURL") doesn't work? How did you find that?
Joined: Apr 02, 2010
It works fine when I called from .jsp file. For example, when I want to show the list of a cattegory, from a .jsp page, I need to declare only
<jsp:useBean id="dataManager" scope="application"
and get data from:
But the problem is that I cannot get data directly from Servle, specificly in doPost method since dataManager in doPost is not a global varible, it's local. And I don't know how to get data from database directly from doPost method. You have any suggestion? Thanks