aspose file tools*
The moose likes JSP and the fly likes Call a Servlet from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Call a Servlet from JSP" Watch "Call a Servlet from JSP" New topic
Author

Call a Servlet from JSP

Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79


Hi,

I want to redirect to a Servlet from my jsp page? How do I do it. Can I use dispatcher.forward() to call servlet from JSP as used to call jsp from a servlet ??

Guide me.

Thanks


Regards,
Trupti (SCJP)
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

You can simply use forward action



Swastik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

That's backwards. Why would you want to do that?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Swastik Dey wrote:You can simply use forward action



Thanks. I have to sent a mail to the user on successful registeration. In my registeration.jsp page, when the user is registered successfully, I call the following lines to call the servlet:


Results - on the jsp page, I don't see any thing, status bar says "Done", else blank page. If I remove the jsp:forward, it shows me Refistered Succeessful msg of the jsp page only.

I tried calling as "<%=request.getContextPath()%>/MailServlet" in jspforward, then it shows error on 2nd param saying jsp:param cannot be outside include, forward or params. I tried adding <jsp:params> </jsp:params> to cover all jap:param, but yet same error.

Can help me out, where am I going wrong.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Are you doing this processing in a JSP? If so, that's where you are going wrong.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

As said in the above post its not good to do processing in jsp, however as far as your problem is concerned, you are probably missing the flush with output stream writer, thats why its probably not displaying the output generated by servlet.

out.println("<br> Username has has been sent to your email address. You may now login.");

your code is missing
out.flush();
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Bear Bibeault wrote:Are you doing this processing in a JSP? If so, that's where you are going wrong.


sending mail process is done is servlet only. Once registered successfully, then from my jsp page I am trying to call servlet to sent the mail.

I just tried the same stuff in the same jsp file only, and got error "sending mail Could not connect to SMTP host: localhost, port: 25 "

I guess this will give you an idea of where am I wrong.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

that means it was earlier generating the same exception in the servlet. Now coming to you problem you are connecting to localhost, so do you have a smtp server configured on your system?
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Swastik Dey wrote:that means it was earlier generating the same exception in the servlet. Now coming to you problem you are connecting to localhost, so do you have a smtp server configured on your system?


Yes, i gave out.flush and it showed me the exception via servlet also.
.....javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect

Then should I give my IP address or what ???
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

do you have any smtp server configured and running locally, i mean on your local system?
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Swastik Dey wrote:do you have any smtp server configured and running locally, i mean on your local system?


I am connected to internet.
I gave props.put("mail.smtp.host", smtpServerIP");

yet it is throwing - .....javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
Your smtp server is not configured or you are giving incorrect configuration details.

Smtp details can be obtained from your internet service provider.

Hope this helps.


Thanks,
Prabhat
SCJP 1.5, SCWCD 1.5, SCBCD 1.5
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Trupti Mehta wrote:
sending mail process is done is servlet only. Once registered successfully, then from my jsp page I am trying to call servlet to sent the mail.

The register process belongs in a Java class (a Servlet) as well.
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Prabhat Jha wrote:Your smtp server is not configured or you are giving incorrect configuration details.

Smtp details can be obtained from your internet service provider.

Hope this helps.


I inquired the details and the one I had added is correct. But I have to provide username and password to use it from an external application. How can I set the username & password of the smtp server in the application?
amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
Trupti Mehta wrote:
Swastik Dey wrote:You can simply use forward action



Thanks. I have to sent a mail to the user on successful registeration. In my registeration.jsp page, when the user is registered successfully, I call the following lines to call the servlet:


Results - on the jsp page, I don't see any thing, status bar says "Done", else blank page. If I remove the jsp:forward, it shows me Refistered Succeessful msg of the jsp page only.

I tried calling as "<%=request.getContextPath()%>/MailServlet" in jspforward, then it shows error on 2nd param saying jsp:param cannot be outside include, forward or params. I tried adding <jsp:params> </jsp:params> to cover all jap:param, but yet same error.

Can help me out, where am I going wrong.



please get a copy of Head First -Servlet And Jsp - By Cathy Sierra.
you will get the answers in first few pages.
Please make sure you clear your concepts before going to coding.
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
amit daundkar is correct... your design is not a correct one. make it more simple.. forget to pass to servlet. Atleast create a class for mailing and call the method in the class directly from jsp. atleast it will be easy to maintain..

<%
MailUtils mail = new MailUtils(username,password,smtpserver);
mail.send(content,to,cc,bcc, subject)
%>

Remove the servletness from your current mail servlet and make it as simple class and call it.

but before that buy Headfirst Servlet & Jsp book... even i just passed 146th page currenlty

"Many of lifes failure are people who did not realize how close they were to success when they gave up."
-Thomas Edison
~ar~stutzen~
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

Try doing telnet to smtp server.


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Priya dharshini
Ranch Hand

Joined: Aug 06, 2005
Posts: 78
Your SMTP server may not be configured. Try with SMTP server name which you knew working fine.


Thanks & Regards,
Priyadharshini . T
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call a Servlet from JSP