Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass a string from the jsp to the servlet 2

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,
as i'm new to jsp so my question is stupid but i need your point of view :

if i want to pass from a jsp a string to a servlet, and access it from a class, what can i do ?
i thought i could make a cookie, but i'm not sure that a cookie made by a jsp can be read by a servlet. isnt'it ?

is there other way ?
Best regards X-),
Elise
 
You could pass a parameter when you call a function in the servlet (which you will need to convert to a bean):
Code:
<jsp:useBean id='mybean' scope='application' class='mypackage.testbean'>

<% 
String arg1 = &quot;this is argument 1&quot;;
String arg2 = &quot;this is argument 2&quot;;
String r = mybean.myFunction(arg1, arg2); 
%>

Another method would be to use the <jsp:setProperty> action.

Setting a cookie in the JSP and reading it in the servlet is entirely possible but probably not the most efficient way unless the servlet and the JSP are totally independent. If this is the case, the following code will read our cookie set by something else:
Code:
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();

Cookie[] cookies = request.getCookies();
Cookie cookie;
for(int i=0; i<cookies.length; i++) {
  cookie = cookies[i];
  out.println(&quot;Name: &quot; + cookie.getName() + &quot;<br>Value: &quot; + cookie.getValue() + &quot;<p>&quot;);
}

Hope this helps,

Tim --
Tim <tim@planetedge.co.uk>
 
thanks Tim, i was working on the second method (as i can't use beans for the moment).

i have made this code in my servlet :
public void getCookie (HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
String value=&quot;&quot;;
Cookie[] cookies = request.getCookies();
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();
if (cookies != null) {
for (int i=0; i < cookies.length; i++) {
if (cookies.getName().equals(&quot;MyCookie&quot;)) {
value = cookies.getValue();
break;
}
}
}
out.println(&quot;<html><body>&quot; + value + &quot;</body></html>&quot;);

}


but i'm a bit lost for my jsp... i guess i should use a post method, but i don't know how.. what i begun to make is :
<html>
<body>
<%
Cookie info = null;
info = new Cookie (&quot;MyCookie&quot;, &quot;toto&quot;);
info.setPath(&quot;/&quot;);
info.setMaxAge(10*24*60*60);
response.addCookie(info);
%>

<form name = &quot;myForm&quot; ACTION=&quot;????&quot; METHOD=&quot;POST&quot;>
.... (submit button and form)
</body>
</html>

i think i don't use the correct method. in fact i don't know what it should do : create the cookie, and then ? or is there way to send them to the servlet ?

as you have noticed i put ACTION=&quot;????&quot; because i don't know what to call.. my method getCookie ? nothing ?

an other wuestion is that i have read i need to put those parameters : HttpServletRequest request,HttpServletResponse response but i don't even know why and what they are....

thanks for the time you took to answer me, i'm sorry for my coarse english.
Best regards X-),
Elise
 
okay i've found some answer by my own.

if i've well understood, to simply create a class (public class GetCookie extends HttpServlet{} for example )
and a method in it :
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
.....
}

put them in a servlet called GetCookie
and then in my jsp :
<form name = &quot;myForm&quot; ACTION=&quot;GetCookie&quot; METHOD=&quot;POST&quot;>

and by pressing the submit button this should launch the doPost method, isn't it ?


Best regards X-),
Elise
 
That is correct Elise. Also, if you want your servlet to support both GET and POST you should add this:

To do that simply add,
Code:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   doPost(request, response);
}

Now your servlet can be called from a FORM or from a URL like myhost.com/GetCookie?name=value&blah=blah

Tim --
Tim <tim@planetedge.co.uk>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top