servlet间通信

时间:2010-05-15 15:28:19

标签: java servlets

我有两个servlet:LoginServlet和MailServlet。 LoginServlet使用jdbc查询mysql表以获取字符串(eMail)。我想要的是将此字符串转发给MailServlet,MailServlet将发送一封电子邮件到LoginServlet发送的电子邮件ID。

我的问题是如何从LoginServlet调用并将变量eMail发送到MailServlet?我想创建一个MailServlet实例:

  

MailServlet servlet = new MailServlet();

然后使用servlet对象调用MailServlet中的函数doGet()。 但我觉得这有一些错误,因为这不是调用servlet的正确方法。那么如何调用并将变量传递给MailServlet?

3 个答案:

答案 0 :(得分:2)

servlet的目的是响应HTTP请求。你应该做的是重构你的代码,以便你想要的逻辑与另一个servlet分开,你可以独立地重用它。因此,例如,您可能最终得到一个Mailman类,以及一个使用Mailman来完成其工作的MailServlet。从另一个servlet调用servlet是没有意义的。

如果您需要在点击第一个页面后转到其他页面,请使用重定向:

http://www.java-tips.org/java-ee-tips/java-servlet/how-to-redirect-a-request-using-servlet.html

修改

例如,假设您有一个像以下的servlet:

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");
    }
}

相反,做这样的事情:

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);
    }
}

答案 1 :(得分:1)

我认为这可能是您最初寻找的:请求调度员。来自Sun示例文档:

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);
   }
}

这似乎为同一容器中的不同servlet,JSP或其他资源指定了一个新URL,以生成响应而不是当前的servlet。

从这里的教程: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPTags6.html

答案 2 :(得分:1)

您可以使用

的forward()方法
  

RequestDispatcher

所以代码如下:

<强> 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);
}

如果您不清楚这个答案,请告诉我。