V
V
Viktoria Smirnova2019-10-08 08:12:43
Java
Viktoria Smirnova, 2019-10-08 08:12:43

How to extract the value of request.getparameter?

Hello everyone, although the question is lamer, but I can not find the information.
On the server, I receive a string string
String p_scnt = request.getParameter("p_scnt");
:

User [userid=1, firstName=Ira, lastName=Ivanova, dob=null, [email protected]].

Question: to write to the database, I only need the value userid, how to select it.
It is advisable to do this on the server side, on the client a text field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bestie, 2019-10-10
@bestie

Ideally, it is better to send data at least in JSON, then you can easily deserialize the message into a specific object on the server side. And from it already pull the data you need.
And so, the simplest solution in the forehead:

String request = "User [userid=1, firstName=Ira, lastName=Ivanova, dob=null, [email protected]]";
int beginOfString = request.indexOf("=") + 1;
int endOfString = request.indexOf(",");
String id = request.substring(beginOfString, endOfString);
System.out.println(id);

which will stop working as soon as the order of the fields in the line changes.
You can play it safe and look for the occurrence of "userid=" in the line:
String request = "User [userid=1, firstName=Ira, lastName=Ivanova, dob=null, [email protected]]";
String userId = "userid=";
int beginOfString = request.indexOf(userId) + userId.length();
int endOfString = request.indexOf(",", beginOfString);
String id = request.substring(beginOfString, endOfString);
System.out.println(id);

But the solution is the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question