Interservice communication
I have two servlets: LoginServlet and MailServlet. LoginServlet queries mysql table using jdbc to get string (eMail). I want to redirect this string to the MailServlet, which in turn will send an email to this email id sent by the LoginServlet.
My question is, how can I call and send the eMail variable to the MailServlet from the LoginServlet? I thought about creating a MailServlet instance like:
MailServlet servlet = new MailServlet ();
And then use the servlet object to call the doGet () function in the MailServlet. But I feel like there is some mistake in this as this is the wrong way to call the servlet. So how do I call and pass the MailServlet variable?
a source to share
The purpose of a servlet is to respond to an HTTP request. What you need to do is refactor your code so that the logic you want is decoupled from the other servlet and you can reuse it yourself. So, for example, you can get the Mailman and MailServlet class that Mailman uses to do its job. It doesn't make sense to call a servlet from another servlet.
If you need to navigate to a different page after you click 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:
public class MailServlet extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
Message message =new MimeMessage(session1);
message.setFrom(new InternetAddress("someone@something.com"));
message.setRecipients(...);
message.doSomeOtherStuff();
Transport.send(message);
out.println("mail has been sent");
}
}
Instead, do the following:
public class MailServlet extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
new Mailer().sendMessage("someone@something.com", ...);
out.println("mail has been sent");
}
}
public class Mailer {
public void sendMessage(String from, ...) {
Message message =new MimeMessage(session1);
message.setFrom(new InternetAddress("someone@something.com"));
message.setRecipients(...);
message.doSomeOtherStuff();
Transport.send(message);
}
}
a source to share
I think this might be what you were originally looking for: a request dispatcher. From the Sun examples documentation:
public class Dispatcher extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("selectedScreen",
request.getServletPath());
RequestDispatcher dispatcher =
request.getRequestDispatcher("/template.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("selectedScreen",
request.getServletPath());
RequestDispatcher dispatcher =
request.getRequestDispatcher("/template.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
}
}
It seems to be specifying a new url for another servlet, JSP, or other resource in the same container to generate a response instead of the current servlet.
From the tutorial here: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPTags6.html
a source to share
You can use the forward () method for
So the code looks like this:
LoginServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String emailID = "abc@abc.com"; //Write code to retrieve email id from MySql and store in emailID variable
request.setAttribute("emaiID", emailID);
RequestDispatcher rd = request.getRequestDispatcher("MailServlet");
rd.forward(request, response);
}
MailServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String value = (String) request.getAttribute("emaiID");
pw.println("The value of email id is: " + value);
}
Let me know if this answer is not clear to you.
a source to share