2014-10-16 67 views
0

我有HttpServlet类调​​用,让我们称之为是Test。我可以使用HttpServletResponse对象输出此类中的任何内容到我的html页面。现在,我有另一个普通的java类,我们称它为Home,并且在这个类中我需要将某些东西放到html页面中。不幸的是,即使我试图从Home类继承HttpServlet并使用HttpServletResponse对象来输出,它仍然可以工作。如何从普通的Java类输出重定向到的HttpServlet

有没有一种方法来重定向从主类的输出测试类?

这里是我的doGetof测试类,它营造家的对象,并调用该方法connectToHom()进行身份验证方法。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String tst = "username" 
     tst = new Home(); 
     tst.connectToHome(tst); 

} 

这里的班级主页的方法connectToHome():

public void connectToHome(String tst){ 
    -> Try to login using tst, 
    connect = Connection.open(tst); 

    if(connect.getMessage()!=null){ 
    System.out.println("-- Error: " + connect.getMessage()); 
    return null; 
} 
} 

上面的代码的作品,但它打印出的控制台,而不是HTML页面上。

在此先感谢!

+0

请张贴一些代码。 – 2014-10-16 20:56:21

+0

嗨鲍里斯,我已经发布了代码。谢谢! – 2014-10-16 21:20:05

回答

2

您需要发送OutputStream参考。到Home类别:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String tst = "username" 
    tst = new Home(); 
    tst.connectToHome(tst, response.getOutputStream()); 
} 


public void connectToHome(String tst, OutputStream out){ 
    -> Try to login using tst, 
    connect = Connection.open(tst); 

    if(connect.getMessage()!=null){ 
    out.print("-- Error: " + connect.getMessage() + "\n"); 
    out.flush(); 
    return null; 
} 
} 
+0

嗨,你好!我会尝试一下并让你知道。谢谢! – 2014-10-17 01:10:02