In 1.jsp I have this input boxes
<input type="hidden" id="muncId" name="muncId"/>
<input type="hidden" id="muncDesc" name="muncDesc"/>
I want to send the values in 2.jsp, 3.jsp, 4.jsp etc.
The form submit is for one page only. How should I do that in multiple pages?
the other alterante for hidden form field is to use session.Kepp the values in session
In 1.jsp keep the values in session like this
session.setAttribute("value","hi");
now in other pages 2.jsp,3.jsp,4.jsp retrive the values from session like this
session.getAttribute("value");
In your 1.jsp page include one form for each target page containing the same hidden fields.
Add a little JavaScript to your submit button to submit all forms on click.
Related
So the question is like we manipulate the values of the form fields through servlets by using request.getParameter() can we do the other way round i.e set the form feild values fromthe servlet itself?
Basically what I am trying to do is to create a dropdown and some textboxes. the dropdown gets the ids from the database , when user selects a particular id, the text boxes should get filled with the other values from database for that id so the user can either edit them or leave unchanged accordingly.
For ex: The html code is:
<form action="GetValues">
<select name="ids"><option>1</option><option>2</option></select>
<input type="submit" value="Edit">
</form>
<form action="Save">
Product name:<input type="text" name="name" id="tb1"/></br>
Price:<input type="text" name="price" id="tb1"/>
<input type="submit" value="Save">
</form>
The Getvalues servlet establishes the database connection and gets the values of name and price from the datbase which I can do, but how to display those values in the two textboxes?
I can than make the Save.java servlet to get the values from textfields and update into the database.
I am not at all comfortable using JSP scriptlets. I want to do this using servlets only.
I know I could have created textboxes using the servlet itself but that won't work for me because that makes my jquery on the form die.
Like we do in javascript:
var x="hii";
document.getElementById("tb1").value=x;
Is there anything like this in Java too?
I know you requested no scriptlets, but IMO this is the easiest way to do it.
In your servlet, set a request attribute as such:
request.setAttribute("attributeName", attributeValue);
Then in your jsp, you can access the attributeValue like this:
<%= request.getAttribute("attributeName") >
Edit: For the followup question in the comment, here is how you can show the IDs in the select box using scriptlets:
First, set the list of IDs in the servlet:
List<String> idList = ...;
request.setAttribute("idList", list);
Then in your JSP, construct the select field as follows:
<select name="ids">
<%
List<String> idList = request.getAttribute("idList");
for(String id : idList) {
%>
<option><%=id></option>
<%
}
%>
</select>
. the dropdown gets the ids from the database , when user selects a particular id, the text boxes should get filled with the other values from database for that id so the user can either edit them or leave unchanged accordingly.
That is the perfect place to use a AJAX call. Please fire a AJAX request and get the values from Database.
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();
}
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>
This is a follow on to my previous question:
JSP / servlet / IE combination doesn’t submit form detail on Enter
Inside the form, we have:
<input ... type="submit" name="confirm" value="Confirm"/>
There are no input fields on this form. This form appears at the end of a workflow and is essentially a verification to proceed.
This form is not submitted to either IE or Firefox when the Enter key is pressed. It works perfectly if the "Confirm" button is pressed.
Following on the answer to my previous question, I have tried adding dummy fields such as:
<input type="hidden" />
or
<input type="text" style="display: none;" />
but they make no difference.
For various reasons, we would prefer not to use Javascript.
Any suggestions to get the Enter key to submit?
Unfortunately, you should have at least one focusable input element in the form to get that to work and then only when the input has focus. If you don't have any input elements, then there's no other way to get around this than by letting Javascript to listen on the enter key in the body.
<body onkeypress="if (event.keyCode == 13) document.formname.confirm.click();">
Where formname is the value of the name attribute of the parent <form> element.
Note that I used document.formname.confirm.click() instead of document.formname.submit(), because otherwise IE wouldn't send the name=value pair of the button to the server side.
The only Javascript-free way would be to let the user to tab to the button and then press enter. This all is by the way regardless of the browser used and thus not IE specific.
I have got two forms in a page. The second form has got a file upload input component along with other controls. My requirement is to submit the second form with the file and refresh only that form without refreshing the other form. If it was only normal components, I could have done this easily in Ajax. But when the form is having a file component, I feel its not that straigh forward. Please suggest any ideas to do it???
You can still use AJAX on a form with file components. Maybe you can use the jQuery library (if you are not already) since that makes these tasks trivially easy.
Put the second form in an iframe.
The way I have done it in the past is to hide an iframe on the page. Then set the target of the file upload form to the name that was given to the iframe. If you need to be xhtml compliant you can use JavaScript to create the iframe after the page loads and to set the target on the form. The code will look something like this. You can apply css to the frame to hide it.
<iframe name="myFrame" src="blank.htm"></iframe>
<form action="uploadFile.php" method="post" enctype="multipart/form-data" target="myFrame">
<input type="file" name="myFile"/>
<input type="submit" value="Upload"/>
</form>