• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Call a Servlet from JSP

 
Ranch Hand
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can simply use forward action

 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's backwards. Why would you want to do that?
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you doing this processing in a JSP? If so, that's where you are going wrong.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you have any smtp server configured and running locally, i mean on your local system?
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 79
Android Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Try doing telnet to smtp server.

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your SMTP server may not be configured. Try with SMTP server name which you knew working fine.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic