Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1111753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:35:53+00:00 2026-05-17T02:35:53+00:00

I have a form in JSP. I have to populate it based on the

  • 0

I have a form in JSP. I have to populate it based on the request object (from the servlet). How do I use Java Script for accessing request object attributes or if you can suggest me any other better way to populate form dynamically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:35:54+00:00Added an answer on May 17, 2026 at 2:35 am

    You need to realize that Java/JSP is merely a HTML/CSS/JS code producer. So all you need to do is to just let JSP print the Java variable as if it is a JavaScript variable and that the generated HTML/JS code output is syntactically valid.

    Provided that the Java variable is available in the EL scope by ${foo}, here are several examples how to print it:

    <script>var foo = '${foo}';</script>
    
    <script>someFunction('${foo}');</script>
    
    <div onclick="someFunction('${foo}')">...</div>
    

    Imagine that the Java variable has the value "bar", then JSP will ultimately generate this HTML which you can verify by rightclick, View Source in the webbrowser:

    <script>var foo = 'bar';</script>
    
    <script>someFunction('bar');</script>
    
    <div onclick="someFunction('bar')">...</div>
    

    Do note that those singlequotes are thus mandatory in order to represent a string typed variable in JS. If you have used var foo = ${foo}; instead, then it would print var foo = bar;, which may end up in “bar is undefined” errors in when you attempt to access it further down in JS code (you can see JS errors in JS console of browser’s web developer toolset which you can open by pressing F12 in Chrome/FireFox23+/IE9+). Also note that if the variable represents a number or a boolean, which doesn’t need to be quoted, then it will just work fine.

    If the variable happens to originate from user-controlled input, then keep in mind to take into account XSS attack holes and JS escaping. Near the bottom of our EL wiki page you can find an example how to create a custom EL function which escapes a Java variable for safe usage in JS.

    If the variable is a bit more complex, e.g. a Java bean, or a list thereof, or a map, then you can use one of the many available JSON libraries to convert the Java object to a JSON string. Here’s an example assuming Gson.

    String someObjectAsJson = new Gson().toJson(someObject);
    

    Note that this way you don’t need to print it as a quoted string anymore.

    <script>var foo = ${someObjectAsJson};</script>
    

    See also:

    • Our JSP wiki page – see the chapter “JavaScript”.
    • How to escape JavaScript in JSP?
    • Call Servlet and invoke Java code from JavaScript along with parameters
    • How to use Servlets and Ajax?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can pre-populate my Stripes JSP form with an object, client in my case,
I have two objects: Form . A form that I populate from a JSP
I have my domain object, Client , I've got a form on my JSP
I have a JSP (AddDocument.jsp) with the following form: <form action= ${pageContext.request.contextPath}/project?to=submitAdd&idProject=${param.idProject} method=post name=form>
Hi: In my jsp page,I have a form to add/update a nested object using
I have created one jsp form which contains the username textfield. On the click
I work with a front-end developer who writes JSP files. We have a form
I have a JSP that is using Spring:form tags to bind controls to a
I have the following code: index.jsp: <form name=f action=db/ajoutConducteur.jsp method=post> <input type=text pattern=\d{8} required
I have one form in JSP. I have some input fields in that page,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.