gpt4 book ai didi

java - 将参数传递给 doGet() servlet

转载 作者:行者123 更新时间:2023-11-30 22:54:53 25 4
gpt4 key购买 nike

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

    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() 。有什么指南吗?

提前致谢。

最佳答案

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

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

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

而且你不应该在请求中添加任何NameValuePair

关于java - 将参数传递给 doGet() servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com