how to click on a button, pass data to a Java class, then open a new window with result?

Hi experts,

I have a JSP app, which has a form with several buttons. When user clicks on one of them, the page will not be closed or replaced, but opens a new page with result displayed. But, between the pages, the form should pass the user query to a Java class, like Servlet to process the request and send back the result to the result page.

My problem is I don't know how to click on the button without submitting the form, which would cause close the page, and pass the request to another class to process the request. thanks.
doradoraAsked:
Who is Participating?
 
rrzConnect With a Mentor Commented:
>I don't know how to click on the button without submitting the form  
You could use a JavaScript function  something like the following.
<script language="JavaScript">
                 function getResults(){
                  var w = window.open("ServletURL?a=1&b=2","PopUp","width=300,height=200");
                 }
</script>  
In the your form use
<input type="button" value="Show results" onclick="getResults()"/>  
0
 
KaraaCommented:
<td width="16"><input name="bValid" type="button" onClick="document.form1.action="left.jsp";"></td>                  
0
 
doradoraAuthor Commented:
thanks. but both answers are not what I want.

Please read my question one more time. I need to submit some data to a Java class, like a Servlet when user click on the button. then, the Servlet will proceee the result and pass it back to another new page.

Your answers only open a new page when user click on the button. My problem is I don't know how to pass the data to a Java class to process the data before opening a new window. I don't want to do the data process in the newly opened window because it will mix up the "view" and "controler" design pattern. Any experts?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
rrzCommented:
>My problem is I don't know how to pass the data to a Java class  
In my suggestion I appended a query string(?a=1&b=2) to the url  that  I used to request  the new window.  
>I don't want to do the data process in the newly opened window because it will mix up the "view" and "controler" design pattern.    
I don't understand.  
Can't you send the request for the new window to the controller ?    
0
 
doradoraAuthor Commented:
thanks.
by looking at your codes:

  var w = window.open("ServletURL?a=1&b=2","PopUp","width=300,height=200");
                 }

I thought you are trying to open a new window with the url, ServletURL. But, in my thought, the process should be:
1. pass the data to a Servlet when user click on the button(but not submit)
2. the Servlet process the data and return the reuslt to a new window


Do you mean your codes do all these? I think maybe I read your codes wrong...
0
 
doradoraAuthor Commented:
to karaa,

I test your codes which does not work. It sets the action to the new page, but it does not submit it. Am I on the right track? thanks.
0
 
rrzCommented:
>Do you mean your codes do all these? I think maybe I read your codes wrong...    
Yes, I think so.  
When the button is clicked, the javascript tells the browser to open a new window and make a request to the servlet with the parameters in the query string. The servlet receives the request and responds with data calculation results to the popup.
0
 
doradoraAuthor Commented:
thanks. So, what should I do inside the servlet? I mean how do I pass data from the servlet to this opened window since it does not have a name for the window?

0
 
rrzCommented:
I suggest that you try the code with a simple servlet first. You will see that clicking the button is the same as clicking on a link. The request is sent to servlet and its response is sent to the new window.    
>So, what should I do inside the servlet?    
You could do this.
public class Tester extends HttpServlet {  
        public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
                     {
                      String  adata = request.getParameter("a");
                      String  bdata = request.getParameter("b");  
                      PrintWriter out = response.getWriter();
                      out.println("a = " + adata + " , b = " + bdata);
       }
}
0
 
doradoraAuthor Commented:
thanks. It does work like you described.
However, I have some radio buttons which I also want to pass the user's choice to the Servlet. Could you give me advice how to pass the radio button input to this Servlet? Thanks a lot.

0
 
rrzCommented:
in your form
<input type="radio" name="mark" value="red" />
<input type="radio" name="mark" value="blue" />
in servlet
mark= request.getParameter("mark");
                if(mark.equals("red")){
                                       // do something
                }
0
 
doradoraAuthor Commented:
thanks. Actually, there is still a bug. The request.getParameter("mark") does not return anything.

My guess is because this click does not submit, but open a new window. Do you have any advice about this? thanks.
0
 
rrzCommented:
>The request.getParameter("mark") does not return anything.
Yes, mark is null if a checked radio is not present in submitted form  
>My guess is because this click does not submit, but open a new window  
I am not sure if I understand you.  
Do you want to add the state of the radio buttons to the query string that was sent to your servlet with your button  ?  
You can use javascript  to do that but I don't have the code.
something like   var markvalue = document.formname.radioname.value  and then in the query string   &mark=markvalue
But I would ask in the javascript area.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.