• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

web app that shares file data in servlet context Attribute

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
munish sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, use getResourceAsStream() which assumes everything is relative from the web root.
 
munish sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate your help, thank you people !

getResourceAsStream() worked, and now i will try this code with ServletContext.getRealPath() ;)
reply
    Bookmark Topic Watch Topic
  • New Topic