Pass Two different values on a form to a servlet? - java

Given the following HTML:
<form action="" name="<%=rs.getString(1)%>" method="post">
<select name="opcao">
<option value="Did not like">Did not like</option>
<option value="Ok">Ok</option>
<option value="Liked" selected="selected">Liked</option>
<option value="Loved!">Loved!</option>
</select>
</form>
Is it possible to get the name of the form and from the select tag to the same servlet as different parameters?

HTML form name is NOT submitted as part of the request. Although if you want, you can pass as form hidden field as below:
<form name="myForm" action="/my_servlet">
<input type="hidden" name="htmlFormName" value="myForm"/>
....
However if you have two form fields, then they are passed to servlet and can be accessed using request.getParameter("fieldName") method inside the servlet..

Related

Transform WSDLtoJava in Spring boot and JSP

I have a JSP page and a Spring boot controller, in the jsp i have a form with a select and options when i choose the option i need to pass that value to a java code to transform WSDL to java classes. How can i do it?
<form id="form" action="genclasses" method="post">
<select name="WSDLcat" class="custom-select ml-2" id="inputGroupSelect04">
<%
ServiceClass[] services = (ServiceClass[]) request.getAttribute("services");
%>
<c:forEach items="${services}" var="service">
<option id="WSDL" value="${service.getWsdl()}">
${service.getNombre()}</option>
</c:forEach>
</select>
<input class="btn btn-outline-secondary align-right" id="genclases" type="submit" onclick="generarClases()" value="Generar Clases">
</form>
String[] params= {"-d","./src/main/resources/generatedFiles","HERE I NEED THE VALUE OF THE OPTION"};
WSDLToJava.main(params);
ZipUtil.pack(new File("./src/main/resources/generatedFiles"), new File("./src/main/resources/generated.zip"));
I cannot do it in java inserted in jsp becasuse i need to use different libraries and i do not know how to pass it to the controller or do it in the same page. Any help?

How to show some selected value of dropdownlist

Thymeleaf View
<option th:each="technology:${technology}"
th:value="${technology}"
th:selected="${technology==project.getTechnology()}"
th:text="${technology}"/>
Java Controller
model.addAttribute("technology",organization.getTechnology());
assuming that you are binding your domain as myobject in your controller which also have technology as an attribute. Then add th:field="*{technology}" your code will look like below
in controller
model.addAttribute("technology",organization.getTechnology());
model.addAttribute("myobject",new MyClass());
<form th:object="${myobject}">
<select class="selectpicker"
id="tech"
th:field="*{technology}">
<option th:each="technology:${technology}"
th:value="${technology}" th:text="${technology}">
</option></select>
<form>

html load conditional value to select tag

I try to load conditional value to a select tag based on the user's choice on a pervious form tag. it might require java or php. i am not competent in any of those two language.
the logic is explain as the following.
<form>
<input name="complaint" value="copyright" type="radio">copyright <br>
<input name="complaint" value="trademark" type="radio">trademark <br>
<input name="complaint" value="other_concern" type="radio"> other concerns
</form>
if complaint="copyright" then
<select>
<option value="image">image</option>
<option value="product">product</option>
</select>
elseif complaint="trademark" then
<select>
<option value="item">item</option>
<option value="image">image</option>
</select>
elseif complaint="other_concern"
<select>
<option value="explain">explain</option>
</select>
end if
any solution?
Heres a solution in Jquery. Add a class name to the inputs and hide the selct options by default
<input name="complaint" value="copyright" type="radio" class="complaint">
then add unique ID tags to the select options along with a css class i like to use, .addClass and .removeClass are slighty faster and more efficent than .hide() and .show() selectors
Add the css at the bottom of your css sheet
.hidden{position:absolute;width:0;height:0;overflow:hidden;visibility:0;padding:0;margin:0}
<select id="copyright" class="selectOptions hidden">
<option value="image">image</option>
<option value="product">product</option>
</select>
Finally add some JQuery You need to copy the remainder for your other hidden values
$('.complaint').click(function() {
$('.selectOptions').addClass('hidden');
var checkComplaint = $(this).val();
$('#' + checkComplaint).removeClass('hidden');
});

HTML Select box returning null in Servlet

I have an HTML form that looks like this:
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" size="50" />
<input type="submit" value="Upload File" />
<select name="options">
<option value="public">Public File</option>
<option value="locker">Locker File</option>
</select>
</form>
I want the user to select a file and then select an option from the dropdown to choose where the file is saved to.
I am attempting to retrieve the value of the option using
String option = request.getParameter("options");
However, for some reason, the option is null, despite choosing an option.
Anyone know why this is?
I found the solution after tearing through results on StackOverflow/Google.
The issue comes into play with the "multipart/form-data" enctype attribute on the form element. Whenever you call .getParameter with that enctype, it will return null.
For a solution, check out this question/answer

Append to JSP view using Java

I have a form in my JSP which has a list box.
I want on submit of the form, I do some server side validation on and then duplicate the list box and append it to jsp again?
Can someone help me know how we can achieve this ?
Jsp :
<form:form commandName="user">
<div id="primaryList">
<select name="data" size="5" id="s">
<option value=""></option>
</select>
</div>
</form:form>
I know we can do it using javascript, but I need validation done on server side before duplicating.

Categories