Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting null parameters from servlet  RSS feed

 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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>
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!