Passing Parameters in JSP to Servlet - java

I have following code
JSP
<tbody>
<c:forEach var="defect" items="${defects}">
<tr>
<td>${defect.name}</td>
<td>${defect.description}</td>
<td>${defect.summary}</td>
<td>${defect.priority}</td>
<td>${defect.originator.name}</td>
<td>${defect.assignee.name}</td>
<td>
<form action="AllOpenDefects?defectId=${defect.id}" method="get">
<input type="submit" value="Update" />
</form>
</td>
</tr>
</c:forEach>
</tbody>
Servlet (in doGet method)
System.out.println((String) request.getParameter("defectId")); // It is printing null
and also in url the defectId is NOT been appended ... Is there any issue with my code?
EDIT: The url is http://localhost:8080/BugManagemetSystem/AllOpenDefects but it should be like http://localhost:8080/BugManagemetSystem/AllOpenDefects?defectId=2

It looks like your browser clears parameters after ?... in action="..." attribute. In that case try passing it via <input type="hidden" .../> like
<form action="AllOpenDefects" method="get">
<input type="hidden" name="defectId" value="${defect.id}"/>
<input type="submit" value="Update" />
</form>
This way form should add them to URL as ?defectId=value of ${defect.id}.

IMHO the short answer to your question is "your code is ok".
As a side note, if it doesn't yield the new required link, it's often an update problem. E.g. try changing the URL algtogether just for testing (form action="Junk"), see if the browser catches it.
If not, try clearing various caches - browser cache, deleting the exploded war and work directories (e.g. in Tomcat it's /webapps/myapp/ and /work)...

Related

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?

input value text field in jsp error

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.

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

form values not submitted to the servlet

I am trying to submit the text field value and print it using the servlet. The index.jsp is my main page and I am using jsp:include to include the form which reside in another page which is login.html.
here is the code i have for login.html
<form id="f1" action="ControllerServlet" method="GET">
<p>username
<input class ="text-input" type="text" id="txtusername" />
</p>
<p>
<input type="submit" value="submit" />
</p>
the index.jsp
<div id="col3_content" class="clearfix">
<h1>H1 Heading</h1>
<jsp:include page="login.html"></jsp:include>
</div>
the controller servlet
String usrname = request.getParameter("txtusername").toString();
out.print(usrname);
The problem is this is throwing a null pointer exception. what am I doing wrong here ? any help appreciated. thanks
Please use name not id
<input class ="text-input" type="text" name="txtusername" />
The id is not used to identify the name of the input parameter. The right attribute for the parameter is name, currently you are using an input without a name. So use
<input class ="text-input" type="text" name="txtusername" id="txtusername" />
You need to define name attribute of input tag to get it in Servlet by name.
<input class ="text-input" type="text" id="txtusername" name="txtusername" />
Also make sure you are writing code in doGet or service method of servlet as you have GET as action in form tag.
Code for Login.html
<form action="ControllerServlet" method="GET">
<p>username :
<input type ="text" name="txtusername" /></p>
<p><input type="submit" value="submit" /> </p>
</form>
ControllerServlet.java
public void service(ServletRequest request, ServletResponse response)
{
String username = request.getParameter("txtusername");
PrintWriter out = response.getWriter();
out.println("User Name " + username)
I faced a similar situation, when I checked front end, the form seems to have all the value populated correctly. However, after form.submit, from server side using request.getParameter("the parameter") does not return the value populated. After tuning on the network traffic tab in browser, I see the parameter was there, but there was a typo.
Hopefully could save you some time if same thing happens to you.

Sending form parameters from JSP to Struts action class

I am implementing a search functionality in a website I am building, which involves searching by the md5 hash of the name of the file submitted and searching by the notes associated with each submitted file. So, I should detect as to which button is pressed "Search by MD5" or "Search by Notes". This is the code I have:
JSP code for the form:
<form id="search" name="search" action = "search.do"
method="POST" enctype="multipart/form-data">
<table align = "center">
<tr>
<th colspan="4" bgcolor="#004276"><font color="white">
Search for Sample
</th>
</tr>
<tr>
<td><input name="md5" type="text" value="${form.md5}"/></td>
<td><input name="md5search" type="submit" value="Search by MD5"/>
</tr>
<tr>
<td><input name="notes" type="text" value="${form.notes}"/></td>
<td><input name="notessearch" type="submit" value="Search by Notes"/>
</tr>
</table>
</form>
search.do is mapped to SearchResultsAction.java. Code in Java action class (SearchResultsAction) which handles the request is:
if(request.getParameter("md5search").toString().equals("Search by MD5")){
searchSubmissionsList = submissionsDAO.searchSubmissionsByMD5(form.getMD5());
}
if(request.getParameter("notessearch").toString().equals("Search by Notes")){
searchSubmissionsList = submissionsDAO.searchSubmissionByNotes(form.getNotes());
}
But the problem I am facing here is that, request.getParameter("md5search") and request.getParameter("notessearch") return null for some reason. I have been working on this for a while and have not been able to figure it out. The weird thing is that it once worked for me sometime back when I was working on another project. Am I missing something here?
It's null because you used multipart/form-data form encoding instead of (default) application/x-www-form-urlencoded. Basically, you have to (let Struts) extract the text fields from the multipart form data body the same way as you (or Struts) extracted the uploaded file. Or, as there is actually no <input type="file"> field in your form at all, just remove the enctype attribute altogether.
See also
Does form with enctype="multipart/form-data" cause problems accessing a hidden field

Categories