Question

I'm trying to write some code to handle the process of an HTTP callback in Java. I have very little knowledge of Java and was hopping you could lend me a hand or point me in the right way.

I want to call the script from a page that will listen for a POST from other machine with some parameters and their values. I then want the script to save them somewhere (a file or a database).

Any help would be really appreciated.

For further clarification, I want to create a servlet on a specific URL to handle a HTML post from another machine and receive all parameters and their values and insert them into a database for example.

Another edit, got to this code so far:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class CallbackServlet extends HttpServlet
{

public void doPost(HttpServletRequest req,HttpServletResponse res)throws 
IOException,ServletException
    {
        String instId=req.getParameterValues("instId")[0];
        String cartId=req.getParameterValues("cartId")[0];
        String desc=req.getParameterValues("desc")[0];
        String cost=req.getParameterValues("cost")[0];
        String amount=req.getParameterValues("amount")[0];
        String currency=req.getParameterValues("currency")[0];
        String name=req.getParameterValues("name")[0];
        String transId=req.getParameterValues("transId")[0];
        String transStatus=req.getParameterValues("transStatus")[0];
        String transTime=req.getParameterValues("transTime")[0];
        String cardType=req.getParameterValues("cardType")[0];
        Connection conn = null;
        Statement stmt = null;
        PrintWriter out=res.getWriter();
        try
        {

             conn = DriverManager.getConnection(
             "jdbc:mysql://localhost:3306/orders", "root", "root");
             stmt = conn.createStatement();

               int i=stmt.executeUpdate("insert into orderdetails values('"+transId+"','"+instId+"','"+cartId+"','"+desc+"'"+cost+"','"+amount+"','"+currency+"','"+name+"','"+transStatus+"','"+transTime+"','"+cardType+")");
               if(i>0)
                   out.println("Inserted Successfully");
               else
                   out.println("Insert Unsuccessful");
        }
        catch(SQLException ex)
        {
          ex.printStackTrace();       
        }
    }
}

I can't test it atm unfortunately. Could you guys take a look at it and point out any mistakes/improvements?

Cheers

Was it helpful?

Solution

Probably easiest way for this would be to use Servlet api with some Java application server (tomcat, jetty, ...).
Look at http://www3.ntu.edu.sg/home/ehchua/programming/java/javaservlets.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top