Extracting data from cookies and saving it in the correct variable

A servlet sends three cookies to the client containing form entries for name, age, and surname with values submitted by the user.

Upon receiving the cookies back from the client, the server needs to store:

  • The value of the "name" cookie in a string variable named 'name'
  • The value of the "age" cookie in an integer variable named 'age'
  • The value of the "surname" cookie in a string variable named 'surname'

Although I have written the following code, it fails to compile without any clear error message:

Cookie cookies[] = request.getCookies();                                    

String name; int age; String surname;

    for (int i=0; i<cookies.length; i++) {

If (cookies[i].getName().equals("name"))

name = cookies[i].getValue();   

Else If (cookies[i].getName().equals("age"))

age = cookies[i].getValue();    

Else If (cookies[i].getName().equals("surname"))

    surname = cookies[i].getValue();                        

There are 3 errors present in this code snippet:

Error: Cannot find symbol. Variable 'getValue' is not recognized in Class Cookie.

Error: Cannot find symbol. Variable 'getValue' is not recognized in Class Cookie.

Error: Cannot find symbol. Variable 'getValue' is not recognized in Class Cookie.

Answer №1

The function getValue must be called with parentheses because it is a member function, not a variable.

surname = cookies[i].getValue();

Make sure to correct the order of 't' and 'h' in your loop when accessing cookies.length.

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

Steps to turn off popover functionality for a specific child element

Within a container, there are details along with a button. The container exhibits popover behavior upon hovering over it. However, the challenge lies in disabling the popover behavior while hovering specifically over the button within it. You can find th ...

I am interested in generating a Stacked Chart in Google Charts using data from a JSON file

Hey there, I'm looking to generate a stacked chart using JSON data in Google Charts. My current challenge revolves around the code snippet var data = google.visualization.arrayToDataTable([. <?php $tmp = array(); require "configdashboard.php"; /* ...

Retrieving JSON data from an API

As a beginner with Jquery JSON and API's, I am trying to work on hitting an API that returns city names in JSON format. I need to display these city names dynamically on my webpage in a list format. Can someone guide me through this process? If you w ...

Modifying the URL does not alter the selected tab

As part of my project, I wanted to ensure that when the page is refreshed, the previously selected tab remains active. To achieve this, I created a basic HTML page and added some jQuery. However, I encountered an issue when manually changing the URL from ...

What is the trick to incorporating nested tabs within tabs?

My tabs are functional and working smoothly. To view the jsfiddle, click here - http://jsfiddle.net/K3WVG/ I am looking for a way to add nested tabs within the existing tabs. I would prefer a CSS/HTML solution, but a JavaScript/jQuery method is also acce ...

Change the blue line to a crisp, clean white

Is it possible to customize the color of the blue line that appears when clicked? class Greetings extends React.Component { render() { return <div>Greetings {this.props.name}</div>; } } ReactDOM.render( <div> <p tabInde ...

Pass values from one page to another using jQuery and dynamically load them onto the current page. Implement a "Show More" button to

Apologies if there was any confusion caused. The jquery code provided below is intended to load a webpage within the current webpage, similar to when you click on "load more" or "show more results". The specific page I am attempting to load into the curren ...

Tips for implementing linear-gradient text in Android applications developed with React Native without using Expo

How can I implement linear-gradient text in react native mobile apps without using Expo? I want to apply a linear-gradient to change the text color. Is it possible to achieve this effect? If yes, please provide guidance on how to do it. If not, how do po ...

What is the best way to connect the imagemap shape with the checkbox?

I need assistance with synchronizing an imagemap collection of shapes and checkboxes. How can I ensure that clicking on a shape selects the corresponding checkbox, and vice versa? Imagemap: <div class="map_container"> <%= image_tag("maps/main ...

Navigate back to the parent directory in Node.js using the method fs.readFileSync

I'm restructuring the folder layout for my discord.js bot. To add more organization, I created a "src" folder to hold all js files. However, I'm facing an issue when trying to use readFileSync on a json file that is outside the src folder. Let&ap ...

Ways to conceal a div during the page loading process that is located in a separate PHP file

I am working with a PHP file that contains multiple PHP and JavaScript files being included. Within the AJAX .done(function(){ }) function, I am reloading my main page which includes all other files. The question is, how can I hide the div element inside a ...

Issues encountered when updating MySql Database from WordPress with Error 500, whereas the code functions properly when used outside of WordPress environment

Question: How can I update a MySQL database from within a JavaScript function on a WordPress WooCommerce page using Ajax? I have a working code snippet for updating the database, but when I integrate it into my WordPress page, I encounter a 500 error. To ...

What is the significance of the ngf prefix within the context of AngularJS and ng-file-upload?

I recently discovered the ngf-select directive within ng-file-upload for AngularJS. Here's how it's used: <button type="file" ngf-select="uploadFiles($file, $invalidFiles)" accept="image/*" ngf-max-height="1000" ngf-max-size="1MB"> ...

Issues with expected identifiers and type mismatch errors encountered when defining a TypeScript class

As someone who is still relatively new to coding, I am facing a challenge while trying to create a TypeScript class within an Angular project that is based on a complex JSON file. The issue arises from the way the properties were defined in the JSON using ...

What is the best way to reduce the file size of a group of PNG images for

For my game character, I have a collection of PNG files including stand.png, run1.png, run2.png. To display a series of actions on the JavaScript canvas, I need to load these images. However, the size of each .png file adds up quickly – for example, it ...

Adjust the CSS content using the data-content attribute to include line breaks

My current coding setup includes: <div id="mydiv" data-content="Loading..."></div> This is how I style it with CSS: #mydiv:after{ content: attr(data-content); height: 100%; opacity: 1; position: absolute; text-align: center; ...

Creating images or PDFs from HTML with CSS filters: a guide

Looking for someone who has experience creating images or PDFs from HTML code. The HTML contains images with CSS filters such as sepia and grayscale. If you have worked on this type of project before, I would love to hear about your experience. <img cl ...

What are the steps to limit entry to a page in Rails unless the user has come through a designated link on another page?

I'm currently developing an online computer-based test using Rails. Each question is accessed through a unique page URL in the format /questions/<id>. On each question page, there is a 'next' button that directs users to the follow ...

What is the process for obtaining the position integer of an item within a PHP array and transferring it from PHP to HTML?

I'm currently working on a function called getTopics() that should not only return the topic but also its position. For instance, when returning "Musical instruments," it should also provide the integer 0 for use in the getFaqs() function. Likewise, r ...

Find common connections on Facebook using an app PRIOR to downloading it

Is there a way to programmatically find mutual friends between myself and an application or page? For instance, in the scenario shown below: In the image above, it's evident that prior to installing the Causes App (1) I can observe that myself and t ...