aspose file tools*
The moose likes JSP and the fly likes Get parameter from web.xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Get parameter from web.xml file" Watch "Get parameter from web.xml file" New topic
Author

Get parameter from web.xml file

cho con
Greenhorn

Joined: Apr 02, 2010
Posts: 26
Hi all,

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!
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3205
    
  49

Welcome to JavaRanch.

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


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
cho con
Greenhorn

Joined: Apr 02, 2010
Posts: 26
Hi Devaka,

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!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67

Please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3205
    
  49

I still not understand your problem.
Did you really find that config.getInitParameter("dbURL") doesn't work? How did you find that?
cho con
Greenhorn

Joined: Apr 02, 2010
Posts: 26
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"
class="shop.model.DataManager"/>
and get data from:
<%dataManager.getCattegoryDetails(catId);%>....
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
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3205
    
  49

As Bear has already said, please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise.
Please edit your post to add code tags by clicking the button.

So, you have used <jsp:useBean id="dataManager" scope="application" class="shop.model.DataManager"/> in the JSP.

In the Servlet, you can get that attribute from the Servlet context using the getAttribute(String) method..
cho con
Greenhorn

Joined: Apr 02, 2010
Posts: 26

Thank you so much Devaka, I've got it . Sorry for my mistake of posting code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get parameter from web.xml file