Problem with receiving data from jsp to servlet - java

I have problem with receive data from jsp to servlet.
I know that I must serializes this data using JSON.
In my jsp in JavaScript I make something like this:
var myJSONText = JSON.stringify(items);
document.getElementById('test').value = myJSONText;
where, items is an array of JavaScript objects, and test:
<input type="hidden" name="test" id="test">
Now I want to receive this array on the servlet, I'm trying that (in method doPost()):
request.getParameter("test");
but it contains an empty value. Has anyone idea how to do it?

Can you show your code, when you do post request to server? Seems it should be something like
$.post('http:myurl.com', data)
or
<form ...>
<input type="submit">
</form>

Related

Passing multiple values from one page to another

I have the followin question, how will I pass multiple values from one jsp page to another? I have i piece of code here, which works fine, but it only sends one value from one page to another (year):
<form method="post" action="Display data.jsp" name="inputpage" >
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
</select>
For example if I had another value, for example
String str = "value";
Is it possible to send it using form post method as well? I googled it, and the answer I found included loops and too much code, is there short and simple way of doing it? Many thanks!
When you submit the form all values of the form will be passed, they only need to be inside the form. You can read other values normally by using:
request.getParameter(ParamName)
Take a look at this article for more information
You can send as many variable you want by Form Method.
For sending the value of String Str, assign its value to hidden field as:
<input type="hidden" id="hidden1" value=<c:out value="${variableName}" />
where variableName=str.
Could you use a hidden input inside your form to pass other data using the form post?
<input type='hidden' id='myExtraData' value='hello' />

How to manipulate a variable before sending to servlet

I've got a JSP page which contains a textbox, wrapped in a form. This form's action is set to a servlet.
I would like to manipulate the string (from the user's input in the textbox) before it is sent to the servlet, thus basically carrying out a simple request.setParameter call from the JSP to the servlet. Can this be done? If so how can I obtain the textbox's value in the JSP?
<form action="MyServlet" method="post">
<input type="text" name="txtUsername"/><br/>
<input type="submit" value="Submit"/>
</form>
You cannot do this using JSP code.
Remember, a JSP is processed, outputting its contents to the browser; that's where the JSP's request/response cycle ends.
Your options are:
Using JavaScript.
Using a Filter: http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html
Call a Javascript function on submit e.g. below:
function fnSubmit(){
document.getElementById("txtUsername").value = "new Value";
document.forms[0].submit();
}

how to handle response contents by AJAX

for me it seems impossible but expecting clarification on it. i am sending a request as follow :
<form action="/name" method="get">
<input type="text" />
<input type="submit" />
</form>
Now action class at server side manipulates & send the response to client, can i handle this response by ajax somehow ??
Yes, but you have to submit it via ajax (XmlHttpRequest) in order to be able to get the response that way.
Using jQuery makes this simple:
$.post("/name", {param:param}, function(data) {
});
In that example you should pass manually each form field as param. In case of bigger forms this is not that good. So you can use serialize():
$.post($("#yourForm").attr("action"),
$("#yourForm").serialize(),
responseHandlerFunction);

can we include both a file type and text in a form in sending email using jsp

I'm developing an email program using JSP. In that I need to send data as well as upload file.
<form name="email" enctype="multipart/form-data" method="post" action="/servlet/sendmail">
<input type="file" name="namefile" size="40">
<input type="text" size="100" name="sub">
<input type="submit" name="submit" value="send">
</form>
In servlet java program I can upload the file but the text fields returns null.
In doPost() method,
String msg=request.getParameter("sub");
Here getParameter() method returns null for text fields.
Can we include both file type and text in a single form with enctype="multipart/form-data"?
Yes, that's possible. You should obtain the text field using the same API as you've obtained the uploaded file. It's unclear which one you're using to obtain the uploaded file, so I can't give a detailed answer. But a defacto standard API to parse multipart/form-data requests is Apache Commons FileUpload and I've posted an answer before how to do that right here. To the point, you need to handle cases as well where FileItem#isFormField() returns true. This indicates a "regular" form field.

in JSP is there a way to pass HttpServletRequest object as an attribute to another HttpServletRequest object

I am using Struts/JSP for a webapp. I have a page A where I am collecting user defined parameters (as request parameters can't make them session params) and then I go to page B to ask yes/no kind of a question to the user. In case of yes I need to come back to page A and continue regular processing. But obviously the request object for page A is gone.
Is there a way to set page A's request object as parameter in page B so that when I come back to page A i have the same request object I had when i was there (on page A) the first time.
I need something like below:
page A --(req1)------> page B (set req.setAttr('prevReq', req1)) ------> page A (req = req.getAttr('prevReq'))
Any help is appreciated.
No, you can't do what you have in mind. Do you understand how the HTTP request-response cycle works?
User sends HTTP request to the server using a browser.
Server processes the request (your servlet or JSP is called).
Your servlet or JSP produces a response which normally consists of an HTML page.
The server sends the response back to the browser.
There is no way that you can save the request for page A, and then in page B respond to that request to make the browser go back to page A. That's just not how the request-response cycle works.
What you can do, is store data in the session object. You can call request.getSession() to get a HttpSession object, in which you can store data for the duration of the session of that user. In page A, you can get the data out of the session object again.
In a multi page process you will need to store all the intermittently gathered data into the session. See HttpServletRequest.getSession() and HttpSession.setAttribute(String, Object).
Use hidden input elements (input type="hidden") wherein you retain the request parameters of the form submit. Don't duplicate/store it as request attribute. They get lost when the response is finished.
Since I don't do struts, here's a basic example how the JSP should look like (leaving input labels and obvious security issues like XSS outside consideration, Struts should be smart enough to handle it itself).
Page A:
<form>
<input type="text" name="input1" value="${param.input1}">
<input type="text" name="input2" value="${param.input2}">
<input type="text" name="input3" value="${param.input3}">
<input type="hidden" name="yesorno" value="${param.yesorno}">
<input type="submit" value="go to page B">
<input type="submit" value="submit">
</form>
Page B
<form>
<input type="checkbox" name="yesorno" value="yes" ${!empty param.yesorno ? 'checked' : ''}>
<input type="hidden" name="input1" value="${param.input1}">
<input type="hidden" name="input2" value="${param.input2}">
<input type="hidden" name="input3" value="${param.input3}">
<input type="submit">
</form>

Categories