Learn the art of using AngularJS to seamlessly update status messages on your webpage based on different outcomes

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

Answer №1

By utilizing the sendRedirect method within your AddActor.java servlet, any incoming requests to this service will be redirected to the statusMsg.java servlet.

You also have the option to use getRequestDispatcher(URL).forward(request, response) in your AddActor.java servlet, allowing you to forward the response as a new request to the statusMsg.java servlet.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When an input in Vue.js registers a @keyup event, it will return to the parent

I am encountering an issue with my input component where pressing enter redirects me to the parent component. Even adding a @keyup event does not solve the problem. Could this be happening because I am in a child component? How can I prevent the enter key ...

Storing user input in a variable - JavaScript Facebook Messenger bot

I am currently developing a facebook messenger bot using node.js. I am looking for a way to store the user response from a message that the bot sends, so that it can be accessed later by using a variable. // defines that 'text' is any message se ...

Game board in disarray due to an unexpected array exceeding its limits

In the code for a slightly more complex version of tic tac toe, there is a method that is causing an issue. Whenever the computer attempts to make a move, it triggers an array out of bounds -1 error. This is particularly perplexing because I have taken c ...

A4J support and Seam: Error in Syntax for Setting Operation

My usual approach to solving this issue involves using JavaScript, but I'm attempting to implement a4j in a Seam project with JSF. My goal is to create a simple input mask for dates using ajax4jsf, but I keep encountering the same error: Illegal Synta ...

Verify the front-end application and authenticate the backend REST API

My project involves developing a REST API and application logic on the client-side, keeping them separate and independent of each other. Now I am looking to implement an authentication system that will automatically authenticate users both on the client-si ...

Can Angular JS code be viewed on the browser when working with Mean Stack?

I came across a fascinating article on mean stack development recently. It mentioned how in node js, scripts are rendered on the server side and not visible in the browser. This got me thinking - "Is the script of Angular JS visible in the browser when u ...

Is it possible to integrate Express 4 standalone middleware into an Express 3 application?

While I am not yet prepared to upgrade my Express 3 installation to Express 4, I am interested in utilizing some of the enhanced components in Express 4, such as express-session and compression. I have experimented with implementing these upgrades and eve ...

Create an Angular-UI function that appends a modal inside a <div> element with the class

We are facing an issue with conflicting rule names between our legacy .css and Twitter Bootstrap on our large website. To resolve this conflict, we have implemented a .sass version of Bootstrap and structured everything as follows: .bootstrap-enabled { / ...

Tips for navigating the material ui Expanded attribute within the Expansion Panel

After looking at the image provided through this link: https://i.stack.imgur.com/kvELU.png I was faced with the task of making the expansion panel, specifically when it is active, take up 100% of its current Div space. While setting height: 100% did achi ...

Extract data from a JSON object and connect it to input fields on a form

I am currently working with a large JSON object that I need to parse and bind to various form fields. Here is a snippet of the code, but you can view the entire code here. The main question I have is what would be the most efficient way to achieve this? Wo ...

Automatically increase the dates in two cells once they have passed

Summary: Although I'm not a programmer, I've managed to incorporate some complex coding into a Google Sheets document for tracking my team's projects. This includes multiple-variable dropdown menus and integration with Google Calendar to mo ...

An error occurred while trying to import a module due to an unexpected token

Take a look at this codepen link I encountered an error (line 10 in index.vue) with the following import: import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js"; Any idea what could be causing this issue? All other ...

Is it possible to capture a screenshot and insert it into a Microsoft Word document through appium using Java programming?

I am currently utilizing the following method to capture screenshots and save them in a folder named 'Screenshots'. However, my objective is to automatically insert these screenshots into a word document based on the corresponding test cases. Is ...

Updating existing objects in json using the google-http-java-client

Currently, I am attempting to utilize the google-http-java-client library on an Android platform to parse JSON responses from my server. To achieve this, I have implemented the code provided in the project examples. private static final HttpTransport ...

What is the best way to generate multiple JPanels in an array and include JLabels within them?

After exploring numerous websites, I have encountered an issue with the labels when incorporating panels. The error that arises is: Exception in thread "main" java.lang.NullPointerException Any suggestions on how to resolve this problem? Here is the pro ...

Having trouble with the installation of nodemon globally on macOS Mojave?

When using the Visual Studio Code terminal, I ran the following command: npm install -g nodemon The output in the terminal showed: npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules npm ERR! code EACCES npm ERR! syscall access n ...

How can I toggle the visibility of a div based on whether a variable is defined or not?

How can I display a specific div on my webpage only when certain variables in PHP pull out a specific result from a database? I attempted to use the code snippet below, but it's not working as expected. Can someone provide guidance on how to achieve ...

What are the potential reasons for a "child-removed" event failing to trigger?

Let's look at a simplified version of the code: var queueTask = function (taskObject) { var deferred = $q.defer(); // Creating a new task reference & pushing a new Task object to that location var newTaskRef = firebase.database().ref ...

What is the best way to retrieve and utilize JSON data from an AJAX request in a Django view?

I have been attempting to send dynamically generated JSON data from my template using an ajax call to the view. I am successful in creating and passing the JSON data through the ajax call, but unfortunately, I am encountering difficulties reading the same ...

What steps can be taken to resolve the delay in transition when clicking on "read less

I implemented a transition effect for the Read More and Read Less buttons, however, there seems to be a delay on the transition when clicking on the Read Less button. This delay was not intentionally set by me. How can I resolve this issue and also apply a ...