This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSP and the fly likes Communicate between JSP and Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Communicate between JSP and Servlet" Watch "Communicate between JSP and Servlet" New topic
Author

Communicate between JSP and Servlet

Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Dear Expert,
I have 2 components, one is JSP (test.jsp), the other is Servlet.
The JSP act like the User Interface to accept 3 parameters from the user.
The servlet will perform calculation based on a SINGLE parameter sent from the JSP.
After the user input 3 parameters,
1. How to call the Servlet 3 times in the same time (since the serlvet only calcuate one parameter)?
2. Let the user remain in the test.jsp while the servlet perform the calculation ?
3. How jsp can get the calculation result from the servlet ?
Thanks
Vince


Vince Hon<br /> <br />SCJP 1.4 | SCWCD | SCBCD <br /><a href="http://vincehon.homeip.net:8000/VJW" target="_blank" rel="nofollow">http://vincehon.homeip.net:8000/VJW</a>
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
After the user input 3 parameters,
1. How to call the Servlet 3 times in the same time (since the serlvet only calcuate one parameter)?

hmm I don't think I understand your question. Why not call the servlet only Once, sending the 3 input parameters?

2. Let the user remain in the test.jsp while the servlet perform the calculation ?

If there are 3 input parameters, I believe the user will have to click on a "submit" button, your servlet will process the request and forward immediately to the same/different jsp. Still, i don't think I understand the whole concept of the question.

3. How jsp can get the calculation result from the servlet ?

After your servlet is done with the request, you can use a RequestDispatcher object to forward your request back to your jsp page. If you've calculated some values in your servlet that you need to display in your jsp, then you can store those values in the Request object, session object, servletcontext object. Have a look at them in the specification and then make your decision which one is the more appropriate to solve your problem.
hope this helps


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Thanks Andres.
3. How jsp can get the calculation result from the servlet ?
I got it.
for questin 1:
I just take an example. Is it possible to called 3 servlets from a jsp to handle the request at the same time ?
Actually, it is about a SMS application.
It consists of a JSP (send.jsp) and a Servlet (RouterServlet, to handle ONLY ONE received phone). If user enter multiple receive phone number, I will split them in the send.jsp and then call the RouterServlet.
e.g. If the user enter 3 received phone no. I will split them and then call the RouterServlet 3 times to handle the request.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
It still doesn't make sense to me Vince, sorry ....
e.g. If the user enter 3 received phone no. I will split them and then call the RouterServlet 3 times to handle the request.
what happens if the user enters ten "reveived" phone no. Would you want to call the servlet 10 times!?
how about: "if the user enter 3 received phone no. I will call RouterServlet 1 time to handle the request". the servlet will extract the 3 "received" phone no. and will do its processing with each.
Why do you want to split them?
[ November 09, 2003: Message edited by: Andres Gonzalez ]
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Andres,

Actually, I am still considering which one is better. Since I want to make the RouterServlet handle only one Route.
A. If I use this approach, I have to consider how to call multiple RouterServlet in the same time.
B. If I design the RouterServlet to handle the "split" function, the case is simplier, I just need to call the RouterServlet once.
By the way, if I choose the "B" approach:
Since the RouterServlet will return a flag that indicate whether it has received the request successfull or not. And I want the test.jsp to get this flag. Is it approapriate to use:
// in test.jsp
<jsp:include page="RouterServlet">
or
use:
<jsp:forward page="RouterServlet">
and then in RouterServlet.jsp, use RequestDispatcher to redirect back to test.jsp after it is executed ?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
You know what doubts you have because you understand the context and the problem you are having. We might help you here if you can explain what RouterServlet does and what your web application will be doing. You want to split things, but I personally don't understand what's the problem you want to solve. I just noticed it was a bit odd what you were trying to do, and this is the reason I asked you why you wanted to handle requests that way.
e.g. If the user enter 3 received phone no. I will split them and then call the RouterServlet 3 times to handle the request.
It's hard to know exactly what you want to achieve. We cannot give you any suggestions of which jsp action you should use if I still understand nothing.
maybe I'm the only one not following you
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Andres,
Thx for yr patient.
I would like to set up a web site that allow user to sent SMS.
For the front end
test.jsp
- ask the user to input parameter, like the sender phone no., receive phone no, the SMS message....
For the back end.
1. It has a RouterServlet
- to accept the parameters and find the best route.
- based on the best route, relay the SMS request to other component,
known as ESMEServlet (also a Servlet)
2. ESMEServlet
- the ESMEServlet will then sent the SMS to the SMS service center.
If I split the multiple phone on RouterServlet, let say, there are 3 phone numbers. RouterServlet will call ESMEServlet 3 times.
-------------------------------------------------------------------------
As you can see, there are 3 tiers.
test.jsp --> RouterServlet --> ESMEServlet
The difficulty is that I need to get the return flag on each tier.
For the test.jsp --> RouterServlet
I need to know whether RouterServlet can successfully accept the SMS request from test.jsp
Once I get the return flag from RouterServlet, I will used this to inform the user whether his/her SMS sending is successfully or not.
If success, I will debit his bonus point.
For the 2nd tier
RouterServlet --> ESMEServlet.
Same as test.jsp --> RouterServlet. RouterServlet will also need to get the return flag from ESMEServlet to indicate whether ESMEServlet is invoked successfully or not
My question is:
1.How can I get the return from RouterServlet and inform this to the user on the test.jsp page?
2. for the test.jsp --> RouterServlet.
Assume RouterServlet can return the flag after it is executed 50%. Is it possible to return the flag from RouterServlet to test.jsp and then let the RouterServlet to execute the remaining part (in this case, RouterServlet will invoke ESMEServlet) ? I want to do this because I do not want the user to wait for a relative long period of time.
thanks !
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
to accept the parameters and find the best route.
do you have to find the best route? how are you sending the SMS?
I asked this because I once developed a web app similar to yours. I had a form in which a user input a mobile number(s), the text message to send, and will click the submit button to send the SMS.
I did it this way (it might give you an idea) : I had a helper class that was in charge of only sending the SMS. My servlet will get the parameters from the form and it will fire up a thread, using my helper class. So, my helper class will run in the background sending the SMS to the destinations.
Once I start my thread from my servlet I forward the request immediately to a thank you page, displaying a message to the user saying that the SMS messages are being processed. I figured that if the network was down/slow, didn't want to hang my servlet (and my user) waiting for an immediate response.
if something went wrong with the SMS sending (network down, wrong number, etc), I caught the exception and used javamail to send an email to the user, describing the exception I retrieved and explaining that the system could not send the message.
The difficulty is that I need to get the return flag on each tier.
hmm.. if you find the way let me know ...
I want to do this because I do not want the user to wait for a relative long period of time.
exactly, it is the same as with a credit card processing. You never wait in a website like amazon/ebay until your credit card validation process is done. You always get a "thank you, your order is processing", and if you don't have enough funds/wrong cc number then you'll get an email explaining what the problem was. This is all about asynchronous communication, you do not want your user to wait for a long period of time.
hope this helps
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Ya, thanks for your suggestion, this really give me an idea to redesign the flow of my web application. (I have to finish the task in schedule, my boss is ).
In my case, ESMEServlet is like the "helper class" you mentioned.
One ESMEServlet is corresponding to one SMS Service Center.
i.e. If we are deal with 3 SMS Service Center, we will have 3 ESMEServlets.
And different ESMECenter will have different route.
In this stage, since we may have more than one SMS service center, we should find the best route for the sms (based on the lowest cost, time, and priority).
I think the difficulty in my case is that I "split" two component "RouterServlet" and "ESMEServlet" in 2 different servlets (they may be installed in different computer).
If I make the ESMEServlet as a "helper class" like your case, It will be better for me to implement.
If I want the 2 components to cooperate (in thread mode), will it possible to use Servlets ? (may be I "convert" the ESMEServlet to a helper classe and let it to cooperate with the RouterServlet using RMI ).
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
.e. If we are deal with 3 SMS Service Center, we will have 3 ESMEServlets.
And different ESMECenter will have different route.
...
...
If I want the 2 components to cooperate (in thread mode), will it possible to use Servlets ? (may be I "convert" the ESMEServlet to a helper classe and let it to cooperate with the RouterServlet using RMI ).

hmmm... you can certainly convert your ESMEServlet to a simple class, since you now that this class will only process SMS stuff. I think you can, you are only delegating some work to it.. (I'm also hoping you'd have a superclass and using subclasses of it (the only difference with these subclasses are the way they route the message )
[ November 10, 2003: Message edited by: Andres Gonzalez ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Communicate between JSP and Servlet
 
Similar Threads
How to deploy a simple Web App with no EJB
getting parameters from url
Attribute versus Parameter
Servlet - doGet - doPost - parameter
Page refresh after Action execute