aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in reDirect and post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in reDirect and post" Watch "Doubt in reDirect and post" New topic
Author

Doubt in reDirect and post

sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi All,
I have created a html page having a checkbox and two servlet.When a user select submit the form using the get method it tranfer the req to first servlet.First servlet redirect the request to the next servlet using response.redirect("url") what i need it is the second servlet has to collect the data selected by the user.The problem is iam unable to find the selected checkbox by the user.O/P iam getting is NullPointerException
The code is:
Html Page :
<html>
<head>this is 3 parmeter
<title>this is first</title></head>
<body>
<form Method =get action = "redi">
color :<select name = "c">
<option> light
<option> dark
<option> thick
<option> amber
</select></br>
<input type = checkbox name =a value = "first">first</br>
<input type =checkbox name =a value ="second">second</br>
<input type =checkbox name =a value = "third">third</br>
<input type = "submit">
</form></body>
</html>

First Servlet code is
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class redi extends HttpServlet
{
public void doGet(HttpServletRequest req ,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
res.sendRedirect("fin");
}
}

Second servlet code is:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class fin extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("from final");
String s3=req.getParameter("c");
pw.println("</br>");
String ss[]=req.getParameterValues("a");
//String ss1=req.getParameterValues("a")[1];
//pw.println(ss1+" values at 1 </br>"+"vlues : ");
for(String st:ss)
pw.println(st+" , ");
pw.println(s3+"<br>");
pw.println("sdaf"+"<br>");
pw.println(req.getHeader("User-Agent")+"<br>");
pw.println(req.getMethod()+"<br>");
pw.println(req.getContentLength()+": header names");

}
}
Thanks in Advance
sankar
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Hi sundar, is this a doubt or a question?

I do not doubt that the redirect is happenning so I will assume that your question is "Why is the posted parameter lost?"

When you re-direct you return to the browser and the browser sends a new request (which does not have your posted info) to where ever you wanted to re-direct. If you forward, you will not lose your posted information

Please let us know if this was your question and that you have received the information you need to understand what is happening
sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi Michael Ku,
Thanks a lot you are perfactely rignt and your explaination cleared my doubt.
regards
sankar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in reDirect and post