Why am I getting parameter value as null? - java

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.

Related

Pagination issue with JSP

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.

Passing localstorage value to Server (Java, JSP)

im currently creating a canvas Javascript game and im passing localstorage variable to my database which is the Score. Score being 'elapsed'. When i was debugging i saw that the localstorage value is there. But it just doesnt seem to store into the database. Preferably without the use of Ajax?
function drawElapsedTime(){
var elapsed=parseInt((new Date() - startTime)/1000);
g.save();
g.beginPath();
g.fillStyle="#008000";
g.font="14px Verdana"
// draw the running time at half opacity
g.globalAlpha=0.50;
g.fillText(elapsed+" secs",canvas.width-75,25);
localStorage.setItem("score",elapsed);
g.restore();
}
var storedTime = localStorage.getItem("score");
<form action="UserActionServlet" method="post">
<input type="submit" value="scoreObj" class="submit" />
</form>
Command:
String xscore = request.getParameter("score");
int score = 0;
if (xscore != null) {
score = Integer.parseInt(xscore);
}
System.out.println(xscore);
You are not passing the score variable as a parameter in your form post.
<form action="UserActionServlet" method="post" onSubmit="submitForm(this.form)">
<input type = "hidden" name = "score"/>
<input type="submit" value="scoreObj" class="submit" />
</form>
<script>
function submitForm(f){
f.score.value = storedTime ;
f.submit();
}
</script>

a story about this one javascript function that doesn't work

im creating a jsp application , and i got few functions running there. but there is this one function. only one that doesn't work no matter what i do!
i even built it again now from another running functions and it doesn't work!
here are my functions -
<script type="text/javascript">
function ValidateAdd(){
var x=document.add.name.value;
var y=document.add.jobId.value;
var z=document.add.deptId.value;
var w=document.add.salary.value;
if(x==""||x==null||y==""||y==null||z==""||z==null||w==""||w==null){
alert("fields cant be empty");
return false;
}
if(/\D/.test(y)||/\D/.test(z)||/\D/.test(y)){
alert("job id , dept id and salary need to be numeric!");
return false;
}
}
function ValidateDelete(){
var x=document.del.empId.value;
if(/\D/.test(x)){
alert("empId needs to be numeric!");
return false;
}
}
function ValidateGet(){
var x=document.get.empId.value;
if(/\D/.test(x)){
alert("empId needs to be numeric!");
return false;
}
}
function ValidateUpdate(){
var x=document.add.name.value;
var y=document.add.jobId.value;
var z=document.add.deptId.value;
var w=document.add.salary.value;
var v=document.add.empId.value;
if(x==""||x==null||y==""||y==null||z==""||z==null||w==""||w==null||v==""||v==null){
alert("fields cant be empty");
return false;
}
if(/\D/.test(y)||/\D/.test(z)||/\D/.test(y)||/\D/.test(v)){
alert("employee id ,job id , dept id and salary need to be numeric!");
return false;
}
}
</script>
my form is -
<form name="update" onsubmit="return ValidateUpdate();" action="GetAndUpdateEmp" method="post" >
update an employee:
<br>
empId<input type="text" name="empId"/><br>
name<input type="text" name="name"/><br>
jobId<input type="text" name="jobId"/><br>
deptId<input type="text" name="deptId"/><br>
Salary<input type="text" name="salary"/><br>
<input type="submit"/>
</form>
another form that works just fine -
<form name="get" onsubmit="return ValidateGet();" action="GetAndUpdateEmp" method="get" >
get an employee by id:
<br>
empId<input type="text" name="empId"/><br>
<input type="submit"/>
</form>
im really starting to get crazy i cant find whats wrong
The form reference inside ValidateUpdate method does not seem right. You have defined it as "update" but trying to refer to it by "add".

Passing a javascript variable in a JSP form

I want to pass a value calculated on the client-side, back to the server. Here is my code:
<script>
var today = new Date();
function startTime() {
document.getElementById('time').innerHTML = today.getTime();
t = setTimeout(function () {
startTime()
}, 1);
}
startTime();
</script>
<form method="POST" action="../frontier/warehouseops/servlet/TimeCalculator">
<input type="hidden" name="time" value="time"/>
<input type="submit" value="Submit" name="buttonSubmit"/>
</form>
What I want to happen is that the value of the javascript variable "time" gets passed to the server. But what is happening, is simply the string "time" is being passed to the server. How do I access the value of a javascript variable and pass it back to the server in a form?
instead of:
document.getElementById('time').innerHTML = today.getTime();
try this:
document.getElementsByName("time")[0].value = today.getTime();
Well
<input type="hidden" name="time" value="time"/>
This line will pass string "time" only as the value attribute of this hiddle form field has "time" as string. What you realy need to do is write a javascript function onSubmit of form and then set the value of this parameter to whatever you want.
<script>
function submit(form)
{
form.time.value = new Date();
document.getElementById("myForm").submit();
}
</script>
<form id="myForm" method="POST" action="../frontier/warehouseops/servlet/TimeCalculator">
<input type="hidden" name="time" value="time"/>
<input type="submit" value="Submit" name="buttonSubmit" onClick="submit(this.form)"/>
</form>
Hope this helps

how to know that how many dynamic controls created?

I have created dynamic row on click of button in table using following code :
<script type="text/javascript">
var counter = 1;
function displayResult()
{
counter++;
document.getElementById("myTable").insertRow(-1).innerHTML = '<td><select name="list_dispatch_state" id="list_dispatch_state"><option value="01">01</option><option value="02">02</option><option value="03">03</option></select></td><td><input type="text" name="txt_email'+ counter +'" id="txt_email'+ counter +'" value='+ counter +'></td>';
}
</script>
<body>
<form action="Dogetdat" method="post">
<table id="myTable" border="1">
<tr>
<th>Select</th>
<th>Value</th>
</tr>
</table>
<br />
<button type="button" onclick="displayResult()">Insert new row</button>
<input type="submit">
</form>
</body>
my question is that, on click of button new row and control inside it created but when I click on submit then form submitted to servlet page.
Then how servlet will know that how many data are received ?
Because in servlet I ll get data using
String str1= request.gerParameter("txt_email");
how servlet will know that how many variable it have to create and what will be the name of that ? what will I have to pass in request.gerParameter(""); ?
You have to use the following request.getParameterValues and get the result as an array
String emails[] = request.getParameterValues("txt_email");

Categories