input value text field in jsp error - java

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.

Related

JSP FORM POST : empty attributes

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.

jsp form,servlet doesn't get parameters

I have form in jsp
<table border="1">
<c:forEach items="${sessionScope.proizvodList}" var="proiz">
<tr>
<td>${proiz.sifra}</td>
<td> ${proiz.naziv}</td>
<td>${proiz.boja}</td>
<td>${proiz.dimenzije}</td>
<td>${proiz.tezina}</td>
<td><form action="KupiServlet" method="get">
<input type="text" name="kolicina"/>
<input type="hidden" name="id" value="${proiz.sifra}"/>
<input type="submit" value="Kupi"/>
</form>
</td>
</tr>
and in servlet in doGet method,i have
String id=request.getParameter("id");
String kol=request.getParameter("kolicina");
When I run this,i got error that both id and kol are null,so i guess servlet doesn't get these parameters..but i think that my form looks good..Does someone have idea why this happens?Thanks in advance!
Check this link - you cannot have a form inside a form.
Is it valid to have a html form inside another html form?

How to access the html form data in java program

i have created a java project in eclipse,in this project i have an html form customer.html as shown below
<form>
Name :<input type="text" id="name"/>
Address: <input type="text" id="add"/>
Age:<input type="int" id="age"/>
<input type="submit" onclick="what should i call here in my program"/>
</form>
once the user fills the form and submits it i want to access the form field in java code is it possible.if so how or else is there anyother solution to create an xml file for this form
<input type="submit" onclick="only Javascript function can be called here"/>
if you want to access html form elements at java code you need to submit your form at some controller or another jsp. Like..
<form name="new_employee" method="post" action="../admin/newEmployee.do">
Name :<input type="text" name="employeeName"/>
Address: <input type="text" name="add"/>
Age:<input type="text" name="age"/>
<input type="submit" value="Submit"/>
</form>
form elements can be accessed from generated request by their names like..
request.getParameter("employeeName");

How do I submit the right value?

<c:forEach items="${movieList}" var="movie" varStatus="status">
<tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
<td>${movie.title}</td>
<td>${movie.genre}</td>
<td>${movie.year}</td>
<td>${movie.boxoffice}</td>
<td>
<form:form action=edit.htm>
<input type="hidden" name="edit" value="movie name">
<input type="submit" value="Edit">
</form:form>
<form:form action=delete.htm>
<input type="hidden" name="delete" value="movie name">
<input type="submit" value="Edit">
</form:form>
</tr>
</c:forEach>
This is the section of code I have at the moment. The idea is to display movie data, but also provide buttons to send the user to either a filled form page to edit the data or simply delete the respective data and redirect to the same page. I am just unsure as how to pass along the movie object. I know it is simple, but I can't find the reference I used previously...
Thanks.
U can try this :
put this object in a hidden input
<input type="hidden" id="hidden_object" value="" name="hidden_object"></input>
than you can access the value with the method getParameter()
OR
put this object in your session
in your jsp
<% session.setAttribute("movie", ${movie}); %>
good luck

navigating through tab key not working properly

in my jsp, when i navigate through tab key it skips my Save and Reset button. and move to next component in my page. even though i have not used tabindex in my jsp files. please suggest what could be the reason. thanks
code is like:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<span class="save_button_common" onClick="submitForm('save')">Save</span>
<span class="reset_button_common" onClick="submitForm('reset')">Reset</span>
</div>
May be because it is not input type , you can explicitly try setting tabIndex
Try:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<input type="button" class="save_button_common" onClick="submitForm('save')">Save</input>
<input type="button" class="reset_button_common" onClick="submitForm('reset')">Reset></input>
</div>
Note if it's a form you need input type="button" rather than just using <button> which you can otherwise just for straight links that don't submit a form. This is particularly relevant with Internet Explorer that treats the button tag somewhat differently to other browsers (submitting the "value=" rather than the enclosed text or something like that)

Categories