Ronald Schild

Ranch Hand
+ Follow
since Jun 09, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ronald Schild

Kathleen,

That already answers the question, thanks :-) I will definitely use ehcache and now also know what to look for in the Spring environment.
7 years ago

Paul Clapham wrote:I see, I didn't read your original post carefully enough. So you didn't write a servlet to handle requests for the uploaded images. Did you consider doing that and reject that on the basis that you could somehow persuade the container to handle those requests? You might find that writing the image-serving servlet makes many of your questions go away, or at least allows you to reformulate them into something which is easier to answer.



I think I see what you mean. A custom servlet for images, that purely deals with image requests? As in it would write the byte array to the response directly, dealing with any image requests that result from view components. This servlet would be mapped to any available image type (*.jpg etc.) in the web.xml.


Thank you, that will work. Is this also the way to go when using JSF? I'm thinking many web applications would need an image servlet.

7 years ago

Paul Clapham wrote:I don't see why you should be implementing a cache for these images when you could perfectly well make use of the caching feature of the browsers who access your application.

You can override the "long getLastModified(HttpServletRequest req)" method of the servlet which returns the image to return the time that the image was uploaded; this will cause browsers to cache the image.



It's all a server side cache issue to prevent DB lookups. I don't want to touch the browser cache (yet). I'm not trying to make a form of an image cache to try and make quicker image retrieval across the net. I want a cache that stores product information after it has been retrieved from the DB (this works).

Let's say I've gotten the right bean from the cache that represents a product. I use these values in my view components - but I don't know how to write an img ref to a response, with the filename as identifier, and then make the container access the image data I have available by offering it from memory, skipping writing the file onto the filesystem (for in case my app does not have the rights/something goes wrong..). So I'm wondering if there is a more transparant way to tell the container 'there is a new image resource' that it can use when resolving such an img ref.
7 years ago

Kathleen Angeles wrote:What happens to the images in memory if you restart your server? Are you going to reload all images from the db during the startup of the server?

It sounds like reinventing the cache system.

You may opt to use a cache implementation out there (e.g. ehcache).



All image data is persistent in a relational db (MySQL in this case). I access it with Spring's jdbc DAO and eventually want my cache of objects in ehcache as you describe.

Whenever a page is called that requires product data, this is loaded into memory from the DB in the form of beans. Having the image data in memory, I now write it to file and use the filename in my responses. But the container (or webserver?) must load the data from the file and store it in memory somewhere. I want to skip that file writing part and go from memory to memory.

Thinking about ehcache (unexperienced) I would figure I could create the cache with the data, but then I'd still wonder how to make Tomcat/Apache look in that cache for the images without having a filesystem as a necessary in-between (I want to be ready in case my webapp is not allowed writing to any filesystem and may only use memory and the persistent source).

Thanks for replies so far :-)

Edited:
It's not that I don't want to use a disk, I just want to gracefully degrade when none is available.
7 years ago
Hello all,

I've written a fun fileuploader and it works like a charm. Files uploaded are images of products that various pages must contain. When such an image is uploaded I store it in a database and I write it to a directory in my webapp where Tomcat can find it. What I want is to not write it to the file system, but keep it in memory and make it available that way. How do I tell Tomcat that there is a new image available in memory, so that it hits when requested? (add it to its cache? or can I offer it to Apache somehow, it's the webserver's job to respond to such resource requests right?)

Any reference to the right docs is appreciated, thanks in advance!
7 years ago

Banu Chowdary wrote:
Can anyone explain this? I didn't understand this.



It looks like making an array that contains int[] arrays, initalizing the first element of the first array to 1 and then returning that first array and assign it to 'it'.

array with int arrays new int[][] -> initialize the first element of the first array to 1 {{1}} -> return the first element of the
first dimension, which is an int array [0] -> assign that int array to 'it'.

I don't know if this even works, have no location where I can test anything now but that's what it looks like.




Well, what the duce?

I keep wondering where " " went. "Punit" not being there is something understandable.

Also, I wonder why there is no build up String object "PunitSoto ".

Does this program just check application references and find the objects that they refer to? It shows all the Strings after expressions have been worked through. Is it possible to dump the SCP solely?
Nice to hear

My tip: don't overemphasize perfectionism when trying to come up with a product. It may seem that you have all the time there is to complete something, but consider what you want to have done and when. Some parts of the solutions are intentionally a bit 'dirtier' than other parts. Some years back I started programming a mud and found that I needed an easy way to make objects editable (through an ascii menu) and streamable, etc. I came up with two functions that objects would need to override, to fill up a DOM structure with their data (based on a template that would describe the members etc.) and from there it was handy to write generic code that deals with the actual work. Half a year later I wished I had just made it easier, because with knowledge of libraries and frameworks around I think I would have handled it differently and the solution that took _too_ much time looks shaky, not perfect.
11 years ago
It seems the toString of the collection iterates through its elements and saves up the return value of their toString() in one big String object.
Every time it calls an element's toString(), the function prints "Inside the method tostring". After calling toString() for all elements, meaning
a number of "Inside the method tostring" strings are printed equal to the number of elements in the collection, the total of return values
is printed and gives the remaining info.

Something like:

declare bigstring
loop through all elements
call tostring, and add the returned value to bigstring -> this call also writes "Inside the method tostring"
continue till end of collection
print bigstring

Hope it helps!
11 years ago
Just wanted to say thanks to the widely experienced people who give (quite) some of their time to help beginners!

A short story I'd like to share: back in college, a friend of mine had decided to cut n paste snippets of code to
get his assignment done. In this code, he tried to call functions directly by class name (replaced the object reference
name with the class name, figuring that was the name that he would use in his program). This didn't work well
and he started to randomly try stuff. Seeing as main was static, he decided to make some other methods
static. And this worked, somewhat. Eventually with a bit more fumbling he figured out how to print employee
data on the screen without creating instances of his Employee class. He passed, because the program printed
somewhat correct outcome and with a bit of explaining from the teacher (who just got into java himself and figured
this would be a way one _could_ use staticness) my friend was ready for the next level course. The next level
course was more about OO fundamentals. One could pass this course with doing some UML right and so did
my friend. He also said he had no clue about what OO meant for programming. Luckily my friend never wanted
a job as programmer.

Obviously, to learn java, cutting and pasting code does not add much. It's like having a car that's missing a
part of the engine and you have all kinds of parts of which some even seem to fit. You have to get into the
details of the matter and start at a very basic level. Reserve time to work on it. And it's a good thing that people
here give hints (otherwise, it'd be copy/pasting the solution from their mind to yours - and you'd be nodding but
wondering what to do when you're actually about to write code).
11 years ago
Very nice work done, thanks.

I'm anxious to hear whether this behaviour is covered by the exams or not.
If it is a check on x and y that you want to do before calling super, odds are this check is something you want to place
in the constructor of the super class.

If it is a check that arranges class specific details that do not go for the super class, odds are this is something you want to
place in the subclass. This will go after super(), because it is class specific (there's no reason to try and handle subclass specific
stuff at the moment you're in the process of handling super class stuff during object creation - you build the foundation of a
house before you put up the walls that carry the roof).





11 years ago

Ankit Garg wrote:Ronald you are correct. But I was trying to show that only primitive 1D arrays nherit from Object class. Rest arrays are sub classes of Object[]. So you cannot write this

Object[] arr = new int[5]; //error

This also means that all the multi dimensional arrays are descendants of Object[]. Let me draw the diagram again for completeness



I understand what you are saying. My point is that casting an array of object references follows rules that use the class hierarchy
of the element types rather than the array object type.

For a variable SomeObject [][] x = new SomeObject[1][2], if SomeObject inherits from AnotherObject, being able to cast
x to AnotherObject[][] does not mean AnotherObject[][] is part of the hierarchy. There's only SomeObject <- AnotherObject.

The only conversion possible is array -> object. In the above case, it's the object referenced by x that I'm after, which is
an array object. Casting x to AnotherObject[][] makes x still point to the array object, which is treated as such. Reducing
dimensions uses the array -> object conversion. If x would be cast to Object[], the second dimension: SomeObject[]
arrays are cast to class Object. The first dimension remains intact, and x would still be referencing an array object.

Casting second (and up) dimensions into object causes the array to contain elements of type object, and makes the
array reference of Object[] (or with more dimensions).

So, casting int[][] to Object[] uses the any array -> Object conversion plus a type conversion that tells that the
array elements of array are now objects instead of int[] array references. And casting int[][] to Object uses the
same cast that was used for the array elements, namely array -> object.

If multidimensional arrays really had to inherit from Object[] it would mean that there is a conversion from an
array with >2 dimensions to Object[] before there could be a cast to Object. That would give the following issue:

int [][][][] x = new int[1][2][3][4] ;
Object[][] a = x ;

The last 3 dimensions are cast to Object[]. Object[] is placed in Object[], and Object[][] exists.

This, I think, does not happen. There is only the second dimension array reference elements that are
cast to Object references int[][] -> Object.

So there's a cast that has a focus on the type of element, and there's a cast that actually upcasts the
array object to an object. I think only the last one really describes array hierarchy, the rest is element
hierarchy.

/discuss

Ankit Garg wrote:let me give you a sample hierarchy to make it simple



Wouldn't you say that all arrays objects inherit from Object?

We can upcast String[] to Object[] due to casting rules, which has more effect on how we see the type of elements rather than how we
would write out a hierarchy of array classes.





Punit Singh wrote:

(L5) String object with content "abcdef " created.
String object with content "abcdef abcdef" created.



I did not get this one Ronald.



Multiple things happen at s1 + " " + s2. First s1 + " " is handled. Next "abcdef " + s2 is handled.