I have an web application that uses JSF for form submission. I have a user select an image from a list followed by filling out and submitting a form. I then use the selected image and form input to annotate the image with the information. I then process a pdf file using the annotated images and email it to the user. After processing, I give the user the option to navigate back to the image selection page, or use the same images and go back to the form. Here is the jsf/html for the form that allows the user to navigate to their new page:
<h:form id="form" method="post">
<div>
<h:commandButton value="images" action="#{artwork.backToArt}"/>
</div>
<div>
<h:commandButton value="Form" action="#{artwork.backToForm}"/>
</div>
<div>
<h:commandButton value="Upload" action="#{artwork.backToUpload}"/>
</div>
</h:form>
It seems that IE caches the annotated images and keeps those images on display regardless of my method calls.
Here are my methods:
public String backToArt()
{
log.info("========= Setting back to art page ==========");
this.backToForm();
this.selectedValue = "";
this.resizedArt = "";
this.backArt = "";
return "loginSuccess.xhtml";
}
public String backToUpload()
{ log.info("========= Setting back to upload ==========");
this.backToForm();
this.selectedValue = "";
this.resizedArt = "";
this.backArt = "";
return "CSVSubmit.xhtml";
}
public String backToForm()
{
log.info("============ Reseting all variables ==========");
String temp = null;
if (this.selectedValue != null)
{
temp = this.selectedValue;
temp=temp.substring(temp.lastIndexOf(Dir.getDir("\\")) + 1, temp.length() - 4);
this.selectedValue = temp;
}
log.info("Setting input variables");
//set all previous values to blank
//this.jobNum = "";
this.quantity = null;
this.headLine = null;
this.codeNum = null;
this.pass = null;
this.expir = null;
this.website = null;
log.info("Setting front art to old image");
//setting previous images to non-text
if (temp.length() > 0)
{
temp = this.resizedArt;
temp = "converted" + temp.substring(temp.lastIndexOf(Dir.getDir("\\")),temp.length() - 8)
+ ".jpg";
this.resizedArt=temp;
log.info("Setting back art to old image");
temp = this.backArt;
temp = "converted" + temp.substring(temp.lastIndexOf(Dir.getDir("\\")),temp.length() - 8)
+ ".jpg";
this.backArt = temp;
log.info("Deleting old text images");
//start to delete text images
File folderOne = new File(Env.textDestOne);
this.deleteFiles(folderOne);
File folderTwo = new File(Env.textDestTwo);
this.deleteFiles(folderTwo);
}
log.info("============ Reset Complete ==========");
return "FormSubmission.xhtml";
}
These methods reset the input variables and the images selected as well as deletes my annotated images depending on which button is pressed on that page. Looking at my logging, the methods are called and are processed, but no updating occurs for the images. When I process my pdf, the correct images and input are outputed, but what is viewed still shows old values. This means that my values are being stored properly and my methods are being called correctly. When I go to cancel and reprocess using Google Chrome, the images are updated properly.
Using all of this information, I have concluded that IE must be caching my images and not updating them with the correct values. I'm not sure how to override this, but I need to somehow clear the cache so that the images are updated to the correct values.
Edit: I noticed that when I refresh the page hitting F5, the pictures would update. Not sure what this means.
Related
I am trying to verify if the image is present on the webpage or not. Can you suggest me the most feasible code. I am giving the necessary details below.
(Here I am referring to the main product image on left top side in green)
Page URL : http://www.marksandspencer.com/ditsy-floral-tunic/p/p60072079
Also I can send the screenshot if you want. Please send me the email id.
Please suggest at the earliest as I am in need of it.
try to check size of found elements by xpath:
".//*[#class='s7staticimage']/img"
if width is more than 10px -- picture will be shown =)
Here are some of the ways with which you can verify if the image is present,
Checking if the img src contains the file name
Checking if the img webelement size if greater than your desired size
This one is a little outside the scope of webdriver, you need to get the src attribute from the img webelement, then make a GET request to the src to see if you get a 200 OK.
These above verifications will only help ensure that there is an image present on the page, but you cannot verify if the image you want is being displayed unless you do image comparison.
If you want to do image comparisons then take a look at https://github.com/facebookarchive/huxley
Here is the code to verify all images in a webpage - selenium webdriver, TestNG, Java.
public void testAllImages() {
// test webpage - www.yahoo.com
wd.get("https://www.yahoo.com");
//Find total No of images on page and print In console.
List<WebElement> total_images = wd.findElements(By.tagName("img"));
System.out.println("Total Number of images found on page = " + total_images.size());
//for loop to open all images one by one to check response code.
boolean isValid = false;
for (int i = 0; i < total_images.size(); i++) {
String url = total_images.get(i).getAttribute("src");
if (url != null) {
//Call getResponseCode function for each URL to check response code.
isValid = getResponseCode(url);
//Print message based on value of isValid which Is returned by getResponseCode function.
if (isValid) {
System.out.println("Valid image:" + url);
System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------");
System.out.println();
} else {
System.out.println("Broken image ------> " + url);
System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------");
System.out.println();
}
} else {
//If <a> tag do not contain href attribute and value then print this message
System.out.println("String null");
System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------");
System.out.println();
continue;
}
}
}
In android, I'm loading a local html with local images into a webview. On the emulators and even on one newer android device I tested, everything works fine - there are no problems. However, I have a much older device (with android 2.3.4) that has an issue. When I first load the html file, the images display fine. When I click the phone back button, then navigate back to the page, the images disappear. I'm trying to figure out if this is a common problem or just a problem with an old phone.
Here is the loading file:
w = (WebView) findViewById(R.id.webview1);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setBuiltInZoomControls(false);
w.getSettings().setDefaultTextEncodingName("utf-8");
InputStream is;
try {
is = getAssets().open("Learn/TrigGraphing.html");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String str = new String(buffer);
w.loadDataWithBaseURL("file:///android_asset/Learn/Images/", str, "text/html", "utf-8", "file:///android_asset/Learn/TrigGraphing.html");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the html:
<img id="img03" class="images" src="file:///android_asset/Learn/Images/UnitCircle10.png"/>
There is also one twist - I resize the images if they're too big to fit on screen. This is mainly for tablets. On the html body onload() function, I execute the following javascript:
function checkImgSize() {
if($(document).width() > $(window).width()) {
windowWidth = $(window).width();
var elements = document.getElementsByClassName("images");
for (var i=0; i<elements.length; i++) {
currentImgWidth = elements[i].width;
currentImgHeight = elements[i].height;
newImgWidth = Math.round(0.85*windowWidth);
newImgHeight = (newImgWidth/currentImgWidth)*currentImgHeight;
elements[i].style.width = newImgWidth;
elements[i].style.height = newImgHeight;
}
}
I looked at this resize function and I dont see why it would make the images disappear if they loaded a second time but I'm not sure. Anybody have any advice?
I solved my own problem after thinking about it for awhile. Apparently the webpage was loading from the cache after it loaded once and for some reason this messed it up - though i'm still not sure why. I just cleared the cache with "w.clearCache(true);" before loading the html and it works fine now.
I solved my problem by using css background property instead of img tag.
Instead of this:
<img width="100" height="100" class="images" src="file:///android_asset/Learn/Images/UnitCircle10.png"/>
I used this:
<div style="width: 100px; height: 100px; background:url(file:///android_asset/Learn/Images/UnitCircle10.png); background-size:cover; "></div>
I want to make some kind of a book(or some kind of a photo gallery) using jpg files of a scanned book.
the user gives the number of the page that he wants to go to , and clicks on the button to
see the page .
I need to know what is the best way to load the pictures.
i'm thinking of doing this for each page:
private ImageIcon image1= new ImageIcon ("1.jpg");
private ImageIcon image2 = new ImageIcon ("2.jpg");
....
and then put the pictures in an array and so on ...
but i got over 500 pictures and it is tedious to load pages like that .
so is there any other way?
Well, I can say the best way would be lazy loading plus pre-caching.
Lazy loading means you load the image only when the user needs it. For example:
img = 56; // suppose the user want to see page 56
if(images[img] != null) { // images is an array with the images
images[img] = new ImageIcon (img + ".jpg");
}
Besides, you can guest that when the user see a page they will see the next ones (pre-caching). So you can also load the following X pages.
PRELOAD = 10; // number of pages to preload
img = 56;
for(int i = 0; i < PRELOAD; i++) {
if(images[img+i] != null) {
images[img+i] = new ImageIcon ((img + i) + ".jpg");
}
}
Besides, it's you may think that in the beginning the user will always look at the firsts pages. So you can pre-load the first X pages in the start of your program.
I am developing an application in which the first window is
after clicking on log-in the log-in window appears and after correct credentials is is directed to choose time stamp and data set id window.
This is how I am accessing the value of API key (Login.java)
String value1=text1.getText();
String value2=text2.getText();
URL url = new URL("http://website-link/method/user.json?userName="+value1+"&password="+value2);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String Result;
Result=sb.toString();
String jsonSource = Result;
JSONArray array;
try {
array = new JSONArray(jsonSource);
for (int i = 0; i < array.length(); i++) {
JSONObject firstObject = (JSONObject) array.get(i);
System.out.println("APIkey is " + firstObject.getString("apiKey"));
APIkey=firstObject.getString("apiKey");
}
I want that after successful log-in the user can anytime choose data set and time stamps.
That is Login.java is passing a value to Algorithm.java (for choosing the data set and timestamp window)
if (httpCon.getResponseCode()==200) {
Algorithm frame=new Algorithm(APIkey);
frame.setSize(450,200);
frame.setVisible(true);
dispose();
JLabel label = new JLabel("Welcome:"+value1);
}
now this API key is used in Algorithm.java to set the connection and select some specific nodes based on entered time stamps and data set id.
Now I want that after log-in I can anytime click on the second button shown in this window and choose the time stamp and data set id. But in that case I don't have the value of API key as I am using this in Main.java (responsible for the above window). And in Main.java the value of API key is null. I don't want the user to enter the log in username and password again and again.
if (e.getSource() == myFirstButton) {
Login frame1=new Login();
frame1.setSize(450,200);
frame1.setVisible(true);
}
if (e.getSource() == mySecondButton) {
Algorithm frame=new Algorithm(APIkey);
frame.setSize(450,200);
frame.setVisible(true);
}
Now how can I have the value of API key stored in all files once the user entered the correct access credentials in Login window and can use it anywhere.
Each of the file: Log-in.java , Algorithm.java,Main.java is extending jframe.
and Main.java is having the main() function
I am working in Netbeans 7.1.2. I am coding this in Swing Java. I am new to Java.
The fastest way to do this would be to have a global variable APIKey in Main.java and set it's value after the user logs-in. And if you do this, I would also add a Logout button to unset this variable.
I created two files
1)index.html
2)player.jsp
I am using Tomcat Server.
In the index.html i created some checkboxes in a form and assigned the values...
After clicking submit i forwarded these values to player.jsp...
In player.jsp i dynamically generated an xml file named "generate.xml" .
This XML file changes according to the user request.
For each new request xml file should be overwritten.
if one check box is selected in the form ,structure in generate.xml will be in one form
if two check box's are selected in the form ,structure in generate.xml will be of another form.
I embedded a flash player in the jsp page which takes generate.xml as input and plays the songs accordingly...
The code of the player.jsp is
<%# page import="java.io.*" %>
<%# page contentType="text/html;charset=ISO-8859-1" %>
<%
int iLf = 10;
char cLf = (char)iLf;
String myfile = application.getRealPath("/") + "generate.xml";
File outputFile = new File(myfile);
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
outfile.write(" <?xml version='1.0' encoding='UTF-8'?> "+cLf);
outfile.write(" <playlist version='1' xmlns = 'http://xspf.org/ns/0/' > " +cLf);
outfile.write(" <title>My Band Rocks Your Socks</title> "+cLf);
outfile.write("<trackList>"+cLf);
%>
<%! String[] sports; %>
<%
sports = request.getParameterValues("sports");
out.println("<html><body><h1>hello</h1></body></html>");
if (sports != null)
{
for (int i = 0; i < sports.length; i++)
{
// outfile.writeln (sports[i]);
String total=sports[i];
String[] sa=total.split("[,]");
// String[] sub=new String();
outfile.write("<track>"+cLf);
for (int j=0;j<sa.length;j++)
{
// outfile.writeln(sa[j]);
// outfile.writeln("sa["+j+"]="+sa[j]);
if( j == 0)
{
outfile.write("<location>" + sa[0] +"</location>"+cLf);
}
else if (j == 1)
{
outfile.write("<image>" + sa[1] +"</image>"+cLf);
}
else if( j==2)
{
outfile.write("<title>" + sa[2] +"</title>"+cLf);
}
}// end of inner for loop()
outfile.write("</track>"+cLf);
//outfile.writeln();
}// end of outer for()
}
//else outfile.writeln ("<b>none<b>");
outfile.write(" </trackList> "+cLf);
outfile.write(" </playlist> "+cLf);
outfile.close();
%>
<object type="application/x-shockwave-flash" width="400" height="170"
data="xspf_player.swf?playlist_url=generate.xml">
<param name="movie" value="xspf_player.swf?playlist_url=generate.xml" />
</object>
my problem is in my local system all this is working fine
and the generate.xml is overwritten every time for every new request ...
I created ROOT.war and uploaded this file in www.eatj.com
here when 1st request is submitted an generate.xml file created according to the request..
for the next request this generate.xml is NOT OVERWRITTEN.
The player is taking the generate.xml file generated by the first request for all the new requests made.
pls help me in making any changes in the code so that i can overwrite the previous generated xml file..
The method getRealPath() doesn't work under war, and would return null, AFAIR. You must use relative path in order to make it work. Request.getResourceAsStream() is a better option. The use of getRealPath() is very much discouraged for the very reason.
[Edited]
Found a thread of coderanch confirming my doubt.