JavaScript function used for iteration and its utilization in HTML pages

I'm in the process of developing an online computer store and need to create a login JSP for both admin and regular users. One challenge I'm facing is how to loop through my ArrayList of users and display a specific chunk of code if the user has the attribute isLoggedIn == true.

<a class="whiteOff" href="AdminLogin?logoff">Izloguj se (<%=k.getIme() %>)</a>
<a class="hide" href="register.jsp">Registracija</a>
<a class="hide" href="adminLogin.jsp">Logovanje za administratore</a>
<a class="hide" href="userLogin.jsp">Logovanje za korisnike</a><br/>

If the isLoggedIn == false, then another set of code should be displayed.

<a class="hide" href="AdminLogin?logoff">Izloguj se (<%=k.getIme() %>)</a>
<a class="white" href="register.jsp">Registracija</a>
<a class="white" href="adminLogin.jsp">Logovanje za administratore</a>
<a class="white" href="userLogin.jsp">Logovanje za korisnike</a><br/>

In my JSP file, I've implemented this logic using a loop over the list of users:

<%for(Korisnik k : Baza.getUser()){%>
                <% if(k.loggedIn){%>
                    <a class="whiteOff" href="AdminLogin?logoff">Izloguj se (<%=k.getIme() %>)</a>
                    <a class="hide" href="register.jsp">Registracija</a>
                    <a class="hide" href="adminLogin.jsp">Logovanje za administratore</a>
                    <a class="hide" href="userLogin.jsp">Logovanje za korisnike</a><br/>
                <% } else{%>
                    <a class="hide" href="AdminLogin?logoff">Izloguj se (<%=k.getIme() %>)</a>
                    <a class="white" href="register.jsp">Registracija</a>
                    <a class="white" href="adminLogin.jsp">Logovanje za administratore</a>
                    <a class="white" href="userLogin.jsp">Logovanje za korisnike</a><br/>
                <% } %>
            <% } %>

The issue arises when registering new users, causing duplicate links for each registered user. To address this, I'd like to display the specified code at the top only once when the loggedIn attribute is true for any user, avoiding repetition based on the number of registered users logged in.

Answer №1

The issue lies not in your iteration or HTML code, but rather in a design flaw. When a user requests your page, the JSP will generate links for all users present in the Baza.getUser() collection.

What you actually need is to determine which specific user is accessing the page and only render the HTML content intended for that user. You should maintain some form of tracking in your server-side logic to identify the requesting user. Once you have this information, you can implement something like

<% if (Baza.getCurrentUser().loggedIn) { %>
    <a class="whiteOff" href="AdminLogin?logoff">Logout (<%=k.getIme() %>)</a>
    <a class="hide" href="register.jsp">Registration</a>
    <a class="hide" href="adminLogin.jsp">Administrator Login</a>
    <a class="hide" href="userLogin.jsp">User Login</a><br/>
<% } else{%>
    <a class="hide" href="AdminLogin?logoff">Logout (<%=k.getIme() %>)</a>
    <a class="white" href="register.jsp">Registration</a>
    <a class="white" href="adminLogin.jsp">Administrator Login</a>
    <a class="white" href="userLogin.jsp">User Login</a><br/>
<% } %>

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

The impact of THREE.OrbitControls on the rotation of objects in the scene

Recently started diving into THREE.js and I've put together a simple scene with the basics: http://codepen.io/inspiral/full/Lewgj Overall, everything is running smoothly aside from an odd glitch that's been happening due to the new mouse event h ...

Filtering a table with a customized set of strings and their specific order using pure JavaScript

Recently, I've been diving into APIs and managed to create a table using pure vanilla javascript along with a long list of sorting commands that can filter the table based on strings. My goal is to establish an object containing strings in a specific ...

Troubleshooting Problem with OnItemSelectedListener and Multiple Spinners

In my activity, I have two spinners and I'm implementing OnItemSelectedListener to determine which item was selected using a switch statement. Currently, when the activity is created, a toast pops up displaying the default selected item. I only want t ...

Is there a way to attach an event listener to a span that was inserted using DTColumnBuilder?

I am attempting to include an onclick event for a span element that is generated by DTColumnBuilder. DTColumnBuilder .newColumn('Invoice') .withTitle('<span class="li-table-head ...

When navigating to a different tab, the v-if directive does not function correctly

I am in the process of developing a straightforward listing system that presents a list of items for various platforms, with each platform accessible through a separate tab. The logic for switching tabs has been implemented using VueJS from scratch. Overv ...

Click to refresh a different component in ReactJS

My goal is to create a unique media player that can reload when a user wants to listen to an MP3 file. The concept is as follows: In media-player.js: - Display title, artist, and album art In programs.js: there is a button (LISTEN) that renders in medi ...

Ordering ng-repeat in AngularJS using a separate arrayDiscover how to easily order your

Imagine I have an array containing keys in a specific order orderedItems=["apple","banana","orange]; and there is a JSON object that I want to display using ng-repeat but following the sequence specified in the array: {"fruits": { "apple":{ ...

Creating an illustration with HTML5 from an image

Is it possible to draw on an image by placing a canvas on top of it? I am familiar with drawing on a canvas, but I am facing an issue where clicking on the image does not trigger the canvas for drawing. How can I resolve this? This is how my HTML looks: ...

Issue with using the bootstrap template plugin for cube portfolio

I have integrated cubeportfolio.js into a bootstrap template, but I am encountering an issue with the custom .js code included in the template that is causing an error in the console. You can view the template I am using here, and it appears to be functio ...

Unable to retrieve an identifier from

Whenever I try to save an Entity into the MongoDB database, I encounter the following issue. The code snippet I am using involves Spring and CrudRepository This is how my code looks: UserDocument user = processUser(); userRepository.save(user); The err ...

Struggling to synchronize the newly updated Products List array in zustand?

Let me clarify the scenario I am dealing with so you can grasp it better. I have a Cart and various Products. When a user adds the product (product_id = 1) twice to the cart with the same options (red, xl), I increase the quantity of that item. However, i ...

The Volley library encountered a parsing error due to a JSONException: The input ended abruptly at character 0

I am currently working on developing an Android app and a Java Jax RS Restful web service. I have encountered an error when my app sends JSON data to the service. Interestingly, when I send JSON data from a Restful Client browser, the service successfully ...

Ways to avoid Parents Click Event from triggering during a Child's (Grandchild's) click event

My parent div has a unique feature where clicking on it reveals one of the child divs, and clicking again toggles the visibility of the child div. Inside this child div, there is a switch toggle that functions as a hidden checkbox. The issue I'm facin ...

Using async/await in an Express route handler

Currently, I am facing an issue with my node/express route. The route collects information through url parameters and then uses these parameters to trigger a separate function that takes some time to execute. Despite using async/await to pause the executio ...

The functionality of jQuery click events seems to be disabled on the webpage, however, it is working perfectly fine on jsFiddle

After thoroughly checking the js tags and ensuring everything is properly closed, it's frustrating when the JavaScript suddenly stops working. The code functions perfectly in JSFiddle, leading me to believe that the issue may lie with something I&apos ...

Experience the simplicity of running basic Javascript Scratch code within IntelliJ IDEA Ultimate

Is there a straightforward way to run and debug a basic JavaScript code in IntelliJ Idea Ultimate without the need for additional setup like creating an HTML file or npm project? I'm looking to avoid boilerplate tasks and wondering if there's an ...

Transcluding an element into an ng-repeat template in AngularJS: How can it be done?

Incorporating a carousel directive involves chunking the passed in array of items and mapping it into an array of arrays of elements. This structure then generates markup resembling the pseudo code provided below: <array of arrays> <array of i ...

What is the method for inspecting a WebSocket JSON message content?

I utilize a web application that transmits messages utilizing websocket with JSON payloads. The application sends the payload when specific buttons are clicked. Is there a method to observe and capture this payload? Additionally, could you recommend a J ...

Is it advisable to overlook errors and proceed with executing JavaScript in IE?

My webpage has a JIT Spacetree, but there are a few issues with it on Internet Explorer. If I open the developer tools and run through the errors, everything loads fine and looks great. Is there a way to make IE ignore these minor errors and just continue ...

The back button on the page does not reset the CSS styling

I have a button that activates navigation to a different page. When this button is clicked, it changes the current section/page's display settings to none and sets the new section/page's display to block. <div class="listLeft">&l ...