0

每当我尝试从我的 servlet 中的 ajax 参数中读取数据时,我都会收到一个空指针异常。继承人的ajax代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*;" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script>
        function findNext(id,label){
            alert(id);
            alert(label);
            var xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState===4 && xmlhttp.status===200)
                    {
                        var ret=xmlhttp.responseText;
                        var prob=ret.substring(0,ret.indexOf("///"));
                        var pdesc=ret.substring(3);
                        document.getElementById("prob").innerHTML=prob;
                        document.getElementById("pdesc").innerHTML=pdesc;                            
                    }                        
            };
            xmlhttp.open("POST","Servlet1",true);
            var sendstr="pid="+id+"&labelid="+label;
            xmlhttp.send(sendstr);
        }
    </script>
</head>
<body>
    <%
        Connection con;
        Statement s;
        ResultSet rs;
        String sql,pid=null,prob=null,pdesc=null,yesL=null,noL=null;


        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/cdt", "root", ".hack%//sign66");
            s=con.createStatement();

            sql="select * from cdt.problem where pid='1.01'";
            rs=s.executeQuery(sql);
            rs.next();

            pid=rs.getObject("pid").toString();
            prob=rs.getObject("problm").toString();
            pdesc=rs.getObject("pdesc").toString();
            yesL=rs.getObject("yesL").toString();
            noL=rs.getObject("noL").toString();

        }
        catch(Exception e){
            e.printStackTrace();
        }
    %>

    <!--STYLE THESE 2 <P> STATEMENTS-->
    <p id="prob"><%=prob%></p>
    <p id="pdesc"><%=pdesc%></p>
    <form>
           <input type="button" value="Yes" onclick="findNext(<%=pid%>,<%=yesL%>);"/>
           <input type="button" value="No" onclick="findNext(<%=pid%>,<%=noL%>);"/>
    </form>
</body>
</html>

我传递给函数的变量可以通过警报很好地读取。

继承人的servlet代码:

package cdt1;

import cdt.FindNextLabelLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;

public class Servlet1 extends HttpServlet {

Connection con;
Statement s;
ResultSet rs;    
@EJB private FindNextLabelLocal obj;

@Override
public void init(ServletConfig config) throws ServletException{
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/cdt", "root", ".hack%//sign66");
        s=con.createStatement();

    }
    catch(Exception e){
        e.printStackTrace();
    }            
}

@SuppressWarnings("UnusedAssignment")
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        String pid,label,res;
        @SuppressWarnings("UnusedAssignment")
        float current,next;

        current=next = 0;
        pid=request.getParameter("pid").toString(); // pid of the question
        label=request.getParameter("labelid").toString(); //label of the next question or solution based on button press
        current=Float.parseFloat(pid);
        next=Float.parseFloat(label);

        res=obj.findNext(current,next).toString();
        out.write(res);

    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally {            
        out.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}
}

空异常发生在 cdt1.Servlet1.processRequest(Servlet1.java:66) 的 cdt1.Servlet1.doPost(Servlet1.java:124) 处的 java.lang.NullPointerException 第一个发生在我尝试读取 pid 值第二个时当 processRequest 被调用时。我在网上查找了很多资源,这些资源展示了如何使用 ajax 和 servlet,根据我读过的内容,我没有做错任何事情。如果有人能指出我正确的方向,那就太棒了。提前致谢。我正在使用带有 glassfish 3.1.2 的 netbeans 7.3,如果这会有所不同

4

1 回答 1

1

解决了。使用 post 发送数据时需要设置 content-type http header。在 put 请求期间也需要

于 2013-03-25T10:50:55.853 回答