简体   繁体   中英

Passing parameter to doGet() servlet

I am having some problem when trying to access MySQL from Android via Servlet. What I am trying to do is check if the event exist in database by passing some value to servlet class. If no existing record, then perform DB insertion.

    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();
    }

}

And inside my 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());
    }

}

I not sure how should I pass and get name/value pairs into the doGet() in servlet. With this line:

post.setEntity(new UrlEncodedFormEntity(checkExistnvp));

It's how I pass value into the doPost(). But I need to pass it to doGet() instead. Any guides?

Thanks in advance.

You should use query parameters since HTTP GET does not allow sending entity in HTTP Body.

In order to send parameters for the HTTP GET, you should prepare a URL like this:

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

And you shouldn't add any NameValuePair to the request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM