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
372 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 27

Accepted Solution

by:
rrz earned 60 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 27

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 27

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 27

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 27

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 27

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stateless EJB with glassfish 2 75
servlet cookie finding by name 1 96
maven project jars 7 61
login form jsp example 2 71
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
A hard and fast method for reducing Active Directory Administrators members.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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