I have a JSP page where i am fetching value from html textbox.
I want to insert that value into MySQL database.
I want if textbox in empty,null,zero and undefined then insert 0 in database otherwise actual value insert in database.
Here is my code.
Strins s1=request.getparameter("edate");
s1="1990-07-16 09:12:45"
int s4=0
<script>
var a=<%=s1%>
if(a==null || a==undefined || a=='' || a==0){
<% psmnt.setInt(6,s4); %>
}
else{
<% psmnt.setString(6,s1); %>
}
</script>
Is your texbox <texarea> or <input type="text">?
If you are using <input type="text">, put default value on it:
<input type="text" value="">
While for <textarea>, <textarea></textarea> should give your default value ""
So that you only need to check
if(a==''){
......
}
Or if you didn't allow white space too, do this:
if(a=='' || a.trim() ==''){
......
}
You can use this
<script>
if(typeof a == 'undefined' || a == 0) {
// Insert db 0
}
</script>
Related
I am able to display a table when a user clicks a search button. But now I want to split the table into chunks of 20 rows where the user can click next or previous to go forward and backward through all of the data. I am not allowed to use JavaScript for this assignment. Only JSP, Java, HTML.
The two debugging out.print() calls are not showing up. A different page is being loaded after one of the buttons is clicked, but the two debugging out.print calls are not displaying any HTML. I have checked out How to know which button is clicked on jsp this post but had no luck.
<form method="GET">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
try {
if ("Previous".equals(val1)) { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if ("Next".equals(val2)) { // Get next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
I also have a follow up question. If the user clicks next or previous button, will my current table on the page be overwritten by the new one? That is my intent but I don't know if it will work that way.
I WAS ABLE TO FIX IT BY DOING THIS:
<form method="POST">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
you should add name with value for button after that you can get by parameter click value.
`<input type="hidden" name="myprevious" value="previous"/>
<input type="hidden" name="mynext" value="next" />
<%
String val1 = request.getParameter("myprevious");
String val2 = request.getParameter("mynext");
try {
if (val1 == "previous") { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if (val2 == "next") { // Go next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
`
I hope it will help you.
Thanks.
Try to use the subList() method of a List<>().
As explained here.
HOW TO IMPLEMENT IT ##
you can put an hidden input in your form to give you the last index for your list like :
<input type="hiden" value="${last_index_of_your_list + 1}" name="index">
Then in your servlet part you put like this :
int index = Interger.ParseInt(request.getParameter("index"));
if(index <= 0){
datalist = datalist(0, 19>datalist.size()? datalist.size() : 19);
}else{
if(clicked_on_next){
datalist = datalist(index, index+19>datalist.size()? datalist.size() : index+19 );
}else{
datalist = datalist(index - 40, index-20>datalist.size()? datalist.size() : index-20 );
}
}
You are using hidden fields but you need to use submit button for next and previous.
<input type="submit" name="myprevious" value="previous"/>
<input type="submit" name="mynext" value="next" />
Make sure both are define in form. when we submit form after that you will get button value in parameter.same my previous answer. because we can not get parameter value without submit form.
Thanks.
I hooked login.jsp and wrote following code in it:-
<script>
function a(){
var otp=document.getElementById("otpText").value;
document.location.href="?otp="+otp;
<% System.out.println(request.getParameter("otp")); %> //printing null
return false;
}
</script>
<form name="otp" method="post" action="" >
Enter otp:
<input type="text" name="otpText" id="otpText"/>
<input type="submit" value="Submit" onClick="return a()"/>
</form>
I typed some value inside otpText and that value is displayed in the url when I submitted the form. But when I am printing that value as in above code,it is printing null. I need that value as I want to store the same into session.Please help, any help would be appreciated.
If you change your code to
var otp=document.getElementById("otpText").value;
console.log (otp); // or alert (otp);
document.location.href="?otp="+otp;
you will see the value.
In my jsp i have a variable
String str = "Get_Name";
What can i do to have my variable(str) directly accessed in the input type shown below? By this I want to see the form auto filled with the the variable I have instead of me filling it.
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" />
ANSWER ALSO CONTAINS SOLUTION FOR PROBLEM DISCUSSED IN COMMENTS.
Do this.
<%
long val = 2; //can be anything.
if(val > 1) {
%>
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" value="<%=str%>"/>
<%
else {
%>
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" />
<%
}
%>
<%=str%> is called as expression tag.
I want design a web form,its have to control one Text Box and one Drop Down list when type a number between 5 and 10 in Text Box, Drop Down list is include 1,2,3,4,5 and when type a number between 11 and 20 in Text Box, Drop Down list is include 6,7,8,9,10.
How to do it without page refreshing?
please explain with code.
I think the following code may help you.
<select id="txt" onchange="changeNumber()">
<option>select value</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
</select>
<select id="mySelect">
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
</select>
<script>
function changeNumber(){
var t_value=document.getElementById('txt').value;
if(t_value==10 ||t_value==11 || t_value==12 || t_value==13 || t_value==14 || t_value==15 ){
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect");
var j=1
for(var i=0;i<=y.options.length;i++){
y[i].text=j;
j++;
}
}
}
</script>
In this way you can solve your 2nd problem.
You can also follow the below code.
<input type="text" id="txt" onkeyup="checkValue();" />
<select id="mySelect">
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
</select>
<script>
function checkValue(){
var t_value=document.getElementById('txt').value;
if(t_value==10 ||t_value==11 || t_value==12 || t_value==13 || t_value==14 || t_value==15){
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect");
var j=1
for(var i=0;i<=y.options.length;i++){
y[i].text=j;
j++;
}
}
}
</script>
I think this type of solution you need.
I am getting gra value from one.jsp as follows
<script language="javascript">
function gotoAddPanelAction(elem)
{
var st=elem.value;
if(st!="")
{
Popup=window.open('SelectInterviewPannel2.jsp?gra='+st,'Popup','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes',400,400);
}
else
{
validateForm(document.frm);
}
}
</script>
I am retriving the value in SelectInterviewPannel2.jsp as follows
<td width="60%" class="txt-lable">
<Select name="grade" ><option value="" selected>Select</option>
<%
String gra = request.getParameter("gra");
if(gra.value=="Level 1") {
%>
<option value="E1">E1</option><option value="E2">E2</option><option value="E3">E3</option><option value="E4">E4</option></select>
<% } else {%>
<option value="M1">M1</option><option value="M2">M2</option></select>
<% } %>
</td>
My problem is in SelectInterviewPannel2.jsp if statement is not executing . I am getting only drop down box for select with no values in it.
if(gra.value=="Level 1") {
Shouldn't you use equals() method for string comparission
Some Suggestions:
Don't use java code on jsp.
First of all why this title: how to remove comma in integer in java it does not match with your question.
As an answer:
Instead of this
if(gra.value=="Level 1")
use this
if(gra.equals("Level 1"))
In java == Compares references, not values and to compare values of string you should use .equals(str).
If gra parameters does not exist , request.getParameter("gra") will return null and comparing using if(gra.equals("Level 1")) will throw out Null pointer exception . So you can try to use if("Level1".equals(gra)) to avoid the Null pointer exception when the gra parameters does not exist