0
function pay() // function to retrieve value from the servlet via AJAX

        {   

            var xmlHttp=new XMLHttpRequest();
            if (typeof XMLHttpRequest != "undefined")//object
            {
                xmlHttp= new XMLHttpRequest();
            }
            else if (window.ActiveXObject) //object
            {
                xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");//object
            }
            if (xmlHttp==null)
            {
                alert("Browser does not support XMLHTTP Request")
                return;
            } 
            var url="http://localhost:8084/ElectricityBillPaymentSystem/GenerateReceipt";

            url +="?username=" +document.getElementById("userinfo").value+"&bill="+document.getElementById("amnt").value;
            if(document.getElementById("mode1").checked)
            {

                url+="&type=Card";
            }
            else
            {
                url+="&type=NetBanking";
           }
           alert(url);
            xmlHttp.open("POST", url, true); // open connection to servlet
            xmlHttp.send(null);

            xmlHttp.onreadystatechange = function stateChange() // call if status 200
            {
                if (xmlHttp.readyState==4 && xmlHttp.status==200)
                {   alert(xmlHttp.responseText);
                    document.getElementById("bill").innerHTML=xmlHttp.responseText;
                    alert(xmlHttp.responseText);

                }
            }
            location.href="#payment"; //redirect
        }

public class GenerateReceipt extends HttpServlet { //Http servlet

/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
Statement stmt;
        Connection con;
        ResultSet rs;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DocumentException, ClassNotFoundException, SQLException 
{
            response.setContentType("text/html;charset=UTF-8");//Http servlet
            PrintWriter out=response.getWriter();//Http servlet


            try 
            {            
                Date dt=new Date();
                int mon=dt.getMonth()+1;String currentdate=dt.getYear()+1900+"-"+dt.getMonth()+"-"+dt.getDate();
        String fname=request.getParameter("username"); // parameters passing working..
        String amnt=request.getParameter("bill");
        String typ=request.getParameter("type");
        String transid="TRANS"+Integer.toString(mon);
        String serviceno;
        Class.forName("com.mysql.jdbc.Driver");

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        stmt=con.createStatement();
        rs=stmt.executeQuery("select * from userprofiles where FirstName='"+fname+"' ");
        rs.next(); 
        serviceno=rs.getString(8);
        rs.close();
        stmt.executeUpdate("update Payment set BillAmount=0 where ServiceNo in( Select ServiceNo from userprofiles where FirstName='"+fname+"' )");   // insert into tables
        stmt.executeUpdate("insert into Transaction values('"+serviceno+"', '"+currentdate+"', '"+transid+"', '"+typ+"'  )"); // insert into tables
        stmt.close();out.println("haa"); // not printed
        String buffer="<table border=1><tr><th>TransactionID</th><th>Bill Amount</th><th>Date of Transaction</th><th>Type of Transaction</th>";// insert into tables
        buffer+="</tr><tr><td>"+transid+"</td><td>"+amnt+"</td><td>"+currentdate+"</td><td>"+typ+"</td></tr></table>";// insert into tables
        out.println(buffer);// not printed in the responseTEXT
        out.println("haa");}} 

两者都与数据库连接完美

我没有通过 AJAX 从 Servlet 得到响应

4

2 回答 2

0

利用

 out.write(buffer);

代替

out.println(buffer)
于 2013-04-12T15:14:36.930 回答
0

使用 response.getWriter().append(buffer);

于 2018-05-11T20:41:33.907 回答