Transmit a label through a web address

Apologies for any language barriers as English is not my first language.

I am currently developing an application in JSP and encountering an issue with a field labeled "comments" in one of my forms. Upon form submission, the value of this field is sent to my servlet via an ajax request.

var request = 'mainServlet?command=SendRequest';
request += ('&comments=' + $('#comments').val());

The problem arises when the field contains characters like "<" or ">". $('#comments').val() converts them to "&lt" or "&gl", for example: becomes &lt ;test&gl ;

When trying to retrieve the value in my servlet using:

String comments = request.getParameter("comments");

The URL appears as follows:

mainServlet?command=SendRequest&comments=&lt ;test&gl ;

This results in request.getParameter("comments"); returning an empty string.

I considered manually replacing the string like &lt before passing it to the servlet, but I am wondering if there is an easier solution to this issue?

Thank you.

Edit: Additionally, the comments variable will be reused in another JSP.

Answer №1

If you're looking to transform a string into a URI-compatible format, the encodeURIComponent function is what you need.

Just keep in mind that you'll need to decode it on the other end. The URLDecoder class can handle this task for you.

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

Having trouble resolving the dependency injection for stripe-angular

Attempting to integrate the stripe-angular module into my Ionic/AngularJS application. https://github.com/gtramontina/stripe-angular I have installed the module using npm install stripe-angular. This is how I include the dependency in my app: var myApp ...

Transform a personalized Google map into a visual representation

I am facing a challenge with the following issue. I have implemented a custom overlay on a Google map and it works perfectly in the browser. However, my dilemma arises when I need to display an image with a marker for each point of interest on a different ...

Beginner CSS - tips for centering my navigation menu in the Ubershop-Child theme CSS file

Starting a new website with my child theme and currently facing alignment issues with the menu. You can check it out here I need help adding code to the style sheet to make sure the logo and menu items align properly next to each other. Any advice on how ...

Export data from a JSON object to a CSV file within the Osmosis function

I am currently utilizing Osmosis, a node.js tool, to extract data in the form of an array of JSON objects. The behavior of Osmosis functions indicates that the array is confined within the function's scope. Consequently, I must also write the file in ...

"Encountering an error while making a RestApi call with iron-ajax

While attempting to make a RestApi call from localhost, I encountered the following error message: XMLHttpRequest cannot load https://api-address/v1/registeruser. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origi ...

When you utilize the [Authorize] attribute in an ASP.NET MVC controller, it effectively prevents any Script code from executing

I have encountered an issue with my JavaScript code within script tags in my ASP.NET MVC project. Everything runs smoothly, but as soon as I include the Authorize keyword in my controller, the JavaScript stops working. Strangely, I have been unable to find ...

Updating the image source through ajax by retrieving the location from the database

Is there a way to dynamically change the image source using AJAX? I have the location saved in my database and I want to set the img src from the value of something like data[0]['patient_photo']. Below is the HTML code for the image: <img id= ...

"Utilizing Normalizr to standardize complex nested data structures

I am dealing with a complex nested data structure that looks like this: { components: [ guid: "cms-container/c154c79596b3af6326966b0c994e2a934", regions: [{ guid :"r1c154c79596b3af6326966b0c994e2a934", components: [{ guid: ...

Attempting to develop a worldwide npm package. Upon running my script, I encounter the message "The designated path cannot be found."

I have been working on converting my npm module into a global command. You can find the code at this link. However, when I try to run $ seed, an error stating "The system cannot find the path specified" is displayed, indicating that it recognizes it as a ...

Search for a string using the getDate method, then trigger a click event if there is a

Hello everyone, this is my first time posting but I've been silently observing for a while. Exploring selenium with javascript, mocha, and chai I am dealing with a date picker that has multiple buttons, divs, and spans holding the date numbers. I ne ...

Java: Creating a Method for Prioritized Reversible Operations

I am currently working on a project centered around a package delivery service. My goal is to establish a system where packages can be delivered and then returned if needed, essentially implementing operations for both issuance and reversal. To achieve thi ...

Eliminating a decimal point from the Document Object Model

I've got some HTML code that's being generated through PHP. It includes a number with two decimal places within a span element that has the class "amount": <span class="amount">200.00</span> My goal is to find a way to use a client- ...

Enhance the efficiency of your JavaScript code by minimizing repeated selectors

I've been working on a JavaScript project where I came across the following lines of code: $('#content').on('click', 'input[type=submit]', function(){ $('#content').on('click', 'a.removebutton&a ...

How come my reducer isn't changing the state of my store with ImmutableJS?

I have the following code within my reducer: const copy_states = fromJS(state); const i_copy_jobs = copy_states.get('calendar_jobs').get(s_from_day_key).get(s_dept_id).get(s_job_id); let i_calend ...

Leveraging v-tooltip with a personalized component that is not native

I have a unique custom component that is used throughout the application. However, in certain instances, I would like to display a tooltip when hovering over this component. According to the Vuetify documentation, this should work, but unfortunately it doe ...

Struggling to fetch a custom attribute from the HTML Option element, receiving [object Object] as the result instead

I've been facing a challenging issue all day. My task involves making an ajax call to a predefined JSON file and trying to save certain contents into option tags using custom attributes. However, every time I attempt to retrieve the data stored in the ...

What is the best way to effectively filter the HTMLDivElement card in Bootstrap?

I need assistance with creating a filtering function for application cards. The current function is not displaying the elements correctly, causing parts of the card, button, picture, or text to be missing. I have attempted to use "#myDIV *" in the .filter ...

What is the best way to ensure my fetchMovieDescription function is executed only after the story state has been updated?

I am facing a challenge with the fetchMovieDescription function. It is being called simultaneously with fetchBotReply instead of after my story state is updated. As a result, it generates a random image rather than using the one from the story result. impo ...

Error occurred in Build.Gradle file at line 22: method chandroid() could not be found

Just starting out in Android Studio and I've successfully completed my first simple app. However, I encountered a Gradle issue when attempting to open the project after transferring the app folder from my desktop to my laptop. (including screenshot li ...

The powerful combination of Nuxt, Vuex, and Computed Properties allows for

Currently exploring Nuxt.js with Vuex, I have created a basic form containing an email field, a password field, and a button. All my state, mutations, and actions are functioning correctly. However, I encountered an issue when trying to create a computed ...