I have a image placed in a jsp. I have the image kid.jpg downloaded and kept in /resources/images folder. The below is the code snippet.
<li style="background-image: url('resources/images/kid.jpg');">
Now my web app is up and running. How to give end user to change that image/ upload a different image in that place. The same with the text in a div.
I am not getting the data from database and adding to the div dynamically.
Is database communication only way in such cases or any other way? I see many web sites contents are changing day by day. Are they modifying the webpages and replacing the existing files/ are they getting the data from database?
Please suggest how to achieve such scenarios.
Thanks
If There is no database on server and you want to allow all users ( unregistered ) to change a specific image, then what you can do is :- store the image file name in cookie, and on server check every time for that cookie, and if does not exists and render the same output with default image, other wise use that cookie's file_name image , and then on client side you can provide a facality to users to select images from particulers images that reside on server, and when user select some image so save that file name in cookie :) , Sorry for spelling mistakes, It's just a overview of solution of your problem :)
Related
Am developing a application in which html content of the pages are able to change dynamically. From my application itself user can able to change their html pages using drag and drop form(which is used to design pages).
Once the page is edited and saved successfully, I need to restart the server to reflect the changes in browser which is hurting lot.
I want to build this solution without restart the server.
What I found:
Number 1: I try to have my html pages in DB or file and in run time, i will read from that and render in browser.
Problems:
1: performance
2: HTML code is working fine in out.print() But JSP code are showing as code itself in browser.
Is there any other solution available for to fix this or any existing framework available?
I've been trying to develop a website where people can store their profile and I am really new on this. I need to upload and display their profile pictures and other data according to their need.
I stored them in one of my locations lets say 'C:\testUpload' and saved that location in my Database. Now I have to display that image when someone wants to view his/her profile. I tried to retrieve the data from database and store it in $scope.user as
$scope.user= $sce.trustAsResourceUrl("C:\\testUpload\\Screenshot (7).png");
And in the Html display I tried doing
<img ng-src = {{user}} />
But the Image is not displayed. It gives an error on the console
Not allowed to load local resource: file:///C:/testupload/Screenshot%20(7).png
What is my mistake in the above circumstances and what should be my solution in this case?
Any help would be greatly appreciated and some codes and links would also be very nice.
I am working on a J2EE application in which I need to save user's pictures. my question is where should I save these pictures ? knowing that I want to use a relative path .
NB : I am using glassfish as server .
The best practice here is to save the image inside your web application under web content (make a separate folder) , in this way you will be able to render images in other browsers also (not just in eclipse default's)
and you need to save the complete path of the image into database.
eg : e:\workspace\projectname\webcontent\pic\image.jpg
NB : never store image in database
I'll try to be short with the description of my situation:
I'm making a restaurant recommendation web site. I want users to be able to add a new restaurant and upload 1 picture of the restaurant (restaurant's profile picture). That picture will later be displayed when users search for the restaurants. Each time a new restaurant is added I want to create new folder for that restaurant and place the uploaded picture there.
I can upload an image and place on my file system, but when I try to display it it doesn't show.
<img src="\C:\glassfish4\glassfish\domains\domain1\applications\__internal\WebAppName\profilePicture.jpg" />
This is where I was originally placing the images, but they wouldn't show.
If I understood correctly from here that is not a good practice to reference images in this manner because then the browser will be looking for that location in user's machine.
I tried to place the images in:
C:\glassfish4\glassfish\domains\domain1\eclipseApps\WebAppName
because I figured that this is where WebContent folder is (please, correct me if I am wrong), and that this location is accessible with:
<img src="http://localhost:8080/WebAppName/..."/>
but that worked only for a short time. As soon as I redeployed my app the folders in which the pictures were placed had gone away (and they were created).
So my question(s) are:
How and where to place these images, and what should my src attribute look like in an html document (should it be like C:\... or http://localhost/...)?
What are conventions, practices for this, and how is this generally done?
And does redeployment has anything to do with my pictures being gone?
I found this post, but it did not solve my problem.
Note: - I am using glassfish4, and Java Servlets, JSP, JSTL/EL, and generally Java.
Thanks in advance!
And does redeployment has anything to do with my pictures being gone?
It does. When you redeployed your application, GAS removed the application directory at ${GAS_INST_DIR}\domains\${YOUR_DOMAIN_NAME}\application{YOUR_APP_BUNDLE_NAME}. Once you decided to store you images there, they are gone after the redeployment.
How and where to place these images
The most straightforward way is to put your files somewhere outside application server folder could be a solution but I would say just half a solution. Let's assume you store your pictures in a local folder /var/application/data. Later you decided to cluster your application. Now you are again in trouble. Each instance has its own /var/application/data directory and as a rule you do not know what node will handle a request for storing an image.
What are conventions, practices for this, and how is this generally done?
I would say it is you who decides what way to go according to the needs of your application. I will list the ways that are the most obvious. All have their own strength and weaknesses.
You can put the images in a local folder. The strong side is simplicity. Once you decide to cluster, you would have to remake this approach. If you go this way the most general approach would be to create a servlet that loads your images and in this case your src= will point to the servlet. Did not find a good example right away, but I think this example will give you an idea how to do it. The only thing I would suggest using finally block or if you use jdk 1.7 the try-with-resources for closing stream. Another thing you will need to pass file name as a parameter to the servlet.
Store images in database. It could be RDBMS or noSql. Down side is that not all RDBMSs work efficiently with binary data. Again src could point to a servlet that loads images from the DB. Here you should design your DB accordingly so you can retrieve images effectively. I would not choose this approach, but this is just a personal opinion. Cannot say how efficient the noSql databases are for storing binary data. You should do it yourself
Consider Webdav. In this case your src attribute will be a link to a resource in webdav server. You can use it in clustered environment, relatively simple implementation.
and what should my src attribute
look like in an html document (should it be like C:... or
http://localhost/...)?
Depends on the approach you choose. See item 1-3.
Hope that helps.
I wrote a small log-in java program that works with servlets, and JSPs using the MVC pattern that allows me to register and log-in accounts(if we can call them that) to my local mySQL DataBase. I am using sessions to pass values between the servlet and the JSP, and I have a list of if statements on the servlet that work as validation for invalid inputs. Right now when my application is executed this happens:
Sign_Up.jsp opens up and displays UserName and Password fields with a submit button below it. Also it shows a link on the top left corner to the Log_In.jsp. If you enter an username and password that follows requirements, an account is created on the database, and you are redirected to the Welcome.JSP which only shows a few lines of text. (I am not checking weather the password and username entered are unique atm, so there are duplication entries in the user table on the DB)
If you click on the link at the top of the Sign_UP.jsp, then you are redirected to the Log_In.jsp. Here you are required to enter your credentials, if they exist on the database you are redirected to the Welcome.jsp , otherwise you are told that they are invalid and that you need to enter them again.
-----------------------------------THIS IS WHAT I WOULD LIKE TO DO NOW--------------------------------------
Once an account is validated and redirected to the Welcome.jsp I would like that page to show a button that says "Choose File", which when clicked will allow you to browse your computer for files. From there I should be able to select a .csv file that I can parse and enter into the database. Maybe the .csv will contain something as simple as:
Username , Password
UserTest1, 123
UserTest2, 234
UserTest3, 567
UserTest4, 890
So these are my questions regarding this whole procedure:
Is it possible to use JS inside a JSP to accomplish my task?
Is it a good idea to use JS inside a JSP to do it?
If I were to built a more complex website is it recommended to build it using html,css and jQuery code inside the JSP?
The whole idea is to build a a website that allows the admin ONLY to enter a .csv file containing a list of prices for items, that will be grabbed by the website and uploaded into the database, which in return will show a new stock of items for a certain product. I know that I am far from done, but this is just a start. :)
You don't need JavaScript to upload a file
I don't see how it would help. Just use a input of type file
You can of course use JavaScript, whatever you use at server-side to generate the HTML pages. Embedding JavaScript code inside HTML pages is a bad practice though. You should try to externalize the JS code to .js files as much as possible.
Just a few notes regarding the title and body of your question:
reading a CSV file is the job of the controller, not the job of the view
using the session to pass objects from a servlet controller to a view is not what you should do. You should use the request to do that.
You can't read files with JS because it doesn't have permission to access the filesystem.
You will have to upload the file into the server and get the parsing done from there.
There are tons of examples for JSP file uploading in the net.
Most complex websites employ the technologies/libraries you have listed so the answer is yes use them and it will increase usability and look and feel.