Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forwarding Java request object to Perl script

Status
Not open for further replies.

DavidEmmett

Programmer
Aug 28, 2002
5
NL
Hi,

I have a JSP page which I want to do two things. The page takes user data entered in a form and writes it to a database. This already works. However, I then want to take this request, and forward the entire request on to an existing Perl script which processes and mails the data. Rewriting the Perl script in Java is not an option.

Can I use the RequestDispatcher.forward() method, using the RequestDispatcher object obtained using the request.getRequestDispatcher("my/perl/script.pl") method, or will the getRequestDispatcher not accept a path to a perl script?

Thanks in advance,

David
 
I found the answer to my own question.

I could not find a way of forwarding the request object, but I can create a HTTPUrlConnection object, and forward the request using that. Here's my code:

Code:
private int sendToMailer(HttpServletRequest request) {

	int result = 0;
	URL url = new URL("[URL unfurl="true"]http://www.mydomain.com/cgi-bin/mailer.pl");[/URL]
	HttpURLConnection connection = (HttpURLConnection )url.openConnection();

	connection.setDoOutput(true);
	connection.setRequestMethod("POST");
	PrintWriter connOut = new PrintWriter(connection.getOutputStream());

	Enumeration reqNames = request.getParameterNames();

	while (reqNames.hasMoreElements()) {
		String name = (String)reqNames.nextElement();
		String value = request.getParameter(name);
		connOut.print(name+"=" + value+"&");
	}
	result = connection.getResponseCode();
}
David
 
You may find it interesting that sending mail in java is as easy as:
Code:
         // Ask the user for the from, to, and subject lines
      String from    = "anyone@here.com";
      String to      = TGTM;

      // Establish a network connection for sending mail
      URL u = new URL("mailto:" + to);      // Create a mailto: URL 
      URLConnection c = u.openConnection(); // Create a URLConnection for it
      c.setDoInput(false);                  // Specify no input from this URL
      c.setDoOutput(true);                  // Specify we'll do output
      c.connect();                          // Connect to mail host
      PrintWriter mailOut =                 // Get output stream to mail host
        new PrintWriter(new OutputStreamWriter(c.getOutputStream()));

      // Write out mail headers.  Don't let users fake the From address
      mailOut.println(&quot;From: \&quot;&quot; + from + &quot;\&quot; <&quot; +
                  System.getProperty(&quot;user.name&quot;) + &quot;@&quot; + 
                  InetAddress.getLocalHost().getHostName() + &quot;>&quot;);
      mailOut.println(&quot;To: &quot; + to);
      mailOut.println(&quot;Subject: &quot; + subject);
      mailOut.println();  // blank line to end the list of headers

      mailOut.println(cBody);
 
      // Close the stream to terminate the message 
      mailOut.close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top