<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 code
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);
}
}
I made a simple app in android from which I am sending three parameters to server.From android side it works fine and no error is shown.But when I try access it on local host on local:8888 it displays error HTTP ERROR: 503 Problem accessing /. Reason: Service Unavailable Powered by Jetty://
Aucun commentaire:
Enregistrer un commentaire