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
363 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
Comment Utility
<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
Comment Utility
>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
Comment Utility
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
 
LVL 27

Expert Comment

by:rrz
Comment Utility
>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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Expert Comment

by:rrz
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to write service layer in Spring ? 4 273
reflection example 3 79
maven project jars 7 46
Suggestion on WebSite Template Sites 5 50
Marketing can be an uncomfortable undertaking, especially if your material is technology based. Luckily, we’ve compiled some simple and (relatively) painless tips to put an end to your trepidation and start your path to success.
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now