Passing a text field value to Java servlets

Alright couldn't find this anywhere, and I was wondering how to grab the values ​​of a text field from jsp or servlet and display it in another servlet.

Now my problem is not transmitting data and actually displaying it, my problem is that whenever the space is in the value, I can only get this first bit of information. For instance:

<form method="post" action="Phase1Servlet"> <p>Favorite Place:</p> <input type="text" name="place"></div> <input id="submit" type="submit" value="Submit"> </form> 

Say what the user enters in "The Mall"

in the servlets that I use:

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

Then print the place of the variable somewhere in my code, I get the word "The"

Do I need to use request.getParameterValues ​​("place"); instead? If so, how do I pass the values ​​from the servlet to the servlet through a hidden field? When I do this:

 String [] placeArr = request.getParameterValues("place"); out.println("<input type=\"hidden\" name=\"place\" value="+ placeArr +">"); 

The hidden field is stored [Ljava.lang.String; @ f61f5c

Does it need to be sorted out or converted somehow?

+4
source share
2 answers

Must be

 String placeArr = request.getParameterValue("place"); out.println("<input type=\"hidden\" name=\"place\" value=\""+ placeArr +"\">"); 

Hidden field line escape

+3
source

Are you sure that when using

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

does the place variable contain only the word before the first space? Because this is a rather strange situation. If you want to pass a parameter to another servlet (provided that another servlet is called from this servlet), you can set the request attribute in the first servlet and then send this request to another servlet, for example:

 request.setAttribute("place", "The mail"); RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( path_to_another_servlet ); dispatcher.forward( request, response ); 

and then in another servlet, ypu can use it like:

 String place = request.getAttribute("place"); 
+2
source

Source: https://habr.com/ru/post/1393217/


All Articles