0

I have a html file which needs to take two servlet parameters the rest is hardcoded and then save itself to then send within a Javamail.

JSP getting parameters:

    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>".

Just to confirm the servlet does give the parameters successfully when executed. The output of the jsp into the html file is given when I call a url like:

localhost/MailServlet/HTMLEmail?USERNO=1&PASSWORD=TEST

My idea was to save the bytes of the jsp output into a html file and then send that html file as an email. The thing is that when I try to copy from the url it becomes an infinite loop of where I cam calling the servlet.

Copying bytes code:

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

Currently I am using a crazy 3 page string with two parameters passed inside which is obviously not good to look at but does the job. Any help is appreciated.

1 Answer 1

1

javax.servlet.RequestDispatcher is what you are looking for: http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe this answer will help you: stackoverflow.com/questions/13721238/…
Thanks been looking ages for a solution to this saved me a hell of a lot of code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.