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
Related
I can't find one attribute sent from a JSP form.
<form name="shuntingForm" action="/TCS_FUND/ShuntAction.aen" method="POST">
<div class="field-item clearfix">
<input type="hidden" name="csrfPreventionSalt" value="<%=CsrfPreventionSalt%>" />
<input type="hidden" name="idSession" value="<%=idSession%>">
Funds Trading<br>
</div>
</form>
request.getAttribute("idSession") & request.getParameter("idSession") show NULL.
Any ideas ? thanks
I would simplify this to return a fixed value.
Change:
<input type="hidden" name="idSession" value="<%=idSession%>">
To:
<input type="hidden" name="idSession" value="123456789">
Once that works, figure out the difference.
You also may want to view the source of the web page. If your idSession does not have the value you expect, your input tag will show the wrong value field.
I want to insert an Attribute into the html Code.
I tried this, but it's not working:
<div id="${var}"> ... </div>
I think you know what I mean. The attribute 'var' should be the id. I didn't find a solution...
You just need to use the th:attr attribute. It is explained in the reference documentation 5.1:
5.1 Setting the value of any attribute
Enter then the th:attr attribute, and its ability to change the value
of attributes of the tags it is set in:
<form action="subscribe.html" th:attr="action=#{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
The
concept is quite straightforward: th:attr simply takes an expression
that assigns a value to an attribute. Having created the corresponding
controller and messages files, the result of processing this file will
be:
<form action="/gtvg/subscribe">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="¡Suscríbe!"/>
</fieldset>
</form>
Use this
<div th:attr="id=${var}"> ... </div>
Thymeleaf only evaluates attributes that are prefixed with th:. Here is a list of the attributes that are evaluated:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes
In your case, th:id is already built in so you can simply do <div th:id="${var}"> ... </div> and it will work. th:attr, is used to define attributes that thymeleaf doesn't normally support.
My index.jsp is as following:
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="${fname}"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
with value=${fname} is get from dopost method in servlet when form summited
request.setAttribute("fname", fileName);
getServletContext().getRequestDispatcher("/index.jsp").forward(
request, response);
But it's weird that when I deployed index.jsp
My text field always show ${fname} in text area, even after form submitted, its still get that value (correctly it must show 'filename')
Does anyone meet this problem like me?
First thing is that, you are using wrong syntax to display value in JSP.
Below syntax is wrong.
value="${fname}"
We use expression tag fordisplaying value in JSP page. Expression tag convert into Java statement.
<%=request.getAttribute("fname")%>
When you first time open your index.jsp page, it will show you blank value and when request will come back from server side, then it will show you correct result.
Use below code.
<%if(request.getAttribute("fname")!=null){%>
<input type="text" value ="<%=request.getAttribute("fname") %>"/>
<%}else
{ %>
<input type="text"/>
<%} %>
In JSP, the correct syntax to retrieve the values is
<%=request.getAttribute("fname")%>
This is known as assignment OR expression tag (<%=%>).
Try this ...
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="<%=request.getAttribute("fname")%>"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
More related to this, here.
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
I have a question on how to update a particular record in a database using JSTL and java servlets.
Ok to call one record, I use a servlet to run a query to populate a jsp page using this type of format.
ResultSet results = getRecords.executeQuery(); //run database query
Result result = ResultSupport.toResult(results);
request.setAttribute("result", result);
RequestDispatcher rd = request.getRequestDispatcher("/showReport.jsp"); //redirect to this page with query info
rd.forward(request, response);
In the jsp page, I use this format to populate the page.
the row number is the corresponding to the index of the value on how the query is run.
<c:forEach var="row" items="${result.rowsByIndex}">
<form action = "/runthis/servlet?id=${row[0]}" method = "get" >
Employee Name<input type="text" value="<c:out value="${row[2]}"/>" />
Department<select>
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option></select>
<input type="submit" value="Save">
</form>
</c:forEach>
So for instance, if someone changes the Employee Name, but leaves the Department field alone, or vice versa, how would I go about this?
I think I should be able to kick this form action to a "update servlet", but I would rather not "update" every element in the record if I do not need to.
Also I'm still learning web dev, so if you can think of a better way to prepopulate stuff, via javascript or whatever, I'm willing to try that as well, if it's a possible solution.
Thanks for any help!
i think you need to do something like this:
<c:forEach var="row" items="${result.rowsByIndex}">
<form action="/runthis/servlet" method="post">
<input type="hidden" name="id" value="<c:out value="${row[1]}"/>" />
<input type="hidden" name="EmployeeName_orig" value="<c:out value="${row[2]}"/>" />
Employee Name<input type="text" name="EmployeeName" value="<c:out value="${row[2]}"/>" />
<input type="hidden" name="Department_orig" value="<c:out value="${row[4]}"/>" />
Department
<select id="Department" name="Department">
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option>
</select>
<input type="submit" value="Save" />
</form>
</c:forEach>
and then, in your update servlet (here called /runthis/servlet) you do some form of update employee and set the name and department where the row is id. note that i send the id in a hidden field, but it could just as well be sent in the url as you mentioned in our discussion above!
about your remark on updating every element in a record:
this can be done in different ways. one way is to load the record from database first, and compare the fields to know what fields to update. but that would require an extra read from the database before the update.
another way (the one i have prepared for in my proposed solution) is to have hidden fields, one for each input in your form. this hidden field holds the original value for its input counterpart. this way you can compare the original value with the new value without having to do a read from the database. on the other hand, you're now transferring the double amount of data over the line for each post request.