I got a problem regarding request.getParameterMap()
In my jspx file multiply parameters are added when the user selects items out
of a table. now that i want to get them in my Controller
with request.getParameterMap(); it doesnt work with Firefox, Safari
but it works fine with chrome
anyone has an idea why it is so ?
thx for the replies .. the html form with the request parameters is build dynamicly at runtime using javascript, which seems to cause problems with widged based browsers like safari, firefox and most likely IE.
i will use ajax to get it work
The problem is not in the way that you are getting the parameter values from the map. It cannot be. The problem has to be in the HTML / Javascript / whatever that is creating the request on the client (browser) side.
It is most likely that you are doing something that is not strictly HTML (or whatever) compliant. Some browsers are treating it one way, and others another way. I suggest that you start by running an HTML validator over the page.
browser is not aware of request.getParameterMap() or any method in servlet api.
In your use case : you have a table of data , out of which user may select some rows and you need this data in server side for some action on those rows.
For eg : mark as read in GMAIL
select one or more rows in gmail and click ,mark as read will sent some request to google server and get those rows marked as read by user.
You can do it this way, you need to send primary key of your rows to server side with same parameter name
for eg: /delete?delId=1&delId=3&delId=7
and use request.getParameterValues() in server side to retrieve a list of ids to be deleted.
Related
Is it possible to send extra data attached to a http response via Java or Php?
My Website is a homework-platform: One User enters homeworks into a database, and all users can then see the homeworks on the website. The current load is very inefficient, as the browser makes two requests for eveything to load: One for the index file and one for the homeworks. For the homeworks request the client also sends settings of the user to the server, based on which the returned homeworks are generated by a Php script.
Now, I wonder, if it is possible, to combine those two requests into one? Is it maybe possible to detect the http request with Java or Php on the server, read the cookies (where the settings are saved), then get the homeworks from the database and send the data attached to the http response to the client? Or, even better, firstly only return the index file and as soon as possible and the homework data afterwards as a second response, because the client needs some time to parse the Html & build the DOM-tree when it can't show the homeworks anyway.
While browsing the web I stumbled across terms like "Server-side rendering" and "SPDY", but I don't know if those are the right starting points.
Any help is highly appreciated, as I'm personally very interested in a solution and it would greatly improve the load time of my website.
A simple solution to your problem is to initialize your data in the index file.
You would create a javascript object, and embed it right into the html, rendered by your server. You could place this object in the global namespace (such as under window.initData), so that it can be accessed by the code in your script.
<scipt>
window.initData = {
someVariable: 23,
}; // you could use json_encode if you use php, or Jackson if you use java
</script>
However, it is not a huge problem if your data is fetched in a separate server request. Especially when it takes more time to retrieve the data from the database/web services, you can provide better user experience by first fetching the static content very quickly and displaying a spinner while the (slower) data is being loaded.
Scenario:
We have one analysis which gives different results based on different inputs. So if the user open the same analysis in two different browser tabs, the session variables being common will get overridden and output will be same in both tabs though we want different outputs based on different user inputs in tabs.
So we plan to send a tab-id at the backend so that we save session variables per tab-id.
Is there some automatic way that tab information is being sent to the server like may be in request header or something like that??
Or we will have to generate a tab-id ourselves and send it with every request?
You'll have to generate your tab-id and pass it back with each request, but the following might make it a bit easier:
You can use sessionStorage from Web Storage API to store values unique to each tab. Every tab in the browser starts a new session so they are always distinct.
https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
It should work with most common browsers (even IE8+): http://caniuse.com/#search=web%20storage
Hope that helps!
I'm working on a project in which, after the user submits a form, I'd like to have them download a PDF file (filled out with information from the form) as well as redirect them to another page. I know how to redirect (returning an ActionForward which is set up to redirect to the correct JSP) and I know how to send the PDF (I build it using iText library, then write the byte array out to the HttpServletResponse's output stream), but I'm not sure how to do both without some sort of kludge.
I'm guessing I'll have to do one of the two using javascript/ajax, but I was wondering if there is a more elegant way to do this with struts.
Not with servlets.
What I end up doing in these situations is request the file with a timestamp as a request parameter.
When I write the response I set a cookie with that timestamp. Every few moments on the client side I check to see if that cookie exists, if it does, I window.location to the 'redirect' page.
I have a PHP page that grabs a variable via GET and then pulls in some information from a database based on that variable. Once finished with the server-side stuff, there is some javascript that runs and takes the data supplied and creates a .png image using a 3rd party API and then saves that image to my server using an AJAX POST call to another PHP page.
That all works fine, but what I'd like to do now is automate some calls to that PHP page. Namely, say I have 100 such variables to go through, I want to, preferably in Java with a for loop, call that PHP page with each variable in turn.
The problem is that client-side javascript. It won't execute with the simple URLConnection in Java. It seems like I need some sort of browser replicator or some way to have java act like it's calling the PHP in a browser?
Alternatively, I could make do with having a third PHP page act in place of the Java as the controller, but I'm faced with the same problem of getting the javascript to execute.
Am I missing something easy? Is this set up not possible? I'd really prefer to do it in Java if possible to fold it into other code I already have running.
Let me try to add some more specifics without bogging it down too much. There's a PHP file getData.php that takes in an ID number via GET. So I call it like ./getData.php?id=someId
That PHP file takes the ID, goes to my DB and retrieves some data and pastes it into the HTML source. Then once the page is finished, I have some javascript within getData.php that retrieves that data, formats it into a DataTable and passes it off to Google Visualization API in order to make a SVG chart.
Then I have more JS that runs that takes that SVG object, turns it into a Canvas object, grabs the base64 image data from it and finally POSTs to saveTo.php with the following array:
{'id' : id, 'data' : imgData}
saveTo.php simply takes in that POST data, creates a file on my server based on id and pastes the imgData into it. The end result is that I can pass in an ID to getData.php and end up with saved image of a Visualization chart that I want made based on data in my DB tied to that ID.
That all works by hand. But I have ~1,000 of these IDs and I'd like to have it so this whole process is run each morning so that I can have updated images based on yesterday's data.
I should mention that I did try using the 3rd party toolkit HtmlUnit (http://htmlunit.sourceforge.net/) but just keep getting these errors:
com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Some more searching around and hitting upon the right keywords to try finally led me to Selenium, which is exactly what I was looking for.
http://docs.seleniumhq.org/projects/webdriver/
Okay, this is going to be hard to explain but here goes nothing:
Lately I've been working a lot with POST and GET requests, but now I want to send a POST/GET request to this site called: http://www.mangareader.net/
The main problem I'm facing is that I want to use the search function of this site. Normally I would send a get request or something like that, but apparently this search function doesn't work that way, it works with some kind of Javascript code? I don't know exactly what it is, but try typing "Elf" in the search bar, and you'll get a drop down list of all the mangas (Japanese comics) with the word "Elf" in them. I want to know how this process is called, and how I can implement it into a Java program. For instance:
Login into a website
- > Send an HTTP post request. Get HTML data back. Process the HTML data. Get the information I need from the HTML source.
Using a search function on a regular site like google.com or bing.com
- > Send get request. Get HTML data back. Process the HTML data. Get the information I need from the HTML source.
Using search function on mangareader.net
- > ??????????
How would I achieve this? A theoretic explanation is enough, but a practical example would be great as well.
If you analyse the javascript that runs when search you get the following:
GET http://www.mangareader.net/actions/search/?q=test&limit=100 [HTTP/1.1 200 OK 113ms]
In other words, you can search on the site by a GET-request to
http://www.mangareader.net/actions/search/?q=test&limit=100
Where ?q contains your search word.
This site uses an ajax call to get a | ( pipe symbol ) seperated list from the page
/actions/search?q=term
It parses this list using string split and then makes it into combobox.
I have little experience with java, but a simple GET request to this page should work
replace {term} with your search function.
http://www.mangareader.net/actions/search/?q={term}&limit=100
You can use chrome network monitor to see if for your self