java.net.URLConnection在这里经常询问使用情况,Oracle教程对此非常简洁.
该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.
那么,我如何使用java.net.URLConnection触发和处理"高级"HTTP请求?
我希望能够在Java操作方法中访问JSON字符串中的属性.只需说出字符串即可myJsonString = object.getJson().下面是字符串的示例:
{
'title': 'ComputingandInformationsystems',
'id': 1,
'children': 'true',
'groups': [{
'title': 'LeveloneCIS',
'id': 2,
'children': 'true',
'groups': [{
'title': 'IntroToComputingandInternet',
'id': 3,
'children': 'false',
'groups': []
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
在此字符串中,每个JSON对象都包含其他JSON对象的数组.目的是提取ID列表,其中任何给定对象拥有包含其他JSON对象的组属性.我把Google的Gson视为潜在的JSON插件.任何人都可以提供某种形式的指导,告诉我如何从这个JSON字符串生成Java?
我刚刚开始使用Servlets并设法使用一些servlet作为单独的URL来填充数据库以进行一些虚拟测试.形式的东西:
public class Populate_ServletName extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
//Insert records
//Print confirmation
}
}
Run Code Online (Sandbox Code Playgroud)
我有大约6个这样的servlet,我想按顺序执行.我正在考虑使用setLocation来设置要重定向的下一页,但不确定这是否是正确的方法,因为重定向应该在插入记录之后发生.具体来说,我正在寻找这样的东西:
public class Populate_ALL extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
//Call Populate_1
//Call Populate_2
//Call Populate_3
//...
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?