Meaningless Drivel is fun!*
The moose likes JSP and the fly likes reading values from property file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "reading values from property file" Watch "reading values from property file" New topic
Author

reading values from property file

Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

Hi ,
I am a beginner to JSP. I am developing a small dynamic web project. In that i am haivng 2 JSP's. 1 is Main.jsp & other is Action.jsp. In Main.jsp(Parent Window) ,am having a check box. On clicking it, it should open Action.jsp(Chile Window). While loading i want some values to be read from the property file & those contents should be displayed in pop-up window(Action.JSP). I am feel difficulty in this. My property file is an text file haivng some static values like A,B,C. How to read the values from property file?

P.S: I am having the propery file in the same location where am having the JSP files.


Please help me in this.

Thanks in advance!
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5


Guaranteed to fail since that notation looks for files in the "current" directory, something you have no control over.

Bill
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Sometimes, when you find something difficult to do it's because it wasn't meant to be done that way.

On anything other than a toy or throw-away application, that is, if you're developing a web application that will be maintained and/or extended over a period of time, you should separate display concerns from concerns related to infrastructure and business logic. Reading from a properties file is an infrastructure/configuration concern. JSPs should be concerned mainly with presentation. They would display information that is already available in the current context. Don't make the JSP responsible for retrieving the values from the properties file. Otherwise, you'll be mixing two distinctly separate responsibilities and there are good reasons for not doing that (maintainability and cohesion, for instance).

It's the same reason you normally don't ask the makeup person on a movie set to go and set up the tracks for the camera dolly -- that would be chaotic, not to mention against union rules

My approach would be to read the properties file in normal Java code, put the name-value pairs in a Map or List of objects that represent Name-Value pairs, put the Map or List in the request context (setAttributes), then iterate over the List/Map in your JSP so that the property names and values are embedded in JSP-generated JavaScript code or directly in your HTML body.


Junilu - [How to Ask Questions] [How to Answer Questions]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
In order to get your properties file, you could use the helpful method in ServletContext: getResourceAsStream.



It could be noted that the Properties class does implement the Map interface, and you can pass it straight back to your JSP for read-only purposes.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Stefan Evans wrote:It could be noted that the Properties class does implement the Map interface, and you can pass it straight back to your JSP for read-only purposes.

Huh, I didn't even realize that but now that you mention it, it makes perfect sense. Shows you how much I ever even bother with properties files. Thanks, Stefan.
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

Thanks so much every one for your valuable replies.

@Junilu Lacar: Thanks for pointing my mistake gently, as iam the beginner of JSP iam not aware of this. Let me know about JSP more in depth now onwards. If you can please suggest any material and links for me to start my JSP reads.

Thanks to stefan & everyone for the reply.

Thanks in advance.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Durga Kannan wrote:@Junilu Lacar: Thanks for pointing my mistake gently, as iam the beginner of JSP iam not aware of this. Let me know about JSP more in depth now onwards. If you can please suggest any material and links for me to start my JSP reads.

I would be remiss if I didn't tell you that William Brogden, one of our all-around good cowpokes and ranchers you've seen in this very thread, has a book of exactly that nature. Also search for articles about Model 2 JSPs (as opposed to Model 1 JSPs), such as this old but still pretty helpful standby from JavaWorld.

Good luck.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
OH NOES !

That book is way way way out of date and hopefully out of print - much better and up to date books are available.

Bill
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Sorry if that reference to your work made you wince there, Bill. Was just trying to impress the greenhorn there. Hey, if I ever get you to autograph my copy (which BTW, I still have sitting on the bottom of my "current" bookshelf; maybe it's time to retire it to the basement shelf with my Clipper and AS/400 RPG books) ... but I digress. Anyway, if I ever get you to autograph my copy of your book, it might actually be worth something some day.

@Durga: In all seriousness, I'm flipping through Bill's old book here and there are still some basic things about JSPs that you can find useful. However, so you don't get confused by some of the older material mixed in there, you'd probably be better served by getting a more recent book about Servlets and JSPs, as Bill suggests.
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

@Junilu and Bill: Thanks for your reply. I will make use of those materials for my further studies.

Now am having a new doubt: This is my current scenario:
I am having the Main.jsp & Action.jsp (As i specified in my older posts). In Action.jsp, am doing some normal selection operation from selection box[e.g A,B] & this result should be passed to back and it should display in the check box present in main page on clicking submit(in action page). How should this be implemented?
These are my tries: I wrote a function cal in the submit button. In that function, am having session.set attribute. So that i can use the get attribute in the main page,to get the value. But am unable to do this.
Could somebody please provide solution for this?

1)In child page Selection should be made.
2)The selected result should display on parent window.

Many thanks!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Durga, that "somebody" who will provide the solution is going to be you. These forums are not meant for finding people who will do your work for you. There are websites like that out there but this is not one of them. Please read some of the things that have already been suggested and give your best effort to try to solve the problem yourself. Thanks for your cooperation.
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

@Junilu : Really i didn't meant in that way. If my words were wrong i beg your pardon.
I am trying some trial and error methods for getting the solution. Mean while i need to get some valuable suggestion. I thought here i would get it. So only i explained my difficulty in my last post. Really sorry and i will try my own ways to get the value passes from one page to other.

And i didn't ask other people to do work for me ever. As iam a new comer to JSP, things are new to me. That's the reason i asked like "could somebdy provide solution" rather asking suggestion.
Sorry and thanks for your reply.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Hi Durga,

Don't worry about it. We understand that English is not the primary language of many of our visitors. That's one of the reasons for our "be nice" policy. As moderators we have to balance niceness with firmness though. So the best way for you to get people to help you is to follow a format similar to this:

Confused About Something wrote:
Hello! I'm trying to figure out how to implement something like this: (describe exactly what you are trying to do here)

My problem is (describe what you're having difficulty with)
I'm not quite sure how to (whatever it is you're trying to do that's confusing or not working)

Here are some things I tried: (include some relevant code samples here)

(Here you can put your final plea for help or statement of gratitude in advance of help offered. Some examples follow:)
I really would appreciate any help or pointers on how to proceed
I am stumped at this point, any comments on how I can get past this?
Are there any concepts I'm not getting here? If so, any helpful tips or links would be greatly appreciated


You have actually followed most of this template except for the part about providing code samples of what you've tried. And of course, your "final plea" was worded in a way that can be easily misunderstood but that's Ok since you clarified that in your last post.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
I wrote a function cal in the submit button. In that function, am having session.set attribute. So that i can use the get attribute in the main page,to get the value.


Perhaps I am misunderstanding what you are trying. Anyway - note that any JavaScript function attached to the submit button can only affect the request generated by the browser. Only the servlet/jsp side can look at the session (HttpSession) or the request (HttpServletRequest) object.

Bill
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

@Brogden: Thanks for your reply. By the way, what i tried was to pass the selected values from child page to parent window, while clicking submit button. Please check my code snippets and provide me some suggestion.

Thanks a ton!!!

Any help for transferring values from child to parent window would be greatly appreciated..

Thanks so much in advance.

//Code for my Child.JSP



Sorry for my long snippet. Thanks in advance for your kind help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
What does "Parent Page" mean here?

Do you want to generate a whole new HTML page to replace an existing one?

If you want to move values between tabs/windows without sending a request that generates a new page, that is a job for JavaScript - see the JavaScript forum.

Bill
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

@Bill :Many thanks for youe reply.

Yes, but i am trying to replace/generate new HTML page. Rather, am having parent page already, in which i want the seleced value from child page should display there.

Anyways i will see the Java script forums.

Thanks so much for your kind reply.
Durga Kannan
Greenhorn

Joined: Mar 18, 2011
Posts: 19

Thanks all for your valuable help. Now am able to read values from property file & i can print the values in console.[i am using Eclipse]. But i am trying to display the property file values in the select box which am having in the JSP page. I wrote the code to read property file inside scriptlet. Now how can i take those values out & i need it inside my javascript function.

I am strucking at, how to pass & set the java string inside javascript function. Those property file values needs to be displayed while page loading.
Could anyone please suggest me the way of doing this?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading values from property file
 
Similar Threads
Error while building JSP in eclipse
Communication between browsers
JSP form submit questions
JSP - Compile errors because of includes
IllegalStateException: Cannot forward a response that is already committed