request.getParameter("String") always returns null. Why? - java

// jsp snippet
<button name="TesterButton" value="TesterButton" onClick="location.href='TesterServlet';">
Servlet Snippet of class TesterServlet
writer.println( (String)request.getParameter("TesterButton") ); // always returns null
The above servlet always returns null. Why does it return null ?

It's working exactly as you told the code to do: changing the window location to the given URL. So, no surprises.
What you actually want should be coded as follows:
<form action="TesterServlet">
<input type="submit" name="TesterButton" value="TesterButton" />
</form>
Or if you really insist in using JS for this for some reason:
<button name="TesterButton" value="TesterButton" onclick="location.href='TesterServlet?TesterButton=TesterButton';">
Note that the use of onClick instead of onclick indicates that you're possibly reading outdated or poor HTML resources/examples.
See also:
HTMLdog - HTML beginner tutorial
W3 HTML forms specification

The button is not send in the request even if is inside a form.
UPDATE:
If you want to send the value of the clicked button (kinda strange) you need to use a hidden element and set the value before call the servlet:
<form id="myForm" method="post">
<input type="hidden" id="hidButtonValue" />
<button id="TesterButton" value="TesterButton" onclick="document.getElementById('hidButtonValue').value=this.value; location.href='TesterServlet';"
</form>
Now you can call the hidden in your servlet
writer.println( (String)request.getParameter("hidButtonValue") );
Also, it would be better to encapsulate the onclick code in a JavaScript function.

A quick and easy way would be using a form with a hidden field:
<form action="TesterServlet">
<input type="hidden" name="testField" id="test" value="SomeValue" />
<input type="submit" value="Submit" />
</form>
Then in your servlet:
writer.println( (String)request.getParameter("testField") ); // Should return "SomeValue"

Related

How to combine AngularJS and Thymeleaf templates

I am converting a web page currently using Thymeleaf to instead use AngularJS.
I want to convert the follow block containing Thymeleaf
<form th:action="#{?deleteLog}" th:object="${log}" method="post" class="in-line">
<input type="hidden" th:value="${log.vid_id}" name="vid_id" />
<input type="hidden" th:value="${log.id}" name="id" />
<button type="submit" class="btn btn-danger">Delete</button>
</form>
These th:value variables are being passed to a #PostMapping method in my controller class that then uses those variables to delete an entry from my database.
How can I achieve this using AngularJS? Taking the same approach I have
<form th:action="#{?deleteLog}" method="post" class="in-line">
<input type="hidden" th:value="{{x.vid_id}}" name="vid_id" />
<input type="hidden" th:value="{{x.id}}" name="id" />
<input type="button" value="Remove" class="btn btn-danger" ng-click="removeRow(tableList.id)" />
</form>
The removeRow() function successfully removes the row from the table in the UI, I just need to send the {{x.vid_id}} and {{x.id}} values to the method in my controller class.
I am obviously not using correct syntax because I am getting parsing errors for the th:value="{{x.id}}" thymeleaf variables when I try to use Angular. I've also tried th:value="${{x.id}}"' and th:value="${{{x.id}}}".
I haven't been able to find anything online regarding passing an Angular variable. Is this even possible?
You shouldn't mix AngularJS (SPA framework for client-side HTML templating) with Thymeleaf (library for server side HTML templating). You should choose to do templating on server or client and stick with it.
Combining these two doesn't make sense at all.

assigning input tag value to a java variable not working

I know it is a very classic question, but I don't know why this doesnt work for me!!!
<input id="code" value="45" type="hidden" name="code" >
<%=request.getParameter("code") %>
the java tag is supposed to show me 45, but it shows null
Jsp code will execute in your container and at that time the code value will be null.
And the below code prints non null when the incoming request(get/post) contains code parameter in it
<%=request.getParameter("code") %>
So to make it print 45 append this your.jsp?code=45 at end of url then it will print 45 in the page
You need html form tag to send the parameters.
Ex:
<form>
<input id="code" value="45" type="hidden" name="code">
<%=request.getParameter("code") %>
<button type="submit">Send</button>
</form>

How to get form name value in JSP

Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.

Struts2: values from action are not seen in jsp, but they are seen in the javascript code from jsp

In action I have a variable which has getter on it.
private String myActionVariable = "predefined....";
public String getMyActionVariable () {
return myActionVariable;
}
In jsp, I try to use my variable in this way:
<input type="button" class="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="${myActionVariable}"
But it is not shown. However, if I output this variable from the javascript code included within the same jsp file:
alert (${myActionVariable})
I will get the value of it....
Any idea please ? ...
You can use a Standard <input/> HTML Tag with an <s:property /> Struts2 Tag for the value, like this:
<input type="button" class="styledButton"
onclick="javascript:doAjax('myActionName',false);"
value="<s:property value="%{myActionVariable}"/>"/>
or a Struts2 Tag directly like this:
<s:submit type="button" cssClass="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="%{myActionVariable}" />
Note that with Struts2 Tag, class attribute becomes cssClass (and style becomes cssStyle), and that %{} is the right OGNL syntax, instead of ${} (that is JSTL syntax)
EDIT: when using Struts2, forget about JSTL, you won't need them anymore.
You should be using struts2 tag.
<input type="button" class="styledButton" onclick="javascript: doAjax('myActionName',false);" value="${myActionVariable}">
Instead of this, use
<s:submit type="button" cssClass="styledButton" onClick="javascript: doAjax('myActionName',false);" value= "myActionVariable" />

Possibility to get all form inputs from several layers of panels and forms

Kinda new to wicket and im wondering if its possible to use a AjaxButton in order to get form values from forms within the first form...
For example, using the "ajaxButton" below would it be possible to also get the value of "anothervalue" even though its not in the same form?
<form>
<input type="submit" wicket:id="ajaxButton" />
<input type="text" wicket:id="aValue" />
<panel>
<form>
<input type="text" wicket:id="anothervalue" />
</form>
</panel>
</form>
Wicket supports nested forms. When you submit the outer form, the inner one should also processed (validate params, update models).

Categories