将参数传递给doGet()servlet

时间:2014-11-10 09:06:29

标签: java android mysql servlets

尝试通过Servlet从Android访问MySQL时遇到了一些问题。我想要做的是通过将一些值传递给servlet类来检查数据库中是否存在事件。如果没有现有记录,则执行DB插入。

    public void createEvent(Event event) {
    String page;
    JSONArray jsonArray;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(ENeighbourhoodActivity.URL + "checkEventExist");
        List<NameValuePair> checkExistnvp = new ArrayList<NameValuePair>(3);
        checkExistnvp.add(new BasicNameValuePair("eventName", event.getEventName()));
        checkExistnvp.add(new BasicNameValuePair("eventX", event.getEventX()));
        checkExistnvp.add(new BasicNameValuePair("eventY", event.getEventY()));

        try {
            post.setEntity(new UrlEncodedFormEntity(checkExistnvp));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        page = "{\'Events\':" + responseString + "}";
        try {
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray("Events");
            int length = jsonArray.length();
            if(length == 0){
                // If no existing record, then perform DB insertion
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

在我的servlet中:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    JSONArray jsonArray = new JSONArray();
    PrintWriter out = response.getWriter();
    if (request.getParameter("checkEventExist") != null) {
        String eventX = request.getParameter("eventX");
        String eventY = request.getParameter("eventY");
        String eventName = request.getParameter("eventName");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost/mydb", "root", "root");

            PreparedStatement statement = con
                    .prepareStatement("SELECT * FROM event WHERE eventName = '" + eventName + "' AND eventX = '" + eventX + "' AND eventY = '"+ eventY + "'");
            ResultSet result = statement.executeQuery();
            while (result.next()) {
                JSONObject eventInfo = new JSONObject();
                eventInfo.put("eventName", result.getString("eventName"));
                eventInfo.put("eventX", result.getString("eventX"));
                eventInfo.put("eventY", result.getString("eventY"));
                jsonArray.put(eventInfo);
            }
        }

        catch (JSONException je) {
            System.out.println(je.getMessage());
        } catch (Exception exc) {
            System.out.println(exc.getMessage());
        }
        out.println(jsonArray.toString());
    }

}

我不确定如何传递和获取servlet中的doGet()中的名称/值对。有了这条线:

post.setEntity(new UrlEncodedFormEntity(checkExistnvp));

我如何将值传递给doPost()。但我需要将它传递给doGet()。任何指南?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该使用查询参数,因为HTTP GET不允许在HTTP Body中发送实体。

为了发送HTTP GET的参数,您应该准备一个这样的URL:

HttpGet request = new HttpGet(ENeighbourhoodActivity.URL + "checkEventExist?" + 
    "eventName=<eventName>&"+
    "eventX=<eventX>&"+
    "eventY=<eventY>");
HttpResponse response = client.execute(request);

您不应该在请求中添加任何NameValuePair