not able to forward values calculated in a servlet to a jsp page using RequestDispatcher.How to do the same?
In my code..i have retrieved the value of input(text type) from a jsp page in a servlet using request.getParameter()
.
Now using these values,i have performed some calculation.
now i want to forward the calculated value (i.e. the result) as well as the operands.. i e. the retrieved value to a new jsp page. I am doing this by setting the values using request.setAttribute()
and forwarding to the destined jsp page using getRequestDispatcher().forward(request,response)
.
But my page is not being forwarded.
package imsf;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import Service;
public class CalcPurRate extends HttpServlet {
String iname;
String cname;
String mrate;
String irate;
String disR;
String disP;
String newAdd;
String q;
int imr;
int iir;
int idR;
int idP;
String dP;
String dR;
int ina;
int iprate;
int total;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String in=request.getParameter("iname");
out.print("my name"+in);
String cn=request.getParameter("cname");
out.print("my cate"+cn);
if(request.getParameter("mrate")!=null)
{
String mr=request.getParameter("mrate");
if(!(mr.equals(null)))
{
imr=Integer.parseInt(mr);
out.print(" m开发者_开发问答rp= "+imr);
}
}
if(request.getParameter("irate")!=null)
{
String ir=request.getParameter("irate");
if(!(ir.equals(null)))
{
iir=Integer.parseInt(ir);
out.print("invoice= "+iir);
}
}
if(request.getParameter("disR")!=null)
{
out.print("disR not null");
dR=request.getParameter("disR");
if(!(dR.equals(null)))
{
out.print("disR not zero");
idR=Integer.parseInt(dR);
/*if(request.getParameter("disP")==null)
{
out.print("disP not zero");
dP=request.getParameter("disP");
if(dP.equals(null))
{
out.print("disP not zero");
iprate=((iir)-(idR));
out.print("iprate="+iprate);
}
}*/
out.print("integer dis R="+idR);
iprate=((iir)-(idR));
out.print("iprate="+iprate);
}
}
/*if(request.getParameter("disP")!=null)
{
out.print("disP not null");
dP=request.getParameter("disP");
if(!(dP.equals(null)))
{
out.print("disP value="+dP);
out.print("disP not zero");
/*if(request.getParameter("disR")==null)
{
dR=request.getParameter("disR");
if(dR.equals(null))
{
idP=Integer.parseInt(dP);
out.print("integer dis P="+idP);
iprate=((iir)-(((idP)*(iir))/100));
out.print("pur rate"+iprate);
}
}*/
//}
//}
/*if(idR!=0)
{
out.print("innn");
iprate=((iir)-(idR));
out.print("pur rate"+iprate);
}
out.print("heheh");
if(idP!=0)
{
iprate=iir-((idP*iir)/100);
out.print("vgfhg"+iprate);
}*/
if(request.getParameter("newAdd")!=null)
{
String na=request.getParameter("newAdd");
if(!(na.equals(null)))
{
ina=Integer.parseInt(na);
total=((ina)*(iprate));
out.print("total="+total);
}
}
request.setAttribute("purRate",iprate);
request.setAttribute("total",total);
//response.sendRedirect("ItemDetail.jsp");
RequestDispatcher rd=request.getRequestDispatcher("/ItemDetail.jsp");
rd.forward(request, response);
}
catch(Exception e)
{
e.getMessage();
}
}
}
processRequest()
is not a method that has any meaning to an HttpServlet
. Your servlet is actually doing nothing whatsoever - processRequest()
will not be invoked by anything.
You should be implementing either doGet()
or doPost()
, and put your logic in there.
Incidentally, storing data in fields of a servlet like this is an extremely bad idea. It's not thread-safe - multiple concurrent requests will corrupt your servlet's state.
if you want to land in a new page, just use the sendRedirect() method. by the way the details that you have provided looks correct. try to get the parameters at the destination jsp and let us know.. it should work..
HTH
精彩评论