0

I'm a PHP coder but need to code some JSP...

I need help... What is the equivalent of this PHP code?

foreach($_POST as $key => $value){
   $$key = $value;
}

to jsp code...


further notes: the above codes is just a short cut of something like this,

$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];

and this sample 3 lines of codes in JSP is,

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

Thanks!

1
  • 1
    Not an answer to your question, but in PHP you can write that in a single call as: extract($_POST); (neither my solution nor yours is safe though...) Commented Jun 3, 2010 at 5:24

2 Answers 2

2

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/forEach.html

for the variable variable name thing - i dont think that is possible in JSP (you do know that JSP is just a better template engine?)

also ... you do know there is google right? :P

Sign up to request clarification or add additional context in comments.

3 Comments

yeah.. I know google... he's my friend... but he's not of help much right now... that's why I'm asking SO, my other friend... and I'm really way new to JSP.... sorry for the noob question...
last time i saw JSP was in 2001... and all i did is to create some html templates - means im not a big help but i think if you look at example code it cant be that hard to figure out a foreach. but im still kinda sure that the variable variables names is something jsp will not do. but im happy to be proven wrong :)
That's my thought too... well, it's going to be the two of us who will be happy if one will prove it... that JSP can do that too. ;)
2

This is untested and I'm quite new at JSP/Servlets, but it seems the Request object has a "getParameterNames" function that will return you the names of all parameters in the request.

If you enumerate through those you should be able to access the values with "getParameter".

http://java.sun.com/javaee/6/docs/api/javax/servlet/ServletRequest.html#getParameterNames()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.