Hit webservice

protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userName = request.getParameter("username");
		String pswd = request.getParameter("password");
		if (userName.equals("Admin") && pswd.equals("Admin")) {

			try {
				boolean flag = hitWebService(userName, pswd, request);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			request.getSession().setAttribute("username", userName + " welcome");
			request.setAttribute("user", "userTest");
			request.setAttribute("productList", Database.getProducts());
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("success.jsp");
			requestDispatcher.forward(request, response);
			response.getWriter().println("back to servlet"); // this wont b displayed
		} else {
			response.sendRedirect("Index.html");
		}
	}

	private boolean hitWebService(String userName, String pswd, HttpServletRequest request) throws JSONException {

		try {

			HttpClient httpClient = HttpClientBuilder.create().build();
			HttpPost postRequest = new HttpPost("http://localhost:8070/users/signin?username=client&password=client");

			// StringEntity input = new StringEntity("{\"username\":" + "client" +
			// ",\"password\":\"" + "client" + "\"}");
			// input.setContentType("application/json");
			// postRequest.setEntity(input);

			HttpResponse response = httpClient.execute(postRequest);

			// if (response.getStatusLine().getStatusCode() != 201) {
			// throw new RuntimeException("Failed : HTTP error code : " +
			// response.getStatusLine().getStatusCode());
			// }

			BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

			String output;
			System.out.println("Output from Server .... \n");
			output = br.readLine();

			request.setAttribute("userDetailsList", getallUsers(output));;

		} catch (MalformedURLException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

		return false;
	}

	private List<UserDetails> getallUsers(String output2) throws ClientProtocolException, IOException, JSONException {
		HttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet getReq = new HttpGet("http://localhost:8070/userdetails/users");
		String token = "Bearer " + output2;
		System.out.println(token);
		getReq.addHeader("Authorization", token);
		;

		// StringEntity input = new StringEntity("{\"username\":" + "client" +
		// ",\"password\":\"" + "client" + "\"}");
		// input.setContentType("application/json");
		// postRequest.setEntity(input);

		HttpResponse response = httpClient.execute(getReq);

		// if (response.getStatusLine().getStatusCode() != 201) {
		// throw new RuntimeException("Failed : HTTP error code : " +
		// response.getStatusLine().getStatusCode());
		// }

		BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

		String output;
		System.out.println("Output from Server .... \n");
		output = br.readLine();
			System.out.println(output);
			//output=output.substring(1, output.length()-1);
			System.out.println("aftr-- "+output);
			//JSONObject registerProducts = new JSONObject(output);
			JSONArray jsonArray = new JSONArray(output);
			List<UserDetails> userDetails = new ArrayList<>();
			
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);
				UserDetails details= new UserDetails(Long.parseLong(jsonObject.get("seq").toString()), jsonObject.get("name").toString(), Long.parseLong(jsonObject.get("mobileNum").toString()));
				userDetails.add(details);
			}
			return userDetails;
	}

Last updated

Was this helpful?