Go Premium for a chance to win a PS4. Enter to Win

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
?
380 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month5 days, 20 hours left to enroll

772 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