GeeCON Prague 2014*
The moose likes Servlets and the fly likes web app that shares file data in servlet context Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "web app that shares file data in servlet context Attribute" Watch "web app that shares file data in servlet context Attribute" New topic
Author

web app that shares file data in servlet context Attribute

munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
hi.

I was working on a web app wherein i have stuck. !
In This webapp i have a controller servlet, 2 view servlets ( one of which fills the form & other servlet displays the information)
i tried to create a text file that will contain the data (model data !!) . This is data which is generated by form_servlet, and it has to be displayed by display_servlet

i used web application listner , could anybody write a small working application demonstrating this, because i don't know where i am stuck.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42046
    
  64
No, nobody here will write the code for you, but folks will be happy to help with the code you wrote. So just post relevant excerpts of the code with which you are stuck, and we'll try to make suggestions on what to do next.

First suggestion: using a file for sharing data between Java classes sounds really odd. I'm not saying that it is never the right approach, but that it is highly unlikely to be the right choice. Context attributes -properly guarded against concurrency- indeed sound much better, so you may be on the right track already. If this is data that all concerns the same user, you might also consider using an HttpSession.


Ping & DNS - my free Android networking tools app
munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
I have recently started developing web applications, and thats why i am using text files
Herein Admin and user are involved , so instead of HTTPSession i think context would serve my purpose better.

InitializeProject ( a web apllication listner ) implementation goes like this :


deployment descriptor looks like this :

<context-param>
<param-name>My-File</param-name>
<param-value>/WEB_INF/data/demofile.txt</param-value>
</context-param>
<listener>
<listener-class>..........web.InitializeProject</listener-class>
</listener>

I need suggestions.....!!
do i need to mannualy create this data file (demofile.txt) in WEB-INF ?
my view servlet is not getting updated!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42046
    
  64
Herein Admin and user are involved , so instead of HTTPSession i think context would serve my purpose better.

OK

What happens when you run this code? My guess would be that there's an exception because the file doesn't exist, and so nothing is set as MyList.
munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
Exactly !!
web app runs fine , but as you say nothing happens as MyList is not there

what should i do to create a file and populate it ?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42046
    
  64
Does "Exactly !!" mean that there is an exception, and you just didn't tell us about it? That is easy to check, since the code actually prints something to the console if there is an exception.
munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
no every things is running fine without exception .

i want to update the view ! How should i continue ??
i am i missing something ??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42046
    
  64
That's odd. If there is no exception, then there should be an attribute "MyList" - but there isn't if I understand you correctly?

Have you considered attaching a debugger to the web app, to see what happens during the request?

And does the file get written correctly by whichever code does the writing of that file?

And lastly, does the file get read correctly if you create it manually?

These are all things you can do independently of waiting for help here.
munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
Hey hi again

I made my code run fine . I have a little doubt though.

When i created a Web Application Listener to load a file ......... i could only load my file from D:\Test\text.txt
instead i want this file to be placed in WEB-INF folder !!

For this to happen i have tried editing web.xml like this
<context-param>
<param-name>abc</param-name>
<param-value>/WEB-INF/text.txt</param-value>
</context-param>

But this does not work ..... only when <param-value> is D:\Test\text.txt the application responds



text.txt exists in both directories...!!
what to do ??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42046
    
  64
How are you constructing a File object from that parameter? Be aware that web apps have no concept of relative file names - you need to use absolute paths. The ServletContext.getRealPath method can help you construct one.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Or, use getResourceAsStream() which assumes everything is relative from the web root.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
munish sharma
Greenhorn

Joined: Jul 16, 2013
Posts: 8
I appreciate your help, thank you people !

getResourceAsStream() worked, and now i will try this code with ServletContext.getRealPath() ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: web app that shares file data in servlet context Attribute