Nirmalya
Nirmalya

Reputation: 420

jsp to servlet request.getParameter not working

I am a in a precarious situation! I have a html page with two frames.

1. xmldiff.html

<html>
 <head>
  <title>XML Diff</title>
</head>
<form name="xmldiff" method="post">
 <frameset rows="50%,50%">
   <frame name="left" src="XML1.jsp">
   <frame name="bottom" src="XML2.jsp">
 </frameset>
</html>

2. XML1.jsp

<form name="xml1">
XML 1:<br />
  <textarea name="xml1text" id="comments" style="width:100%;background-color:#D0F18F;" rows="50">
  Text Area 1!
  </textarea><br />
</form>

3. XML2.jsp

<script language="javascript" type="text/javascript">
function mysubmit()
{
document.xml3.text1.value=window.parent.left.xml1.xml1text.value;
}
</script>
<form name="xml2" action="/servlet/XMLDiffServlet" method="post" onSubmit="mysubmit()> 
Difference:<br />
  <textarea name="result" id="comments" style="width:100%;background-color:#D0F18F;" rows="22">${result}</textarea>
  <br />
  <input type="submit" value="Submit"/>
  <input type="hidden" name="text1" value="hello" />
</form>

And in the servlet XMLDiffServlet I am trying to retrieve the value of hidden button text1 by the following code -

public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
    String origXML=req.getParameter("text1");

    req.setAttribute("result",origXML);
    String nextJSP = "/XML2.jsp";
    RequestDispatcher rd = getServletContext().getRequestDispatcher(nextJSP);
    rd.forward(req, resp);
} 

So basically I am just trying passing the value of the hidden button and trying to display it in the result textarea. If I use XML2.jsp, I am not able to pass the value to the servlet, but I can retrieve any string from the servlet by using ${result}. On the other hand if I just rename XML2.jsp to XML2.html (without changing any code), I am able to pass the value of the hidden button to the servlet, but can't retrieve from it.

So how can I meet both the ends using a jsp - both pass the value and retrieve? Why request.getParameter is working from html and not for jsp? There are plenty of examples where this is working fine, but why not here? I created the onClick call in the submit button as well, but that stuff also didn't work. I am really baffled - can you please help me out?

Thanks Nirmalya

Upvotes: 0

Views: 1760

Answers (1)

Pankaj Sharma
Pankaj Sharma

Reputation: 1853

you should use onSubmit like

function mysubmit()
{
   document.xml3.text1.value=window.parent.left.xml1.xml1text.value;
   return true;
}

and in form

<form name="xml2" action="/servlet/XMLDiffServlet" method="post" onSubmit="return mysubmit()">

Upvotes: -1

Related Questions