JSP & Servlets
1.0.0
1.0.0
  • Setting up
  • Pre-requisites
  • Servlet Part 1
  • Servlet Part 2
  • Understanding the servlet
  • Servlet XML Configuration
  • POST method and passing parameters
  • Passing more parameters
  • Understanding GET and POST
  • Request, Session and Context Part -1
  • Request , Session and Context Part - 2
  • Understanding init, service and ServletConfig
  • Hello JSP
  • Understanding JSP
  • JSP Page directives
  • HttpServletRequest Path Decoding
  • Scopes in JSP and the PageContext Object
  • Understanding MVC pattern
  • PART -1 Writing an MVC app
  • JSTL
    • For-each
  • CRUD-1
  • Project Work
    • Create a Simple Java Web Application Using Servlet, JSP/JSTL and JDBC/Transaction
  • Locale Filter
  • Action Plan
    • Schedule
    • Hit webservice
Powered by GitBook
On this page

Was this helpful?

  1. Action Plan

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;
	}
PreviousSchedule

Last updated 6 years ago

Was this helpful?