October 17, 2018

Angular JS and Servlet Sample web application



index.jsp

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<button ng-click="getdata();"> Click </button>
<h2>NSTYLE APPOINMENT DETAILS</h2>

<table style="width:100%" ng-if="records.length > 0">
  <tr>
    <th>Client Name</th>
    <th>Date</th>
    <th>Shop</th>
     <th>Service Name</th>
      <th>Mobile Nuber</th>
  </tr>


  <tr ng-repeat="record in records" >
    <td>{{record.clientName}}</td>
    <td>{{record.appointmentDate}}</td>
    <td>{{record.shopName}}</td>
    <td>{{record.serviceName}}</td>
    <td>{{record.mobileNumber}}</td>
  </tr>


</table>



<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http) {
  $scope.records = [];
  $scope.getdata=function(){

    $http({
        method : "POST",
        url : "http://localhost:8080/NStyleSample/ApplicationServlets"
    }).then(function mySuccess(response) {
    $scope.records = response.data;
    }, function myError(response) {
      //  $scope.myWelcome = response.statusText;
    });

  }

});
</script>

</body>
</html>


Servlets

package com.nstyle.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.nstyle.models.AppointmentModels;
import com.nstyle.utils.AppointmentUtils;

/**
 * Servlet implementation class ApplicationServlets
 */
public class ApplicationServlets extends HttpServlet {
private static final long serialVersionUID = 1L;


private AppointmentUtils appointmentUtils = null;
/**
* @see HttpServlet#HttpServlet()
*/
public ApplicationServlets() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println(" INSIDE SERVLET ");
appointmentUtils = new AppointmentUtils();
List<AppointmentModels> appointments = appointmentUtils.getValue();
ObjectMapper mapper = new ObjectMapper();
String respMess = mapper.writeValueAsString(appointments);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(respMess);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}


Utils classes

package com.nstyle.utils;

import java.util.ArrayList;
import java.util.List;

import com.nstyle.models.AppointmentModels;

public class AppointmentUtils {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}
public List<AppointmentModels> getValue(){
List<AppointmentModels> appointmentList = null;
appointmentList = new ArrayList<AppointmentModels>();
for(int i =1; i<=5;i++){
AppointmentModels appointment = new AppointmentModels();
appointment.setClientName("Client "+i);
appointment.setAppointmentDate("2018-10-02");
appointment.setMobileNumber("050197390"+i);
appointment.setServiceName("Service "+i);
appointment.setShopName("Shop "+i);
appointmentList.add(appointment);
}
return appointmentList;
}

}

Web.xml ( Deployment descriptor)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>NStyleSample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ApplicationServlets</display-name>
    <servlet-name>ApplicationServlets</servlet-name>
    <servlet-class>com.nstyle.servlets.ApplicationServlets</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ApplicationServlets</servlet-name>
    <url-pattern>/ApplicationServlets</url-pattern>
  </servlet-mapping>
</web-app>

github link

https://github.com/nijeshcfed/NStyleSample

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments