Moving to different servlet other than form action - java

I am having a form :
<form action="sendaddnotification" method="post">
<%String namee=rs.getString(2);%>
<input name="IndUserName" type="hidden" value="<%=namee%>"/>
User Name : <%=namee%>
<br>
First Name : <%=rs.getString(4)%>
<br>
Last Name : <%=rs.getString(5)%>
<br>
Email Id : <%=rs.getString(6)%>
<br>
Contact : <%=rs.getString(7)%>
<br>
<%
String groupidd = request.getSession().getAttribute("groupid").toString();
s=null;
rs=null;
int flag=0;
String sql="select * from TBGROUPUSERS where I_ID=? and GU_GROUPID=?";
s = con.prepareStatement(sql);
s.setString(1,idperson);
s.setString(2,groupidd);
rs=s.executeQuery();
if(rs.next())
flag=1;
request.setAttribute("flag", flag);
%>
<c:choose>
<c:when test="${requestScope.flag == 1}">
<!-- flag is 1 -->
<input type="submit" value="REQUEST SENT" disabled="disabled"></input>
<a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
<input type="submit" value="CANCEL REQUEST"></input>
</a>
</c:when>
<c:otherwise>
<!-- flag isn't 1 -->
<input type="submit" value="ADD"></input>
</c:otherwise>
</c:choose>
<input type="button" value="BACK"></input>
</form>
Now ,In this part of code :
<a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
<input type="submit" value="CANCEL REQUEST"></input>
</a>
I want to move to servlet CancelRequest.java with this given parameters.But as the form action goes to sendaddnotification,So this servlet never runs.
How to make it run on click of this button.Please help

Assuming your CancelRequest servlet is defined in web.xml as:
<servlet>
<servlet-name>cancelRequest</servlet-name>
<servlet-path>packageName.CancelRequest</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>cancelRequest</servlet-name>
<url-pattern>/cancelRequest</url-pattern>
</servlet-mapping>
Write javascript function as:
function cancelRequest(){
var exit = confirm('Are you sure you want to cancel the request?');
if(exit == true){
document.getElementById('sendaddnotificationId').action = 'cancelRequest';
}else{
return;
}
}
Include id for form in jsp as:
<form action="sendaddnotification" id="sendaddnotificationId" method="post">
And change anchor element as:
<a href="cancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="cancelRequest();">
<input type="submit" value="CANCEL REQUEST"></input>
</a>

Related

How can I save all the data from different textboxs in an array, using JSTL?

I want to recieve any number of textbox values, and save it to an array with JSTL if it's possible.
I generate all the texbox where numberAsked can be any number.
<c:if test="${param.buttonSend != null}">
<form action="index.jsp" method="post">
<c:forEach var = "i" begin = "1" end = "${param.numberAsked}">
<input type="text" name="textbox[]" class="form-control">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
</c:if>
Now I want to save all the textboxs in an array.
<c:if test="${param.buttonSave != null}">
<c:set var="data" value="${request.getParameterValues(textbox)}"/>
<c:forEach var = "item" items="${param.data}">
<c:out value="${item}"/>
</c:forEach>
</c:if>
But, it doesn't work, how can I save all the data from all the generated textboxs in an array?.
Here is a demonstration JSP.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<form method="post">
<c:forEach var = "i" begin = "0" end = "5">
<input type="text" name="textbox">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
<c:set var="data" value="${paramValues.textbox}"/>
<c:forEach var = "item" items="${data}">
${item}
</c:forEach>

Sending data using <a href="#"> in the same page and display the result (FORM) using JSP without changing the (FORM) coding

<div id="main_search">
<table>
<tr>
<td>Product</td>
<td>:</td>
<td>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral-VC">Bilateral-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral Non-VC">Bilateral Non-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget Domestic">Budget Domestic
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget High">Budget High
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget International">Budget International
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="IDD Buffet Base">IDD Buffet Base
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Premium">Premium
<br>
</td>
</tr>
<tr>
<td>Destination</td>
<td>:</td>
<td>
<input id="destination" type="text" name="destination" />
</td>
</tr>
<tr>
<td colspan="9" align="center">
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="#">SEARCH</a>
</td>
</tr>
</table>
</div>
<div id="search_result" style="overflow: auto;">
<h2>Floor Price Edit</h2>
<table id="result-tbl" class="searchTable">
<tr>
<th>Destination</th>
<th>Product</th>
</tr>
<tr>
<% String dest=r equest.getParameter( "destination"); %>
<td>
<%=d est %>
</td>
</tr>
<% if (dest !=n ull) { %>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3307/icars" user="root" password="" />
<sql:query var="dest_result" dataSource="${dbsource}">
SELECT * from icx_pricing_desk WHERE DESTINATION LIKE '
<%=d est %>%'
</sql:query>
<c:forEach var="row" items="${dest_result.rows}">
<tr>
<td>
<c:out value="${row.DESTINATION}" />
</td>
</tr>
</c:forEach>
<% } %>
<td>
<% String products[]=r equest.getParameterValues( "product"); %>
<% if(products !=n ull) { %>
<% for(int i=0; i<products.length; i++) { %>
<%=products[i]%>
<sql:query var="prod_result" dataSource="${db }">SELECT * FROM icx_pricing_desk WHERE PRODUCT = '
<%=products[i]%>'</sql:query>
<c:forEach var="row" items="${prod_result.rows}">
<tr>
<td>
<c:out value="${row.PRODUCT}" />
</td>
</tr>
</c:forEach>
<% } } %>
</td>
</table>
</div>
This is some kind of form without <form> tag and for submit button it didn't use <input type="submit"> like normal form. It use <a href="#"> to submit the data.
The question is how do you send the data using the <a href="#">???
In addition, when i click the SEARCH button both <% String dest = request.getParameter("destination"); %> and <% String products[]= request.getParameterValues("product"); %> didn't received any data.
When using the standard form, both can display result. This code is written in same page(JSP).
--The first coding cannot be changed because it was the assignment and need to solve it.--
Add a form tag in your code like
<form name="form1" method="POST">
<table>
<!-- Your code ->
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="javascript:void(0);" onclick="document.form1.submit();">SEARCH</a>
</table>
</form>
Things to remember
Form tag: method(POST), Name of form(form1)
Method called on onclick of a tag

Link to the correct item page by selecting the list of items in for loop

This is my jsp page that retrieve the list of items from database using for loop
<%
itemManager mgr = new itemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
</td>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemID()%>">
<br/>
<%=items[i].getItemDesc()%>
<br/>
Start Bid : <%=items[i].getStartBid()%>
<br/>
Buy it now : <%=items[i].getEndBid()%>
<br/>
Bidding close on : <%=items[i].getDuration()%>
<br/>
<input type="submit" value="View">
<%
}
%></table>
This is the jsp page that link to the item you selected previously
<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
<td> <b> <%=items.getItemName() %></b> </td>
</tr>
</table>
This is the servlet to store the session of the selected item id and forward to the correct item jsp page.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
int id = Integer.parseInt(request.getParameter("itemID"));
session.setAttribute("ITEM_DATA",id);
RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
rd.forward(request, response);
}
However, after I clicked the view button. It keeps linking to the itemID = 1.
The URL dispalys "/ItemServlet?itemID=1&itemID=2" .
In fact, if I click on itemID=2 the URL should display like this:
"/ItemServlet?itemID=2"
As a result, how can I achieve this? Thanks in advance!
The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code
<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
<%
}
%></table>
</form>
When you run this code and if you check the rendered HTML code it will be look
<form action="item" method="get">
<table>
<tr><td>
aaa
<input type="text" name="itemID" value="1">
<input type="submit" value="View"> </td></tr>
<tr>
<td>
bbb
<input type="text" name="itemID" value="2">
<input type="submit" value="View"> </td></tr>
<tr><td>
ccc
<input type="text" name="itemID" value="3">
<input type="submit" value="View"> </td></tr>
</table>
</form>
Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.
Create form inside your for loop like below code.
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
</form>
<%
}
%></table>
Hope This will help you.
change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value. by using EL.I hope this will help you

Form Not Submitting after Jquery Validation

$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.

Cant send the right key to the servlet

I can`t seem to send the right entity key to servlet in my web app. I am using javascript method to submit the form with the data via a button.
The code is divide into jstl code:
<c:if test="${!empty MOFornecedorList}">
<div id="RightColumn">
<%-- Search Box --%>
<div class="searchform">
<form id="formsearch" name="formsearch" method="post" action="<c:url value='FProcurar'/>">
<span>
<input name="searchBox" class="editbox_search" id="editbox_search" size="80" maxlength="100" value="Pesquisa" type="text" />
</span>
<input name="btnsearch" class="button_search" value="Pesquisa" type="button"/>
</form>
<div class="clr"></div>
<h>Criterio de Pesquisa: </h>
<select name="Type">
<option value="1">ID</option>
<option value="2">Nome</option>
<option value="3">Email</option>
<option value="4">Fax</option>
<option value="5">Endereço</option>
</select>
</div>
<%-- END Search Box --%>
<div class="clr"></div>
<table id="ProductTable" class="detailsTable">
<tr class="header">
<th colspan="9" >Modificar Fornecedor</th>
</tr>
<tr class="tableHeading">
<td>ID</td>
<td>Nome</td>
<td>Endereço</td>
<td>Nº de Celular</td>
<td>Nº de Telefone</td>
<td>Email</td>
<td>Fax</td>
<td>Descrição</td>
<td></td>
</tr>
<c:forEach var="MOForn" items="${MOFornecedorList}" varStatus="iter">
<tr class="${'white'} tableRow">
<td>${MOForn.getFid()}</td>
<td>${MOForn.getFNome()}</td>
<td>${MOForn.getFEndereco()}</td>
<td>${MOForn.getFNCel()}</td>
<td>${MOForn.getFNTel()}</td>
<td>${MOForn.getFEmail()}</td>
<td>${MOForn.getFFax()}</td>
<td>${MOForn.getFDescricao()}</td>
<td>
<form action="<c:url value='FMOb'/>" method="post" name="FModifi">
<input type="hidden"
name="MOForn"
value="${MOForn.fid}">
<input type="button"
value="Modificar" onclick="ModF()">
</form>
</td>
</tr>
</c:forEach>
</table>
</div>
</c:if>
the javascript method
function ModF() {
jConfirm('Modificar o Fornecedor?', 'Confirmação', function(r) {
if (r == true) {
$("form[name='FModifi']").submit();
} else {
return false;
}
});
}
and the controller code:
//Check if fornecedor as been selected
int Fid = Integer.parseInt(request.getParameter("MOForn"));
//Get fornecedor object and set it to variable
Forn = transManager.getEnt(Fid,"fornecedor");
request.setAttribute("Forn",Forn);
PagesInF="FModificar";
request.setAttribute("PagesInF", PagesInF);
userPath = "/Fornecedor";
Now when i test the code the jstl will read 5 records in the item MOFornecedorList in ascending order and a button will be created in the last column.
When the button is pressed for example in the third record the JavaScript method Modf() is invoked and a confirm dialog is shown.
When the user presses the OK button the form FModifi is submitted.
Then the servlet will receive a the request to open the page FMOb where the hidden input for the button pressed will be retrieved and put in a variable type int and some other code will execute.
But the value that the form submit's is the wrong one. ex:
1 - button - MOforn = 1
2 - button - MOforn = 2
3 - button - MOforn = 3 (clicked)
4 - button - MOforn = 4
5 - button - MOforn = 5
The Form should send the value of 3 but sends the value of 5.
So please if anyone as any ideas please share.
You've multiple forms with the same name. Your JS function isn't submitting the form from which it was been called, but it is submitting the last occurrence of the form with that name in the HTML DOM tree.
You need to replace
<input type="button" value="Modificar" onclick="ModF()">
by
<input type="button" value="Modificar" onclick="confirmSubmit(this.form)">
and rewrite the function as follows:
function confirmSubmit(form) {
jConfirm('Modificar o Fornecedor?', 'Confirmação', function(confirmed) {
if (confirmed) {
form.submit();
}
}
}
I'd also suggest to use more self-documenting variable and function names like presented above, so that your code is easier understandable and maintainable in long term (not only for yourself, but also for others, for example the ones on Stackoverflow.com from who you expect an answer when you post a question...)

Categories

Resources