Iam new to JSP and i can't seem to figure out how to run code only when the user clicks on the button..here's my code:
$
<form action="list_computers.jsp" method="post">
Search:
<input type="text" name="FromTextBox1"/>
<input type="submit" value="Search it!" >
<%
String TheSearch = (String)request.getParameter("FromTextBox1");
String GetIt = Searcher(TheSearch);
out.println(GetIt);
%>
</form>
The Searcher() is a function i declared above this. Any help would be really appreciated.
You need to do something like
if (request.getParameter("submit") != null) {
// do work here
}
You also need to give a name to your button
<input type="submit" value="Search it!" name="submit">
When user clicks (or presses enter), request['submit'] will be equal to "Search it!"
I highly recommend moving this logic to the top of the page or even better to a controller.
you need to use javascript to check for the onclick event here's a little example with JQuery
$("input[type='submit']").click(function(){
//do your thing
// use event.preventDefault(); to stop submission if need
});
Related
I am attempting to process a form in Java with HtmlUnit. It works fine until it tries to find and click the submit button.
Here is what the form looks like,
<form method="get" action="result.php">
<p>Text: <input type="text" name="text"/></p>
<p>Agree: <input type="checkbox" name="doYouAgree" value="agree" /></p>
<p><input type="submit" name="Submit" value="Submit"/></p>
</form>
I've searched around a tried many different methods for retrieving the element but it consistently returns HtmlTextInput rather than HtmlSubmitInput.
form.getInputByName("Submit").click();
I've also tried processing a form with every type of input and no matter the type it always returns HtmlTextInput.
Has anyone seen this issue or know how to correct it? I'm concerned that this is the reason why HtmlUnit is not submitting any forms.
You can submit a form with a base object, as DomElement.
DomElement button = page.getFirstByXPath("//input[#name='Submit']");
HtmlPage new_page = button.click(); // or you can use the old page
should work.
I have a form with 2 submit type buttons(Yes/ No), i would like to handle this form with single #RequestMapping in my controller class. I certainly wish to handle multiple submit in single request mapping method only.
My first question is this possible. Can multiple submit buttons be handled with single request mapping of form action in the controller class ?
If yes, then below is the code I have written. Please suggest if this a correct way of implementing it or if it needs to be updated.
Currently, my code looks like this:
Form.jsp:
<form:form action="doAction">
<input type="submit" name="buttonClick" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClick" class="button" value="no, do nothing" />
</form:form>
Controller.java:-
private String buttonClick;
#RequestMapping(value = "/doAction", method = RequestMethod.POST, params="buttonClick") {
if("yes, do Something".equalsIgnoreCase(buttonClick))
//
else if("no, do Nothing".equalsIgnoreCase(buttonClick))
//
}
You can change the form action on button click e.g. to
"doAction?buttonClick="+<some value from clicked button>.
Or introduce a hidden input in the form. On click change the input value to reflect clicked button. Then the input is available on server side.
You can use a hidden field and change its value on every button click using jQuery:
<form:form action="doAction">
<input type="hidden" name="buttonClick" id="buttonClick" />
<input type="submit" name="buttonClickYes" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClickNo" class="button" value="no, do nothing" />
</form:form>
<javascript>
$(document).ready(function(){
$("input[type=submit]").click(function(){
$("#buttonClick").val($(this).val());
return true;
});
});
</javascript>
Good Morning all. I have an issue that I am trying to solve but I am stuck, partially due to me not having a full understanding of JSP/ Java platform, and not having a full understanding of what/ how to implement my code in the right way. I do have an ASP background, so I am a bit familiar with some concepts.
The issue I am running into is: I am trying to populate a dynamic label and text box to appear on a webpage (JSP). I have successfully been able to populate the textboxes with a piece of JavaScript code, however, when I submit the form (on Postback) my dynamic controls are gone but all of my static controls are still present. I need to find a way to keep my information in the dynamic control on submit as well. The form does not submit when I have errors but when the form finish rendering all information and dynamic text are all lost.
My Solution: I have decided to use hidden fields to hold this information but I am unable to fire off the hidden fields after form submit: Please have a look below
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
function validateForm(){
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="" && document.getElementById('two').value==1){
alert("EMPTY");
document.getElementById('text').style.display = "block";
return false;
}else{
document.myform.submit();
}
}
function display(el) {
if (el.value == "two") {
document.getElementById('text').style.display = "block";
}else {
document.getElementById('text').style.display = "none";
document.getElementById('dynbox').value = '';
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
<label >First Name</label> <input type="text"/>
<div>
<input type="radio" name="radio1" id="one" value="one" onchange="display(this);"/>Eligible
</div>
<div>
<input type="radio" name="radio2" id="two" value="two" onchange="display(this);"/>Not Eligible
</div>
<div id="text" class="glassy" name="mytext" style="display:none;">Other: <input type="text" id="dynbox"/>
</div>
<div>
<input type="hidden" name="mytext" value="${mytext}" />
<br></br>
<input type="submit" onclick="validateForm()" value="Submit"/>
</div>
</form>
</body>
</html>
What I am trying to do is select the "Not Eligible" option and type some text in the text box 'mytext' then press submit. Once I press submit, and if the First Name textbox is not filled in, then I want to trigger my hidden textbox so that I won't loose my information in my dynamically created textbox.
I hope this makes sense. I am not sure of how to do in JSP. Can someone give guidance on how to get my expected results? Keeping dynamic controls after Postback?
Thanks in advance.
One thing first: don't let the names Java and Java-Script confuse you. They have (almost) nothing in common. What you are using here is Java-Script only (no Java, no JSP anywhere).
Anyway, I think it is the condition in your validateForm() function that does not work.
I tested it a little bit and it works with these changes:
your form
<form name="myForm" action="test.html" method="get" onsubmit="return validateForm();">
<label >First Name</label> <input type="text" id="firstName"/>
<div>
<input type="radio" name="radio1" id="one" value="one" onchange="display(this);"/>Eligible
</div>
<div>
<input type="radio" name="radio2" id="two" value="two" onchange="display(this);"/>Not Eligible
</div>
<div id="text" class="glassy" name="mytext" style="display:none;">Other: <input type="text" id="dynbox"/>
</div>
<div>
<input type="hidden" name="mytext" value="${mytext}" />
<br></br>
<input type="submit" value="Submit"/>
</div>
</form>
I simply changed the first input and gave it an ID (since I couldn't find the id 'fname' anywhere), so we can access it with JavaScript.
The next thing is your submit button. You had an additional onclick="validateForm()"; there – which indeed calls the validateForm function but does not listen to the returned value (it executes the function.. nothing more). So the onsubmit="return validateForm()" is enough.
your validate function
function validateForm() {
var x = document.getElementById('firstName').value;
if ((x == null || x == "")) {
alert("EMPTY");
// document.getElementById('text').style.display = "block";
return false;
} else {
return true;
}
}
x can now be retrieved by the element's id (which I find easier).. Anyway, the if condition was one of the problems. In your form you set the value of your radio button 'two' to two. But in the if condition you ask if could be 1... which (in my understanding) should never happen. I just removed it to show you that your function works like this. If you want to check the state of the second radio button, you can test it like this:
if(document.getElementById('two').checked == true)
or even better
if(document.getElementById('two').checked)
one last thing: in your else statement, you can simply return true instead of calling submit.. because here:
onsubmit="return validateForm();"
you ask your validateForm() function to give you either true and then submit or false and then stop submitting.
Oh, one last thing (it'S the last, promise): Java and Java-Script handle all && and || in the order: first ´x==null´ would be tested and then x=="" && document.getElementById('two').value==1 would be evaluated together (&& has a stronger binding than ||).. Just for your information :)
I have 2 jsp pages and one Servlet. I am fetching data from database by servlet and sending result to result.jsp page where i am displaying result. But i want to add a Back button in result.jsp , by clicking back button i want to go to index.jsp, but problem is when i am clicking back button everytime a message is coming Confirm form submission and it is irritating me. How can i avoid this Confirm form submission? perhaps it is coming as processing is done in servlet.
index.jsp
<form method="post" action="Student">
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
Student servlet
String student_name=request.getParameter("studentname");
-------fetching data from database------------
-------------------sending to result.jsp------
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
result.jsp
//displaying data here
<form action="index.jsp">
<input type="submit" value="back"/>// a back button is here which is going to index.jsp
</form>
EDIT
From result.jsp i am going to another page result1.jsp like below
In result.jsp i have written the following:
<%out.println(student_name);%>
By clicking the above hyperlink i went to result1.jsp
I want to add a back button here(in result1.jsp) and after clicking i want to do to result.jsp, but when clicking back button i am getting Confirm form submission every time. I have written the following in result1.jsp
<input type="button" value="Back" onclick="javascript:history.go(-1)">
Still i am getting that message Confirm form submission. How can i avoid this? I want to go to result.jsp directly with out this message. How is it possible?
you can also use this to go one page back
<button type="button" name="back" onclick="history.back()">back</button>
You can write the below code that let's you to go index.jsp on your result.jsp page
Back
Try this
<button type="button"
name="back"
onclick='window.location='<your_path>/index.jsp'>back</button>
If you want a back button to go index.jsp, why not just make a normal link?
Back
There is only two way to get rid of the message, normal link or window.location. If the previous page is always the same, you don't need to use complicated function client side. If the page could be different, just post the link to result.jsp and use it to actually print a back link!
EDIT :
previous.jsp
<form method="post" action="Student">
<input type="hidden" name="back" value="previous.jsp" />
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
result.jsp
out.println("Back");
This is the easiest way to create a goBack button using the method goBack(). This is the same as clicking the "Back button" used on the top of your browser.
<!DOCTYPE html>
<html>
<head>
<script>
/* The back() method loads the previous URL in the history list.
This is the same as clicking the "Back button" in your browser.
*/
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<button onclick="goBack()">Go Back</button>
<p>Notice that clicking on the Back button here will not result in any action, because there is no previous URL in the history list.</p>
</body>
</html>
You can use a common button.
<input type="button" value="Back" onclick="javascript:history.go(-1)">
With history.go(-1), your browser will simply display previous page by reading from cache, it will not resubmit your data to the server, thus, no Confirm form submission will happen.
EDIT
I was wrong!
All you need is a client side redirect, you may write something like this in your result.jsp after it handles the form submitted from index.jsp:
response.sendRedirect("result.jsp");
So you will need a parameter to help you in result.jsp to tell whether it is a simple display request or a form submission, something like this:
String action = request.getParameter(action);
if("submit".equals(action)) {
// handle form data
response.sendRedirect("result.jsp");
} else {
// other code to display content of result.
}
In your index.jsp, it would be something like this:
....
<form action="result.jsp?action=submit" ...>
I experienced that form submit confirmation with Safari (not with IE/Chrome/FF).
The work around is submitting your form with "get" method. Of course this is only valid for "small" forms (max 2048 K).
Try this
<a href="${pageContext.request.contextPath}/index.jsp" class="btn btn-success">
Back
</a>
with bootstrap.css from here
I am trying to update the existing code of other developer. I am facing the problem of confusing actions.
Existing :
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
My Code to Update:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
My Javascript:
In my script, I try to submit my action by following code.
document.f2.action = "AddFoodAction.action";
document.f2.submit();
It seems like when I click [AddFood] button, it always call the [delFood.action].
For adding food, I need to check something with javascripts before calling the [AddFoodAction.action] action.
Due to limitations, I can't change the existing code. I can only add new codes to the existing one.
So, Any way to call [AddFoodAction.action] from javascripts without confusing with other actions of the same form ?
Thanks ahead.
Looks like a javascript problem here. Make sure the code
document.f2.action = "AddFoodAction.action";
document.f2.submit();
is executing. Maybe document.f2 is not resolving correctly (maybe more than one form with this name?).
This fiddle shows that it should work. It changes the action of a form inside a onclick handler on a <input type="submit">.
And just a reccomendation, don't do document.f2.submit();. It's an <input type="submit"> so it will submit the form automatically when onclick ends.
Finally, I resolve it by doing like that.
1) There will no direct action in form tag.
<s:form name="f2" method="post" enctype="multipart/form-data">
2) for delete part,
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
call the delete action from the javascript, not directly from form.
document.f2.action = "delFood.action";
3) for add part, like delete part. check necessary things in java scripts and call the add action. It works well.
Another Solution:
There maybe common action directly called from Form. For this approach, just name your button and give value and map those value from action class. And differentiate multiple methods by using those value from one action. I read this article at coderanch and javaSample. Thanks.