需要将servlet输出到html文件中

时间:2014-09-18 09:29:40

标签: java jsp servlets

我有一个html文件,需要接受两个servlet参数,其余的是硬编码的,然后保存自己然后在Javamail中发送。

JSP获取参数:

    RequestDispatcher rd = getServletContext().getRequestDispatcher(
            "/email.jsp");
    rd.forward(request, response);

Your User Number is "<em><%=request.getParameter("USERNO")%></em>"
and your password is "<em><%= request.getParameter("PASSWORD")%></em>".

确认servlet在执行时确实成功提供了参数。当我调用如下的URL时,会给出jsp到html文件的输出:

本地主机/ MailServlet / HTML电子邮件USERNO = 1&安培; PASSWORD = TEST

我的想法是将jsp输出的字节保存到html文件中,然后将该html文件作为电子邮件发送。问题是当我尝试从url复制时,它变成了我调用servlet的无限循环。

复制字节代码:

        System.out.println("opening connection");
        URL url = new URL("http://localhost:8080/MailServlet/HTMLEmail?USERNO="+USERNO+"&PASSWORD="+PASSWORD");
        System.out.println("urlString created with URL="+url);
        InputStream in = url.openStream();
        System.out.println("InputStream opened");
        FileOutputStream fos = new FileOutputStream(new File("C:/Users/****/workspace/HtmlMailServlet/WebContent/email.html"));
        System.out.println("FileOutputStream opened");
        System.out.println("reading file...");
        int length = -1;
        byte[] buffer = new byte[1024];
        // buffer for portion of data from connection
        while ((length = in.read(buffer)) > -1) {
            fos.write(buffer, 0, length);
        }
        System.out.println("file read...");
        fos.close();
        in.close();
        System.out.println("file was downloaded");

目前我正在使用一个疯狂的3页字符串,其中传递了两个参数,这显然不好看但是完成了工作。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

javax.servlet.RequestDispatcher正在寻找: http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html