<servlet>
<servlet-name>Server_side_code</servlet-name>
<servlet-class>com.hmkcode.servlet.Server_side_codeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Server_side_code</servlet-name>
<url-pattern>/server_side_code</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
Servlet代码
public class Server_side_codeServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
// This will store all received articles
List<Person> persons = new LinkedList<Person>();
/***************************************************
* URL: /
* doPost(): receives JSON data, parse it, map it and send back as JSON
****************************************************/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// 1. get received JSON data from request
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
// 2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();
// 3. Convert received JSON to Article
Person person = mapper.readValue(json, Person.class);
// 4. Set response type to JSON
response.setContentType("application/json");
// 5. Add article to List<Article>
if(persons.size() > 20)
persons.remove(0);
persons.add(person);
// 6. Send List<Article> as JSON to client
mapper.writeValue(response.getOutputStream(), persons);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();
// 4. Set response type to JSON
resp.setContentType("application/json");
// 6. Send List<Article> as JSON to client
mapper.writeValue(resp.getOutputStream(), persons);
}
}
我在Android中创建了一个简单的应用程序,我将三个参数发送到服务器。从android端,它工作正常,没有错误。但是,当我尝试 在本地的localhost上访问它:8888它显示错误 HTTP错误:503 访问/的问题。原因: 暂停服务 由Jetty提供://
答案 0 :(得分:0)
这是因为您的应用程序可能已关闭或应用程序正在尝试命中未启动并运行的服务器。
见reference to 503 http status。检查以下
1. your server is up and running
2. your host mapping is right and points to right ip
3. your localhost server is configured to respond at the post specified.