send large text to server using html form hidden value - java

HI all
I need to send an dynamically generated html to server using html form, html can be bigger size at present it is 1MB
I m sending an dynamical generated html to server using form hidden input field. at server side exception is : too large content..
The dynamically generated html is used to generate pdf and generated pdf will send back to browser in same request of response.
How to handle bigger size html which is generated dynamically.
Please help me out.
Thanks
kumar kasimala.

If you use asp.net you can do like below:
In Web.config file, add
<httpRuntime maxRequestLength="100000" executionTimeout="360"/>
under <system.web>

I googled a bit on your problem and hit upon this page which says that the post size is set by the server in its configuration and can be changed by resetting it - http://forums.sun.com/thread.jspa?threadID=5400480

If you use java:
Use servlet post method to transfer the data to the server
<form method="post" name="sample_form" action="/xxx">
....
</form>

Split it.
Try to split the content to multiple inputs. Your implementation might have problem with that... but that's not too probable
Split the content to multiple requests and send them with AJAX. Collect the responses and be sure to send it in the right order (not all requests at once). Last request should confirm it's the end and load a page returning the pdf

Related

Problems logging in to a webpage using java and jaunt-api

So I'm trying to log in to a webpage using Jaunt. The first thing to mention is that the webpage is .aspx and the submit button has an option onclick="javascript:WebForm_DoP..." and as far as I know Jaunt doesn't support Javascript right?
In case I'm wrong, the code I'm using is the one in the examples of Jaunt:
Form form = userAgent.doc.getForm(0);
form.setTextField("Login1$UserName","USER");
form.setPassword("Login1$Password","PASSWORD");
form.setCheckBox("Login1$RememberMe",false);
form.submit("GO");
System.out.println(userAgent.getLocation());
All the names and values are correct, and the user and password works since I can log in using the web browser. After I execute the code, in the output I get this:
message: UserAgent.sendPOST; Connection error requestUrl:
http://webpagehere.com/default.aspx [posting
__VIEWSTATE=%2FwEPDwUJLTk5MDc0NjQ2ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAgURTG9naW4xJFJlbWVtYmVyTWUFF0xvZ2luMSRMb2dpbkltYWdlQnV0dG9upWcarODJIwpeMt8HCmfaBn6iMWI%3D&__VIEWSTATEGENERATOR=CA0B0334&Login1%24UserName=USER&Login1%24Password=PASSWORD&Login1%24LoginButton=GO]
response: [none]
The form div is this one:
<form name="form1" method="post" action="Default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1" style="text-align:center">
Any ideas what could be my problem? In case Jaunt doesn't allow me to do this login, could someone please recommend me a library for web scraping and interaction? Thanks!
Seems like you are stuck. Actually .aspx pages uses AJAX pagination. You will have to extract the values of __VIEWSTATE, __VIEWSTATEGENERATOR and all other form values and then send them with POST method in the request body. You can use Fiddler to get the request body which contains all these hidden variables and your entries to the form.
In Java you can use Selenium Or HTMLUnit which are Java GUI-Less browser, supporting JavaScript, to run agains web pages.
edit: You can use Jaunt-api as well, I just tried it with it, all you do is send a POST request alongwith the request-body, you can easily check it with Fiddler, and it works!!
Form values in HTTP POSTs are sent in the request body, in the same format as the querystring. You can find the request body of a link by inspecting it using the Fiddler and then copy request body from Textview and send the encoded data as request body.
UserAgent userAgent = new UserAgent();
userAgent.sendPOST("<your link to form page>","<request body>");

send HTML from servlet to JSP dynamically

Is there an easy way to send HTML from a servlet to a JSP, using AJAX.
I've already figured out how to make AJAX work with servlets dynamically, but now I want to press a button on a form and generate HTML based on text-input.
Is it possible, and if so, how, to send just pieces of HTML to an existing HTML page?
Example,
I have a basic form where you can input your age, and based on the age the text has a different size/color. So, you send for example, 25 as your age to the servlet, and it send back a piece of HTML like this <p STYLE="font-size: age;"> to the page.
Through ajax call you can get the output result either a string, html or a Json object that will be parsed and results can be displayed over JSP/HTML. So for sure you can send html code segment from servlet to jsp through ajax call.
For example you can use this approach--
1. Take a string variable in your servlet.
2. Put appropriate html string as per your conditions in this string variable
3. send this string as a response from servlet like:
response.setCharacterEncoding("UTF-8");
response.getWriter().write("your string variable here");
4. In your ajax call do like this:
success : function(dataString) {
document.getElementById("containerId").innerHTML=dataString;
},
where containerId is the id of html element (like div or span) where you want to display output html.
The easiest approach, without client-side javascript libraries, would be to point an HTML form to an iframe, just like
<iframe name="myIframe"...>
<form target="myIframe"...>
And submit your form as many times as necessary. The HTML returned by the servlet would load itself in the iframe element.
If you like AJAX and client-side javascript libraries, you can find very easy programmatical ways to do this in jQuery and similar libraries.
Basically your servlet can generate any kind of content, e.g. JSON, HTML etc.
You'd then send that content back to the client and integrate it into the page.
How that is done depends on the type of content.
Example:
You issue an AJX request (e.g. by using jQuery's ajax functionality) and your servlet generates plain html. When your JavaScript receives the anser you just replace the relevant part, e.g. by replacing the content of some defined element.
If you used JSON instead, your servlet might send data only instead, e.g. a font size based on the age as in your example. You'd then use JavaScript to access that JSON data and perform relevant operations, e.g. by changing the style of the paragraph.

display an image from server with an output stream

I am trying to show an image from the server in my browser.I am following this link
http://balusc.blogspot.in/2007/04/imageservlet.html. i must say this is pretty well written and documented. I tried this and everything is working fine.
The problem is there when i am using ajax to display this image.the whole image seems to break into some codes inside the div.
i understand that the outputstream used in the code is writing directly to the page.But is it really not possible to handle that outputstream to somehow display the image in image tag of a jsp without having to create a different servlet.
Thank you for reading
You don't need to request image data via AJAX and then manipulate it yourself, in order to display it. Just use an <img> tag!
If /my_url is the location of your image, then
<img src="/my-url" alt="Appropriate description"/>
would do it. NOTE: /my-url doesn't have to be an actual image. It can be any resource (including a servlet) that returns image data with the appropriate MIME type set.
If you want to create the tag dynamically, you can use your favourite library, or do it iwth native JS:
var oImg=document.createElement("img");
oImg.setAttribute('src', '/my-url');
oImg.setAttribute('alt', 'Appropriate description');
oImg.setAttribute('height', imgHeight);
oImg.setAttribute('width', imgWidth);
document.body.appendChild(oImg);
Edit
If you want to be doing this server-side (and if so, is this really AJAX?), then you might want to look at the data uri scheme.
With this scheme, you can data directly to an image tag, without needing to provide it with an HTTP resource. To use this, you convert your outputstream to base64 and use the following:
<img src="data:image/png;base64,converted-data-stream-goes-here..." alt="Who needs HTTP?"/>
The image/png would change depending on the MIME type of your source data.
Read the linked Wikipedia page to fully understand the trade-offs here.
Just adding how i achieved a solution for it.
I referred to a new page(from the page i am submitting the form) through ajax and in the new page i used an image and through its src attribute i called the servlet method which is writing the image through its outputstream.
This way the servlet is writing my image to the new file which i can position anywhere.

Strange behavior with the method getUploadedBlobs

I've a problem with the methode blobstoreService.getUploadedBlobs(). I've a JSP page in wich one I set an uploader like this :
<formname='form' action='<%= blobstoreService.createUploadUrl("/Edit_Engine") %>' method='POST' enctype='multipart/form-data' >
<input label='...' multiple='false' name='myFile' />
//...and multiple input for text
</form>
and I retrieve this code with my servlet :
java.util.Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobK = blobs.get("myFiles[]"); //I don't know why I need to add the characters 's[]' at the end...
But the behavior is strange. The first time I upload an image, everything works. However, the second time, I send my form without uploading somehting (only text data), and then my java code finds a BlobKey. But this BlobKey seems to be the previous sended data, or a corrupted data.
I mean that not normal, because when I deploy this version on my localhost, if the form uploads no file the method getUploadedBlobs returns an empty HashMap. However, when I deploy on google servers, if the form uploads no file, the method getUploadedBlobs seems to return a HashMap with wrong data.
Could you help me? Or tell me if this behaviro is normal...
Many thanks,
bat
If you're getting a valid BlobKey, then myFiles[] is most likely the name given to the file input field in the form. Is that the case? That seems like an odd name for an input field. Are you using a template library to help generate HTML from the JSP?

how to show "data loading" in a jsp page using servlets

we have a classic JSP + Servlets application and would like to show "Content is loading" sort of message when data in the page takes a while to load.
Is there an easy way to do this via JS?
Scenario:
Page1 (a.jsp) -> select drop downs ->
click search //data is sent back to
server for db
URL changes to (b.jsp), white page is shown, then data load after 30ish seconds
for those 30 seconds I want to show a spinner or some message.
adding ajax or jquery would require a design change which we can not do right now. Though the application already uses jQuery for other stuff but b.jsp is making the DB call from that page...
calling DB in jsp would be very much frowned upon. given the premise of the question, you could
//b.jsp
<div id="msg">data is loading...</div>
<%
out.flush();
db.performanLengthJob();
%>
<script> $("msg").remove(); </script>
<p>Data is loaded!</p>
AJAX is the way to do it. You'll need something on the server side to help you keep track of progress.
Load your data (in b.jsp) with ajax request.
Make a JS function which starts on load, let it show your message ('30 seconds left') and then let it perform ajax request to load necessary data.
I recommend you to use some js-framework, like jQuery, it'll make things much simplier.
In order to handle ajax-request on the server-side you'll need to create one more servlet and map it to some url, like your_app_name/ajaxsupport. This servlet should return data in some convenient format (it can be plain text, or xml, or JSON or whatever else). On the client you'll process received data and show it.
A quick solution can be to use onbeforeunload JS event of your base page html body s.g. like
<body onbeforeunload="$("yourComponent").show();">

Categories

Resources