I have two servlets: LoginServlet and MailServlet. LoginServlet queries a mysql table using jdbc to get a string(eMail). What I want is to forward this string to MailServlet which in turn will send an email to that e-mail ID sent by LoginServlet.
My question is how do I call and send the variable eMail to MailServlet, from LoginServlet? I thought of creating an instance of the MailServlet as :
MailServlet servlet = new MailServlet();
And then use the servlet object to call the function doGet() in MailServlet.
But I am feeling that there is some error in this as this is not the right way to call a servlet. So how do I call and pass a variable to MailServlet?
The purpose of a servlet is to respond to an HTTP request. What you should do is refactor your code so that the logic you want is separated from the other servlet and you can reuse it independently. So, for example, you might end up with a Mailman class, and a MailServlet that uses Mailman to do its work. It doesn’t make sense to call a servlet from another servlet.
If what you need is to go to a different page after you hit the first one, use a redirect:
http://www.java-tips.org/java-ee-tips/java-servlet/how-to-redirect-a-request-using-servlet.html
Edit:
For example, suppose you have a servlet like:
Instead, do something like this: