• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call a Servlet from JSP

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • 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
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply use forward action

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's backwards. Why would you want to do that?
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing this processing in a JSP? If so, that's where you are going wrong.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • 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 Java ME
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • 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 Java ME
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • 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 Java ME
  • Mark post as helpful
  • send pies
  • 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
 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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 Java ME
  • Mark post as helpful
  • send pies
  • 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?
 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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.
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try doing telnet to smtp server.

 
Priya dharshini
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your SMTP server may not be configured. Try with SMTP server name which you knew working fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic