Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting null parameters from servlet

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have made following servlet
package table;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import table.TABLEDATA;

public class connectionservlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String roll=request.getParameter("rollnumber");
table.TABLEDATA tableobject = new TABLEDATA(roll);
int totalcolumns=tableobject.getcolumncount();
System.out.println("In connectionservlet are"+totalcolumns);
String[] columnname = new String[totalcolumns];
int[] columnvalue = new int[totalcolumns];
for (int i=3;i<totalcolumns;i++)
{
columnname[i]=tableobject.getcolumnname(i);
columnvalue[i]=Integer.parseInt(tableobject.getcolumnvalue(i));
}
int total=tableobject.gettotal();
int perc=tableobject.getperc();

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/connection2.jsp");
dispatcher.forward(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

However in the connection2.jsp i receive null values when i call
request.getParameter("totalcolumns");
request.getParameter("total");
etc.
I have checked the servlet is correctly receiving value from tabledata class. totalcolumns shown in servlet are 7 but when called from connection2.jsp shows to be null.

Here is the code for connection2.jsp

<%System.out.println("Total Columns are " + request.getParameter("totalcolumns")+"and the total is" +request.getParameter("total"));%>
<table cellspacing="5" cellpadding="5" border="1" style="position:absolute;left:500px;top:100px">

<%
String columnname,columnvalue;


for (int i=3;i<=Integer.parseInt(request.getParameter("totalcolumns"));i++)
{
String str="columnname["+Integer.toString(i)+"]";
String str1="columnvalue["+Integer.toString(i)+"]";
columnname=request.getParameter(str);
columnvalue=request.getParameter(str1);;
%>
<tr>
<td> <%=columnname%> </td>
<td> <%=columnvalue%> </td>
<% }
int total=Integer.parseInt(request.getParameter("total"));
int perc=Integer.parseInt(request.getParameter("perc"));
%>

<Div style="position: absolute; top:350px;left:380px; width:200px; height:25px; color arkRed"<b>Total Marks Obtained = <%=total%> </b></DIV>
<Div style="position: absolute; top:350px;left:650px; width:200px; height:25px; color arkRed"<b>Percentage= <%=perc%> </b></DIV>


</table>

Kindly help.

</BODY>
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anand,
First of all you will need to set an ATTRIBUTE in the request using request.setAttribute(name,value) and then forward it to the connection2.jsp.
Secondly you need to say request.getAttribute(name) in the JSP to retrieve the desired values.
You CAN NOT use request.getParameters() to acheive this. getParameters() returns the parameter passed in the Http request from browser.On the server side when you want to forward the request from a servlet to a JSP(Standard case) you have to set the attributes and get the attributes. There is no such method to setParameter() etc.
regards
Amit
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having 'totalColumns' as a local field in the servlet does not make it a parameter in the forwarded JSP.

Put the data in request.setAttribute and read from getAttribute (you will need to use object wrappers, not primitives)

In future, please put code in code blocks, it makes it easier to read.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks friends.

I am learning from Murach's Java Servlets and JSPs and I am amazed that in that book they have used getParameter. If some one has it he can check the JSP Code for show_email_entry.jsp given at page no.171 of the Indian Edition of the book, reprint 2004. (This was the latest available in India).
 
He baked a muffin that stole my car! And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic