Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-25
13
Medium Priority
?
375 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:doradora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 2

Expert Comment

by:Karaa
ID: 9619592
<td width="16"><input name="bValid" type="button" onClick="document.form1.action="left.jsp";"></td>                  
0
 
LVL 28

Accepted Solution

by:
rrz earned 240 total points
ID: 9619619
>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
 

Author Comment

by:doradora
ID: 9619769
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:rrz
ID: 9619982
>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
 

Author Comment

by:doradora
ID: 9620912
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
 

Author Comment

by:doradora
ID: 9621208
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
 
LVL 28

Expert Comment

by:rrz
ID: 9621273
>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
 

Author Comment

by:doradora
ID: 9621316
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
 
LVL 28

Expert Comment

by:rrz
ID: 9622492
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
 

Author Comment

by:doradora
ID: 9622545
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
 
LVL 28

Expert Comment

by:rrz
ID: 9622650
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
 

Author Comment

by:doradora
ID: 9622998
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
 
LVL 28

Expert Comment

by:rrz
ID: 9623342
>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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
What we learned in Webroot's webinar on multi-vector protection.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question