Applet web app interaction - java

I have an applet that calls a URL, on a user action, and passes some parameters. These parameters are executed on a ROR server and then I update a partial, which is on the applet's page, based on the result. However, the partial is not updating.
The console says that the partial is rendered but it is not. Is it because I am calling the URL from the applet and the server is unaware that the applet is in the current session, hence it does not know which session partial to render?
Here is the code of the controller:
def add_point
#comments = Comment.find(:all)
render :update do |page|
page[:comment_text].value = ""
page.replace_html :comments_partial, :partial => 'comments', :collections=>#comments
end
end
EDIT: I use Apache commons http wrapper to send the request:
PostMethod post = new PostMethod("http://localhost:3001/vizs/add_point");
post.addParameter("upload[test]", dataImg);
post.addParameter("upload[user_id]", new Integer(user_id).toString());
post.addParameter("upload[viz_id]", new Integer(viz_id).toString());
I think I need to set additional request variables but I am unsure what to set....
Any suggestion on how to solve this?
Thanks,
Slotishtype

rails is not aware about the applet, and does not know how to work with it, you need to send xml or json or even html:
def add_point
#comments = Comment.find(:all)
render :partial => 'comments', :collections=>#comments, :layout => nil
end
parse it in applet and fill appropriate fields by yourself.

Related

Junit / HttpUnit poor support on Javascript

I have an application where everything starts from a single servlet and then based on some params to initial servlet complete behaviour of application changes, there are lots of things and forwarding which happens until you get response as a HTML.
I want to write test cases, where I only change those few initial parameters and get a response either as getText() or getXml() or getHTML().
I used HttpUnit, something like.
WebRequest request = new PostMethodWebRequest("http://localhost:9090/xyz/ETransServlet.fsc" );
request.setParameter( "window_id", "1" );
request.setParameter( "screen_id", "4" );
request.setParameter( "nav_to", "something.jsp" );
request.setParameter( "nav_from", "somethingfrom.jsp" );
HttpSession session = sr.getSession(true);
WebResponse response = client.getResponse(request);`
Now you see I have some parameter set window_id and all.
first thing, they are not reaching somehow.
secondly, even if I call some page with no params, still it gives error something like can't process script <script> blab bla </script>
It's unable to process javascript code that we are using. I have js.jar and nekohtml.jar in build path and beside I don't want it to validate javascript or html at all.
Can anyone tell what's wrong or how to stop javascript to validate or fix it even if it validates or not or some other solution entirely.

Establish Connection First, Redirect User Second

I have an idea to make something pretty sweet but I'm not sure if it's possible. Here is an example of a very basic ajax function that I might use to establish a connection a server...
function getFakePage(userId)
{
var ajaxObject, path, params;
ajaxObject = getAjaxObject();
params = "?userId=" + userId
path = getInternalPath() + "someServlet" + params;
ajaxObject.open("GET", path, true);
ajaxObject.send();
// On ready state change stuff here
}
So let's say I have a URL like this...
https://localhost:8443/Instride/user/1/admin
And I wanted to use javascript to redirect the user to this this URL. Normally I would just do this...
window.location = "https://localhost:8443/Instride/user/1/admin";
But my idea is to create a javascript (no js frameworks please) function that could combine the ajax code with the window.location code. Basically what I would like to accomplish is to create a connection with the server via ajax, send a servlet on that server the url I would like the user to be redirected to, and then redirect the user to that URL. So that for however long it takes the user to connect to my server from wherever they are in the world they see a loading icon instead of a blank white page.
So to clarify exactly what I am trying to accomplish; I do not want to put window.location within the success of my ajax function (because that would be encompass two round trips), and I do not want to return a huge chunk of HTML for the requested resource and add it to the page. I want to establish a connection to the server with ajax, send a servlet the URL the user wants to go to, and then somehow override the ajax function to redirect that user. Is this possible?
And I know some of you might think this is stupid but it's not when you're talking about overseas users with slow dial up connections staring at white pages. If it's possible, I'd love to hear some insight. Thank you very much!
First, let me say that the best solution is finding what is causing the slowness and fixing it.
Now as to your question, yes you could do it. You could even shoehorn it onto an existing application. But it wouldn't be pretty. And it comes with it's own set of problems. But here are the steps:
Browser calls ajax cache service requesting "somepage.html"
Browser loads loading icon
Server creates somepage.html and caches it in a temporary cache, (ehcache or other library would be good, probably with file backing for the cache depending on size)
Server responds to ajax request with ID for cached page
Browser now redirects to "somepage.html?cacheId={cacheId}" where the id is from the ajax call.
Server uses a filter to see if any cache can be served up for the page instead of the actual page, thus speeding up the request.
Having said that, it would be better to just have the new page load quickly with a loading icon while it did any of the heavy lifting through ajax.
You can't do an AJAX request and a location change in one. If you want to do only one request you have to choose one of those methods. ie. return some data and replace content on your current page, or load a completely new page.
It doesn't make any sense to want to want to do both. What you could want is stateful URLs; where your URL matches the content displayed, even if that content comes from an AJAX request. In that case an easy solution is the use the # part of the URL which you can change freely (window.location.hash). Some modern browsers support changing the whole URL without causing the page to reload. I've used # with great success myself.

redirect to a different url

I have one application where i have three jsp pages, from index.jsp , control goes to process.jsp and after execution control goes to result.jsp to display data. But i want that instead of displaying data in result.jsp, control will go to another url so that that receiver url will get the requested data. that is: my url is 100.20.3.45:8085/myproject/index.jsp then after processing data i want that result should go to a different url of my same network i.e. 100.20.3.46. How can I send the requested data to this different url?
Ex:
100.20.3.45:8085/myproject/index.jsp
goes to
100.20.3.45.8085/myproject/process.jsp
after processing control will go to 100.20.3.46.
How can I send this data to a different url? what is this mechanism called?
It's called "redirecting". It's to be achieved by HttpServletResponse#sendRedirect().
response.sendRedirect(url);
If you want to send additional data along, you'd need send it as request parameter(s) in a query string, but they will be visible in the URL in browser's address bar. If that isn't affordable, consider storing it in a shared datastore (database?) and then pass alone the unique key along.
Alternatively, you can also just let the <form> action URL point to that different host directly without the need for an intermediate JSP. As another alternative, you could also play for proxy yourself with help of for example URLConnection.
Unrelated to the concrete problem: having controller/business logic in JSPs is a bad practice. I suggest to take some time to learn about servlets.
Redirect to URL
window.location.href = "url"
Examples of use
window.location.href = "/process_payment";
var username = #json($username);
window.location.href = '/' + username;
window.location.href = '/{{ $username }}';

Easiest way to submit a form to ServletUnit using something like WebRequest in HttpUnit

I would like to programmatically create a form with fields etc, however i have not been able to find a public factory etc to create a WebForm(class). Once this is done i would like to then submit the form and have my servlet do stuff with the form.
One approach i noticed the tests use is to create a PseudoServer which is a simple socket server. The tests then eventually make a request to some url which replies with some arbitrary html which includes a form. The problem with this is i cant register my own custom servlet to do stuff.
Im thus stuck between wanting a form but being unable to create one, if i wish to unit servletunit.
Is there a way to submit forms to a servlet inside servlet unit ?
Is there a way to combine parts of httpunit the form submitting stuff w/ servlet unit ?
Im guessing probably not because it(httpunit) wants to submit a form via socket and servletunit does not use sockets at all.
As per Andrey's suggestion and my past experimenting i have attempted to to call numerous methods on WebRequest to attempt to communicate the stuff that exists in a form being posted to a server.
selectFile() - to pick the file to be uploaded
setHeaderField() to set content type/charset/encoding.
You can use PostMethodWebRequest to send POST request to any HTTP URL:
WebRequest request = new PostMethodWebRequest(serverUrl);
And then just set form parameters directly in the request object:
request.setParameter('name', 'user1');
request.setParameter('password', '123456');

Servlet 3.0 include html page

I'm trying to achieve the following behavior using the Servlet 3.0 API:
send an inital html page
send subsequent responses that update the page
This all works except the only way I could send the initial page without getting the response committed is by manually writing using the HttpResponse Writer...
I was wondering if there is a way of using something similar to RequestDispatcher#include with an html page without running into problems with the AsyncContext. Some things I tried until now and didn't work:
use AsyncContext#dispatch: as much as I read on the Internet, it is destined for sending the final response to the container in order to render it
use RequestDispatcher#forward: getting IllegalStateException due to trying to write more content in the OutputStream
use RequestDispatcher#include: if I initialize the AsyncContext before calling this method, request.isAsyncSupported returns true, after calling the method, it returns false. I read that it calls flushBuffer() and sets the commit flag to true on the response
Also, in the Servlet 3.0 spec there are some lines mentioning that dispatching from async servlet to normal servlet is possible but will commit the answer. I believe a static html page belongs to this category...
If you have any ideas of how an elegant include can be done without affecting the ability to still send streamed responses back to the client, please let me know.
Thanks
One solution (not the only one): if it's just a html page, then write the html page itself in html and do ajax calls to the serrvlet that needs to provide the updates.
use static elements on the page that store data and use requestdispatcher.
Or you could also just simply refresh the entire page with such an arrangement using response.setHeader("refresh", "5; URL=officer.html").
I really don't understand your need to send multiple requests without the response being committed to a servlet. are you trying to interact with a serving thread multiple times ?

Categories