Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in reDirect and post

 
sundar sankar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael Ku,
Thanks a lot you are perfactely rignt and your explaination cleared my doubt.
regards
sankar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic