In the process of developing a web application, I have incorporated options to add, modify, search, and remove actors. My specific requirement is to display messages like "actor added successfully" for adding an actor and "actor modified successfully" for modifying one. These messages are sent by their respective servlets to a common statusMessage.html file. Instead of having separate status message pages, I want to consolidate all messages in one place.
The servlet responsible for adding actors is AddActor.java
package com.flp.fms.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.flp.fms.service.ActorServiceImpl;
import com.flp.fms.service.IActorService;
public class AddNewActor extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
response.setContentType("text/html");
IActorService actorService=new ActorServiceImpl();
Map<String,String> actorDetails=new HashMap<String,String>();
actorDetails.put("firstName", request.getParameter("firstName"));
actorDetails.put("lastName", request.getParameter("lastName"));
if(actorService.AddActor(actorDetails))
{
response.sendRedirect(request.getContextPath() +"/htmlTemplates/statusMsg.html");
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
The content of my statusMsg.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Status</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="../script/angular.min.js"></script>
<script type="text/javascript" src="../script/angular-route.min.js">></script>
<script type="text/javascript" src="../script/appConfig.js"></script>
<script type="text/javascript" src="../script/appCtrl.js"></script>
</head>
<body ng-app="myApplication">
<img src='../static/bg.png' width="1280" height="222"/>
<a href="Welcome.html" class="show_buttton button1">Home</a>
<div ng-controller="status">
<h1 align=center>{{message}}</h1>
</div>
</body>
</html>
The controller function:
app.controller('status',function($scope,$http){
var url='http://localhost:8018/angularjs_webapp/statusMsg';
$http.get(url)
.success(function(response){
$scope.message=response;
});
});
The statusMsg.java servlet:
package com.flp.fms.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
public class statusMsg extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String message=(String) request.getAttribute("message");
PrintWriter out=response.getWriter();
response.setContentType("application/json");
Gson gson=new Gson();
String myGsonMsg=gson.toJson(message);
out.println(myGsonMsg);
}
}