HTMLUnit thinks Submit Button is a text input - java

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.

Related

Get value of <td> in Java - request.getParameter

Im trying to get the values of some td elements where the data consist of data from MySQL table. It displays the data fine in my browser (e.g. if i change type from "hidden" to "submit"), but when I try to get the value i only get null.
Here are my jsp and it displays the correct results in the browser.
<td>
<form action="history.jsp" method="get">
<input type="hidden" name="res" value="<%=his.getRes()%>"/>
</form>
</td>
When i try to print the values however, I only get "null" at of evey :
<%
String res = request.getParameter("res");
System.out.print(res);
%>
I'm still very new, so it's proberly a straight forward answer. Thank you in advance for the help.
I suggest that you change the name of your variable :
String newname = request.getParameter("res");
System.out.println(newname)
One can submit (=send) only one <form>. So one must assume there is just one single td with one <form>. Forms also may not be nested in an outer form.
It need some way to submit the form.
So experiment first with:
<td>
<form action="history.jsp" method="get">
<input type="text" name="res" value="<%=his.getRes()%>"/>
<input type="submit" value="Send"/>
</form>
</td>
This will show whether his.getRes() yielded something. And allows a manual submit in the browser.

unable to retrieve the passed value from jsp to controller using url via a button

I am assigning href to a button and passing a value to the controller
JSP page
<td><input type="button" name="remove" value="DELETE"/>
</td>
In controller, I am trying to get the value when the button is clicked using:
if(request.getParameter("remove") !=null)
{
int cart_id=(Integer)request.getSession(false).getAttribute("cartid");
b_id = (String) request.getParameter("book_id");
int bid=Integer.parseInt(b_id);
System.out.println(bid);
}
This prints a null value though I have passed book_id value in the URL.
I want to know how can I get the value passed in the URL via the button.
You can't combine [a] tag with an [input] tag like that.
Try using a form instead with hidden inputs:
<form action=viewController>
<input type=hidden name=remove value=delete>
<input type=hidden name=book_id value=<%=vo.getBookId()%>>
<input type=submit value='Delete'>
</form>
The resulting url will be : viewController?remove=delete&book_id=...
When submit button is pressed, the entire form will be sent. You can select where data will be sent by using attribute action:
<form action="demo_form.jsp">
<!--inputs here-->
<input type="submit">Send me now</input>
</form>
In that case form will be sent to demo_form.jsp. In HTML5 you can use formaction attribute if you want use different servlets for different buttons
Any way, you shouldn't use links for sending forms.
It is possible to use links without button:
Remove

How to send label parameter from JSP to servlet?

I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx

go to jsp page by hitting back button

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

JSP / servlet / IE combination doesn't submit form detail on Enter

Using IE 7, JDK 1.6 and Sun Web server 7.
Inside the jsp form, we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="send" value="Send"/>
i.e. a text box and a "Submit" button (called Send).
and the servlet has:
if (request.getParameter("send") != null && request.getParameter("send").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent when we populate the id text box and hit Enter:
id=123456
However, using Fiddler and IE, we can see that the following is sent when we populate the id text box and click the Send button:
userId=123456&send=Send
The end result is that hitting the Enter key effectively does nothing.
On other jsp pages, e.g. we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="submitId" value="Submit"/>
and the servlet has:
if (request.getParameter("submitId") != null && request.getParameter("submitId").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent for both cases:
id=123456&submitId=Submit
So it seems to us that the behaviour is only exhibited on IE for forms where the "Submit" button is not called "Submit"?
Re-running the tests on Firefox 3.6 shows that the behaviour is correct and the same for both cases.
Any suggestions for getting IE to work correctly?
(Note: I have searched SO for a similar problem but the questions relating to this mainly all ASP related!).
This is indeed another IE anomaly in case of forms with only one input field. The only solid workaround for this is to add a second input field(!). You can hide it using CSS. No, type="hidden" ain't going to work.
<input type="text" name="id" maxlength="20" />
<input type="text" style="display: none;" />
<input type="submit" name="send" value="Send"/>
Why are you checking for request.getParameter("submitId") in your JSP when in fact submitId is the name of your submit button?
In my experience I never had to check the value for the submit button. I only used that button to trigger the form submit and would usually only be interested in retrieving the values for the other form parameters.
If you want to differentiate the submit methods by the name of the submit button, you might want to try adding a "hidden" property using input type="hidden".

Categories