How can i assign test to str ??
No simply you cannot.
Java plays on server side and javascript on server side. A client doesn't execute the java code.
You can assign a java value to javascript variable with jsp or Expression language on server side.
To send a javascript value to java you have to make a server request, So the server process your string and send the results back to client.
Most probably you are looking for AJAX request.
Do not confuse that JSP and java script existed on same document(or file). Yes but JSP part compiles on server side and JavaScript executes by browser.
A clear cut example found here for a start.