简体   繁体   中英

Send a bunch of values from jsp to java code

I have a form on my jsp with fields - Name, IP, username, token. It is mapped to a table with same column names. I have created a Bean class too.

Now, I am not sure what is an ideal way to send all the parameters from jsp to the java code.

I know few options like -

  1. Create input with type hidden and then set its value in the jsp.
    <input type="hidden" id="name" name="name"/> and then retrieve its value in the code using request.getParameter("name")

    Somehow I dont feel this is an ideal way.

  2. Create a JSON with all the values of the input boxes and set the json file as one input and read it on the java code using org.json;

Which one of the two is the better way to do it? Is there any simpler, effective and much better way?

Everything which is built using HTTP methods must send values to the server in one of three places:

  1. Parameters in the url itself ( GET requests)
  2. Content in the body of the request ( POST requests)
  3. Key-value pairs in the header of the request (HTTP method agnostic)

Any mechanism you use to send a value back to the server (excluding web sockets), no matter what the framework, will use one of these mechanisms underneath.

That being said, use whatever best meets the requirements of your application:

  1. Form-based, GET requests:
    • are simple to understand
    • don't require much overhead either on the front-end or back-end
    • are easy to test (just access the url using the appropriate query string)
  2. Form-based, POST requests:
    • are also simple to understand
    • also don't require much overhead either on the front-end or back-end
    • are not as easy to test (you can't just access the url using the appropriate query string)
  3. Ajax-y, JSON body, POST requests:
    • are the new hotness
    • require a bit more front-end work (creating the request in JS and sending it)
    • don't require the browser to make a full-page request/response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM