response.setHeader error on google chrome - java

This is a project on JSP.
1)I have this code where a button is pressed and the action is rendered to another page,which is a JSP page containing a pdf and the user is able to save it.
I had tried my code a month back and it worked fine,now when I try it,it doesnt work on chrome,works on mozilla though.
this is the line of code
response.setHeader("Content-Disposition", "inline; filename=Question.pdf");
2)My second problem is that,I have a reset button,this button resets the database.this function works fine on chrome but not on mozilla.I have no idea why it doesnt,there is no action taking place at all.
this is the line of code for that
<button type="button" onclick="reset.jsp">RESET</button>

For question 1):
What I know from the difference between "content-disposition", "attachment;filename=somefile.ext" and "content-disposition", "inline;filename=somefile.ext" is that in the inline case the browser will try to render it alone and if it can't it will prompt the user to download it. Maybe in your case you had an extension of Chrome that was capable of showing PDF files in the browser and now not. See Content-Disposition:What are the differences between "inline" and "attachment"?
As for question 2) :
This code onclick="reset.jsp" is not a valid construction. Onclick has to be a valid JavaScript function. See http://www.w3schools.com/jsref/event_onclick.asp. And it you case it not necessary to have it at all as you have an link inside the button which performs the actual action. I guess Chrome ignores this error and MOzilla not. Try removing onclick="reset.jsp" from the button.

Related

Android, YUI, Moodle

The problem I'm having is the following:
I have an app with two separate modes: A WebView for browsing and a custom Canvas. The custom Canvas captures handwriting samples for language placement exams. Here's how it works. A user logs in to Moodle via the WebView. After they log in, they navigate to a Quiz inside Moodle. They click a link on one of the Quiz's questions and this launches an Intent which hides the WebView and shows the Canvas. The user then writes (using a stylus) on the Canvas. When a user is finished writing their essay (or whatever), they press a button that uploads an image file to Moodle. I am able to upload images to a point, it's getting them to show up in the HTML page that the user clicked the link in originally (see above) and to get Moodle to commit them to permanent storage that is the problem. Normally this is all accomplished through AJAX (really AJAJ since it's JavaScript and JSON) and when the user drops a file on this one component, the component refreshes and uploads the file.
Here is the problem: I need the WebView so that students can log in to Moodle through Shibboleth. But because the underlying JavaScript in the browser makes AJAX calls to the Moodle server and since the Java side of Android doesn't have access to the DOM, I have use the Apache HTTP components library to make some of the connections below basically to preserve the state of the HTML page in WebView.
In a desktop browser on, say, Windows, I use WebScarab to monitor the browser's requests and this is what I see: the browser uploads a file to Moodle via five successive calls to the following scripts:
POST https://[moodle website]/repository/repository_ajax.php [posts multipart form data]
POST https://[moodle website]/repository/draftfiles_ajax.php [posts some params]
GET https://[moodle website]/draftfile.php/[some_id]/user/draft/[some_id]/[somefilename.png] [returns an icon of the image for a filepicker from YUI]
POST https://[moodle website]/mod/quiz/processattempt.php [returns HTML page]
GET https://[moodle website]/mod/quiz/summary.php [returns HTML page]
Some of these scripts return, as you'd expect, JSON data since they're AJAX and not HTML. The final two calls (4 & 5) return HTML. Now, I can make all of those calls in succession in either the WebView or the Apache HTTP library, but if I do so with WebView, only JSON data is returned to the WebView in calls 1-3 (WebView treats the JSON data as a page and displays it wiping out whatever HTML page was displayed in it). If I capture and process the JSON data using the Apache HTTP library in Java, then the JavaScript components internal to the page do not get updated. If I split the calls so that I send only calls 4 & 5 to the WebView, the HTML merely returns WebView to the first question of the exam and Moodle acts as if I haven't uploaded anything.
I can verify that files are uploading if I manually refresh (press a link) the JavaScript UI elements in the page. I can't expect students to do this, though, because the link to do so is very tiny and it's not obvious that it does a refresh. I need a way to programmatically refresh this one element (it's part of YUI) or to get Android and the Java side to play more nicely with the JavaScript/DOM side.
My question is: does anyone know a way to 1) fire off a drag and drop event using YUI to an element inside an HTML page or 2) a way to consume the JSON data and pass it to an element inside the HTML page.
I'm banging my head against a wall trying to figure this out.
OK, so I figured out that: javascript:document.getElementsByClassName(\"[name of link here]\")[0].click() works in Chrome on the desktop but doesn't work if I pass it to WebView.loadURL(). I just need to be able to simulate that click event reliably in WebView. It appears not to support click(). Anyone have any ideas?
The winning code is:
el = document.getElementsByClassName("[some element]")[0];
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, true);
el.dispatchEvent(event);
This selects the link at [some element] and thereby fires an AJAX request that refreshes the FilePicker. For those working with Moodle, I had to add the above code to the same quiz question that handles so it is invoked by putting that code in its own function and calling it with WebView.loadURL("javascript:myRefreshFunction()").

File upload with Selenium WebDriver and FF24

I am facing an issue with the file upload with WebDriver, using Java, on Firefox 24.
And I can NOT use some external program like AutoIT or similar.
I have to upload a file to a section which's HTML code is:
<td>
<input type="file" name="file">
</td>
And what I was using in Eclipse is
pageObject.getTypeFileLocation().sendKeys(textFile);
pageObject.getUploadButton.click();
which does not work; also tried the first answer of this question but neither did the trick.
The test returns 'OK', but I suspected that nothing was being done so added a check to wait for 'Upload complete' text present; But the file is not uploaded, and timeouts after 20 seconds, even when the file to upload is 5KB and takes less than a second when manually uploaded.
The input=file section contains a button and a 'No File Selected' text that changes to the filepath when a file is selected manually; and this is in what I am basing my idea that the file is not being upload; because the 'No File Selected' remains until the test fails.
I tried this on Chrome and seems to be working fine, and I know that there have been some reworks about input=file in FF since release 23, but mostly pointed to CSS styling, so I don't think it's related.
Also, a question that might sound kind of stupid, but questions are questions: Doesn't the sendKeys() action need a field to input those keys? I feel that the WebDriver is trying to write the path over a button, which can't perform the action as it's only a button.
Any help will be appreciated, and thanks in advance!
Solved it!
Don't know if it works for all browsers, but at least it does on FF and Chrome:
Found out that somebody used the FILE type, so reused it and got its absolute path:
protected File *fileName* = new File("*path to file*");
private String textFile = *fileName*.getAbsolutePath();
pageObject.getInput().sendKeys(textFile);
Hope somebody else finds it useful; as it's weird to be answering my own question.

Reading website content via MATLAB

I need to read the content of a website via MATLAB but the problem is when I load the website as:
URL = java.net.URL(UrlNew);
urlConnection = URL.openConnection();
urlConnection.connect();
str = regexprep(fileread(filename),'\s*',' ');
The website redirects me to a page which I have first to press "I agree" button and then it goes to the requested page.
How can I handle this situation in MATLAB. I need to somehow click the "Agree" button in MATLAB to have access to the original webpage.
Thx
First you need to find out the value of the action attribute of the form on your original page. You can do this by examining the source code of the page, looking for the form tag containing the Agree button, or by manually clicking on the button and looking at the Network activity tab in the Chrome Developer Tools or Firebug in Firefox.
Once you find this, find out how to send POST requests in Java and simulate the clicking of the button by sending a POST request to the action URL of the form. If you collect the output from the socket after that, it should correspond to the page you want.

Access Javascript from Java

I m trying to access JavaScript function from Servlet code. But I'm getting the error shown below.
Here is the code:
out.println("<FRAME src=\"javascript:parent.newWindow('" + URL+ "') \" scrolling=No noresize />");
And this is the error that occurs in JavaScript:
Object does not support this property or method;
You can't access a Javascript function from your servlet code. Javascript executes on the client (= your user's browser) and the servlet code executes on your server (for example Tomcat, JBoss, whatever you're using).
What are you trying to accomplish with your code? I'm sure there's a simpler way to do it than what you just described.
[edited]
I see you just updated your description, so here's my view:
I'm guessing that you want to display a page to the user and when the page is displayed, you want to open a new window which will display another page using the URL parameter to point its address. If this is the case, you should probably just do this in the first page's onLoad() Javascript event using window.open().
There is no newWindow property on a window object (which is what parent references), so this is not unexpected.
Maybe you are looking for the open method instead?
If so, then:
Putting it as the src of an iframe is a very strange thing to do
It will probably be zapped by pop-up blockers
Ok. You try to generate javascript code inside Servlet code. When you do, your code goes to Web browser and it's seen there as a html document with javascript inside. So, your error rather comes from web browser and links to javascript error. Probably it's newWindow method. To open new window you should call window.open() function, I guess.

jquery and java communication

I have jquery pop form to upload a file, after on submit (the page refresh and the pop close) i check something about the file and then if there's something wrong i need to pop up that form again (from the java code?), how could i do that ?
You should use ( or must be using) ajax in jquery with a popup.
When the user hits "submit", control goes to server side code.
The code runs to upload the file.
Whatever the result of upload (success/failure), that message is sent to the popup with ajax automatically.
In case, there is problem in uploading the file then, along with the failure message, you can send in the div which contains your form.
I think rather than refreshing the page to close the popup, allow the user to close the popup with close button.
When "something is wrong" the server-side code (this applies to any language) should include within the HTML content Javascript that will trigger the "form" to be displayed again.
As I feel dizzy presently,can't write the code,but will try to break whole procedure in multiple steps:-
On Trigger(by some event) a pop up form will open from submission,which will have a button which will be calling a OnClick Event,which will be containing an Ajax call for client server communication
till the response don't close or fade out the Pop up box.
from server expect two tags SUCCESS or ERROR
a) On SUCCESS, remove form DIV and fade out/close the pop up
with a success message b)On Error,display a refreshed form DIV
And so on

Categories