Cant send the right key to the servlet - java

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...)

Related

HTMLunit Help Script for checking available classes

I am a bit out of sync with web programming and getting into HTMLUnit is a bit more confusing than I thought it would be.
Essentially I missed registering a class and need to be notified the moment a space opens but prior to getting to that page dump I need to submit a form with two radio inputs ( with the options "Spring Semester 2019" and the "All Classes").
I am in a weird spot where I want to learn more but also need a working script, so a combination of an answer plus some resources I might not be utilizing would be awesome! For example when I do get to the next page how do I download the html file raw and access the required data like number of spots filled and available in xyz class.
https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject
Here is the monkey little program I wrote to get my feet a bit wet:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
public class hateMerced {
public void submittingForm() throws Exception {
}
public static void main(final String[] args) throws IOException {
final WebClient webClient = new WebClient();
// Get the first page
HtmlPage page1 = webClient.getPage("https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("xhwschedule.P_ViewSchedule");
HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) page1.getElementById("201910");
radioButton.setChecked(true);
HtmlRadioButtonInput radioButton2 = (HtmlRadioButtonInput) page1.getElementById("N");
radioButton2.setChecked(true);
final HtmlSubmitInput button = form.getInputByName("View Class Schedule");
// Now submit the form by clicking the button and get back the second page.
// final HtmlPage page2 = button.click();
webClient.close();
}
}
And Here is the lovely error I get:
Jan 16, 2019 1:09:57 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter error
SEVERE: error: message=[illegally formed XML syntax] sourceName=[script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)] line=[38] lineSource=[// End script hiding -->] lineOffset=[24]
Exception in thread "main" ======= EXCEPTION START ========
Exception class=[net.sourceforge.htmlunit.corejs.javascript.EvaluatorException]
com.gargoylesoftware.htmlunit.ScriptException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:892)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:616)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:723)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:689)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:735)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:316)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:396)
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:246)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:267)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:802)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:758)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1194)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1134)
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:221)
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:314)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3179)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2132)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:939)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:452)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:403)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:1001)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:250)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:196)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:158)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:531)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:398)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
at FuckMerced.main(FuckMerced.java:34)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.error(StrictErrorReporter.java:65)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:260)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:232)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:228)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getNextXMLToken(TokenStream.java:1287)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getFirstXMLToken(TokenStream.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.xmlInitializer(Parser.java:2666)
at net.sourceforge.htmlunit.corejs.javascript.Parser.unaryExpr(Parser.java:2641)
at net.sourceforge.htmlunit.corejs.javascript.Parser.mulExpr(Parser.java:2568)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addExpr(Parser.java:2552)
at net.sourceforge.htmlunit.corejs.javascript.Parser.shiftExpr(Parser.java:2533)
at net.sourceforge.htmlunit.corejs.javascript.Parser.relExpr(Parser.java:2508)
at net.sourceforge.htmlunit.corejs.javascript.Parser.eqExpr(Parser.java:2480)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitAndExpr(Parser.java:2469)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitXorExpr(Parser.java:2458)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitOrExpr(Parser.java:2447)
at net.sourceforge.htmlunit.corejs.javascript.Parser.andExpr(Parser.java:2436)
at net.sourceforge.htmlunit.corejs.javascript.Parser.orExpr(Parser.java:2425)
at net.sourceforge.htmlunit.corejs.javascript.Parser.condExpr(Parser.java:2389)
at net.sourceforge.htmlunit.corejs.javascript.Parser.assignExpr(Parser.java:2345)
at net.sourceforge.htmlunit.corejs.javascript.Parser.expr(Parser.java:2324)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper(Parser.java:1282)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:673)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:594)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2601)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1583)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:216)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1572)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.doRun(JavaScriptEngine.java:714)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:877)
... 34 more
Enclosed exception:
net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: illegally formed XML syntax (script in https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject from (11, 54) to (39, 10)#38)
at com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.error(StrictErrorReporter.java:65)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:260)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:232)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:228)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getNextXMLToken(TokenStream.java:1287)
at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getFirstXMLToken(TokenStream.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.xmlInitializer(Parser.java:2666)
at net.sourceforge.htmlunit.corejs.javascript.Parser.unaryExpr(Parser.java:2641)
at net.sourceforge.htmlunit.corejs.javascript.Parser.mulExpr(Parser.java:2568)
at net.sourceforge.htmlunit.corejs.javascript.Parser.addExpr(Parser.java:2552)
at net.sourceforge.htmlunit.corejs.javascript.Parser.shiftExpr(Parser.java:2533)
at net.sourceforge.htmlunit.corejs.javascript.Parser.relExpr(Parser.java:2508)
at net.sourceforge.htmlunit.corejs.javascript.Parser.eqExpr(Parser.java:2480)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitAndExpr(Parser.java:2469)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitXorExpr(Parser.java:2458)
at net.sourceforge.htmlunit.corejs.javascript.Parser.bitOrExpr(Parser.java:2447)
at net.sourceforge.htmlunit.corejs.javascript.Parser.andExpr(Parser.java:2436)
at net.sourceforge.htmlunit.corejs.javascript.Parser.orExpr(Parser.java:2425)
at net.sourceforge.htmlunit.corejs.javascript.Parser.condExpr(Parser.java:2389)
at net.sourceforge.htmlunit.corejs.javascript.Parser.assignExpr(Parser.java:2345)
at net.sourceforge.htmlunit.corejs.javascript.Parser.expr(Parser.java:2324)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper(Parser.java:1282)
at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:1136)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:673)
at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:594)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2601)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1583)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:216)
at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1572)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.doRun(JavaScriptEngine.java:714)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:877)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:616)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:723)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:689)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:735)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:316)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:396)
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:246)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:267)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:802)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:758)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1194)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1134)
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:221)
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:314)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3179)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2132)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:939)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:452)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:403)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:1001)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:250)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:196)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:158)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:531)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:398)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
at FuckMerced.main(FuckMerced.java:34)
== CALLING JAVASCRIPT ==
<!-- Hide JavaScript from older browsers
var submitcount=0;
function checkSubmit() {
if (submitcount == 0)
{
submitcount++;
return true;
}
else
{
alert("Your changes have already been submitted.");
return false;
}
}
// End script hiding -->
<script type="text/javascript">
<!-- Hide JavaScript from older browsers
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31337262-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// End script hiding -->
======= EXCEPTION END ========
And here is the HTML of the form I am accessing in case the link is being weird:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV="Pragma" NAME="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" NAME="Cache-Control" CONTENT="no-cache">
<LINK REL="stylesheet" HREF="/css/web_defaultapp.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="/css/web_defaultprint.css" TYPE="text/css" media="print">
<TITLE>Search Courses by Subject</TITLE>
<META HTTP-EQUIV="Content-Script-Type" NAME="Default_Script_Language" CONTENT="text/javascript">
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!-- Hide JavaScript from older browsers
var submitcount=0;
function checkSubmit() {
if (submitcount == 0)
{
submitcount++;
return true;
}
else
{
alert("Your changes have already been submitted.");
return false;
}
}
// End script hiding -->
<script type="text/javascript">
<!-- Hide JavaScript from older browsers
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31337262-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// End script hiding -->
</script>
</HEAD>
<BODY>
<DIV class="headerwrapperdiv">
<TABLE CLASS="plaintable" SUMMARY="This table displays Menu Items and Banner Search textbox."
WIDTH="100%">
<TR>
<TD CLASS="pldefault"></TD>
<TD CLASS="pldefault"><p class="rightaligntext"></p>
</TD></TR></TABLE>
</DIV>
<DIV class="pagetitlediv">
<TABLE CLASS="plaintable" SUMMARY="This table displays title and static header displays."
WIDTH="100%">
<TR>
<TD CLASS="pldefault"><br /><br /><br /></TD>
<TD CLASS="pldefault"> </TD>
<TD CLASS="pldefault"><p class="rightaligntext"></p>
<DIV class="staticheaders">
</div>
</TD></TR><TR>
<TD width="100%" colSpan=3> </TD>
</TR></TABLE>
</DIV>
<DIV class="pagebodydiv">
UC Merced Schedule--Search Courses by Term or Subject <H4>
Interested in UC Online courses offered at other UC campuses? Check out information at UC Online.
<FORM ACTION="xhwschedule.P_ViewSchedule" METHOD="post">
<TABLE CLASS="plaintable" >
<TR>
<TD COLSPAN="2" CLASS="pldefault">Select a Term:</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - All Courses</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S6" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - First 6-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S62" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - Second 6-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201820 - S8" CHECKED>
<TD CLASS="pldefault">Summer Semester 2018 - 8-week Summer Session</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201830" CHECKED>
<TD CLASS="pldefault">Fall Semester 2018</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="validterm" VALUE="201910" CHECKED>
<TD CLASS="pldefault">Spring Semester 2019</TD>
</TR>
</SELECT>
<BR>
<TR>
<TD CLASS="pldefault"> </TD>
</TR>
<TR>
<TD CLASS="pldefault">Subject:</TD>
<TD CLASS="pldefault">
<SELECT NAME="subjcode">
<OPTION VALUE="ALL">All Subjects
<OPTION VALUE="ANTH">Anthropology
<OPTION VALUE="BEST">Bio Engin Small Scale Tech
<OPTION VALUE="BIOE">Bioengineering
<OPTION VALUE="BIO">Biological Sciences
<OPTION VALUE="CHEM">Chemistry
<OPTION VALUE="CCST">Chicano Chicana Studies
<OPTION VALUE="CHN">Chinese
<OPTION VALUE="COGS">Cognitive Science
<OPTION VALUE="CRS">Community Research and Service
<OPTION VALUE="CSE">Computer Science & Engineering
<OPTION VALUE="CORE">Core
<OPTION VALUE="CRES">Critical Race & Ethnic Studies
<OPTION VALUE="ESS">Earth Systems Science
<OPTION VALUE="ECON">Economics
<OPTION VALUE="EDUC">Education
<OPTION VALUE="EECS">Elect. Engr. & Comp. Sci.
<OPTION VALUE="ENGR">Engineering
<OPTION VALUE="ENG">English
<OPTION VALUE="ENVE">Environmental Engineering
<OPTION VALUE="ES">Environmental Systems (GR)
<OPTION VALUE="FRE">French
<OPTION VALUE="GEOG">Geography
<OPTION VALUE="GASP">Global Arts Studies Program
<OPTION VALUE="HIST">History
<OPTION VALUE="HBIO">Human Biology
<OPTION VALUE="IH">Interdisciplinary Humanities
<OPTION VALUE="JPN">Japanese
<OPTION VALUE="MGMT">Management
<OPTION VALUE="MBSE">Materials & BioMat Sci & Engr
<OPTION VALUE="MSE">Materials Science & Engr
<OPTION VALUE="MATH">Mathematics
<OPTION VALUE="ME">Mechanical Engineering
<OPTION VALUE="MIST">Mgmt of Innov, Sust, and Tech
<OPTION VALUE="NSUS">Nat Sciences Undergrad Studies
<OPTION VALUE="NSED">Natural Sciences Education
<OPTION VALUE="PHIL">Philosophy
<OPTION VALUE="PHYS">Physics
<OPTION VALUE="POLI">Political Science
<OPTION VALUE="PSY">Psychology
<OPTION VALUE="PH">Public Health
<OPTION VALUE="PUBP">Public Policy
<OPTION VALUE="QSB">Quantitative & Systems Biology
<OPTION VALUE="SCS">Social Sciences
<OPTION VALUE="SOC">Sociology
<OPTION VALUE="SPAN">Spanish
<OPTION VALUE="SPRK">Spark
<OPTION VALUE="USTU">Undergraduate Studies
<OPTION VALUE="WCH">World Cultures & History
<OPTION VALUE="WH">World Heritage
<OPTION VALUE="WRI">Writing
</SELECT>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="openclasses" VALUE="Y" CHECKED>
<TD CLASS="pldefault">Open Classes Only</TD>
</TR>
<TR>
<TD CLASS="pldefault">
<INPUT TYPE="radio" NAME="openclasses" VALUE="N">
<TD CLASS="pldefault">All Classes</TD>
</TR>
</TABLE>
<BR>
<BR>
<INPUT TYPE="submit" VALUE="View Class Schedule">
</FORM>
<!-- ** START OF twbkwbis.P_CloseDoc ** -->
<TABLE CLASS="plaintable" SUMMARY="This is table displays line separator at end of the page."
WIDTH="100%" cellSpacing=0 cellPadding=0 border=0><TR><TD class="bgtabon" width="100%" colSpan=2><IMG SRC="/wtlgifs/web_transparent.gif" ALT="Transparent Image" CLASS="headerImg" TITLE="Transparent Image" NAME="web_transparent" HSPACE=0 VSPACE=0 BORDER=0 HEIGHT=3 WIDTH=10></TD></TR></TABLE>
Skip to top of page
</DIV>
<DIV class="footerbeforediv">
</DIV>
<DIV class="footerafterdiv">
</DIV>
<DIV class="globalafterdiv">
</DIV>
<DIV class="globalfooterdiv">
</DIV>
<DIV class="pagefooterdiv">
<SPAN class="releasetext">Release: 7.3 - Developed by UCM SIS</SPAN>
</DIV>
<DIV class="poweredbydiv">
</DIV>
<DIV class="div1"></DIV>
<DIV class="div2"></DIV>
<DIV class="div3"></DIV>
<DIV class="div4"></DIV>
<DIV class="div5"></DIV>
<DIV class="div6"></DIV>
<div class="banner_copyright"> <br><h5>© 2019 Ellucian Company L.P. and its affiliates.<br></h5></div>
</BODY>
</HTML>
Sorry for the lengthy question, hopefully I can pay it back to the community one day :)
I am a bit out of sync with web programming and getting into HTMLUnit
is a bit more confusing than I thought it would be.
If you like to automate web page these days you need a basic understanding of web technologies at least Html, Javascript and HTTP itself to be able to figure out what to do.
Lets start at the top - with your
lovely error
As a starting point open the page with a real browser and have a look at the web console. You will see the same error there; that means the page you are trying to automate has an error (at least one) and your browser simply ignores this.
HtmlUnit was created as a test tool; because of this it is more picky about errors. You have to disable this.
webClient.getOptions().setThrowExceptionOnScriptError(false);
Next step:
You are trying to access the form on the page
<FORM ACTION="xhwschedule.P_ViewSchedule" METHOD="post">
As the method name implies 'getFormByName()' is able to find forms having the right name attribute - but your form does not have one.
Next step:
<INPUT TYPE="radio" NAME="validterm" VALUE="201910" CHECKED>
As the method name implies 'getElementById("201910")' is able to find elements having the right id attribute - but your radio button does not have one.
And the same for the button.
Below you can find a quick hack that does the work. It might help to read at least the HtmlUnit - Getting Started with HtmlUnit page. There is also the javadoc available with detailed descriptions.
Hope that helps
public static void main(String[] args) throws IOException {
String url = "https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.p_selectsubject";
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(1000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
final HtmlForm form = page.getForms().get(0);
for (DomElement elem : form.getElementsByTagName("INPUT")) {
if (elem instanceof HtmlRadioButtonInput) {
HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) elem;
if ("201910".equals(radioButton.getValueAttribute())
|| "N".equals(radioButton.getValueAttribute())) {
radioButton.setChecked(true);
}
}
}
for (DomElement elem : form.getElementsByTagName("INPUT")) {
if (elem instanceof HtmlSubmitInput) {
if ("View Class Schedule".equals(elem.getAttribute("value"))) {
elem.click();
}
}
}
webClient.waitForBackgroundJavaScript(1000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println("----------------");
System.out.println(page.asXml());
}
}

Moving to different servlet other than form action

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>

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

Java Get ALL elements from a multiselect Box

I have a multi-select Box and I'm doing some javascript to sort the order of the elements in the box. I want to submit the whole array after sorting back to the Java and not just the select items. How can I achieve this?
JSP:
<script type="text/javascript">
$(document).ready(function(){
$("#mup")[0].attachEvent("onclick", moveUpItem);
$("#mdown")[0].attachEvent("onclick", moveDownItem);
});
function moveUpItem(){
$('#list option:selected').each(function(){
$(this).insertBefore($(this).prev());
});
}
function moveDownItem(){
$('#list option:selected').each(function(){
$(this).insertAfter($(this).next());
});
}
</script>
<table width="100%">
<tr>
<td width="50%" align="center">
<h1>DET Column Maintenance</h1>
</td>
</tr>
</table>
<form action="process.det_column_order" method="post" name="detColumnSortorder" >
<table class="data_table">
<tr align="center">
<td>
<select id="list" name="fieldNames" size="35" multiple="multiple" style="width: 250px;">
<c:forEach var="field" items="${detFields}">
<option value="${field.fieldName}">${field.displayName}</option>
</c:forEach>
</select>
</td>
<tr>
<td style="text-align: center;">
<button id="mup">Move Up</button>
<button id="mdown">Move Down</button>
</td>
</tr>
<tr>
<td style="text-align: center;">
<input name="action" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
FORM:
private String[] fieldNames;
public String[] getFieldNames() { return this.fieldNames; }
public void setFieldNames(String[] val) { this.fieldNames = val; }
Since forms only submit the selected values, you'll need a little more JS and another form field.
Introduce a hidden form field that will hold the values you care about:
<input type="hidden" name="fieldNamesOrder" id="fieldNamesOrder"/>
And after every click of Move Up/Move Down:
var order = [], sel = document.getElementById("list");
for(var i = 0, len = sel.options.length; i < len; i++) {
order.push(sel.options(i).value);
}
document.getElementById("fieldNamesOrder").value = order.join(",");
Then, on the server side, you can read your ordered field names out of that posted field.
You either need to create a hidden text field in your HTML form that stores the values from each of your options... or you need to programatically select all of the options before the form is submitted.

Working with <div> tag in JSP for making the contents hidden or visible

I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>

Categories

Resources