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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

know where the bug is ? (jsp + servlet)

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
0
0
FR
hi all,
i do have a jsp file :

myJSP.jsp
---------------------------
<html>
<BODY>
<form name = &quot;myForm&quot; ACTION=&quot;GetCookie&quot; METHOD=&quot;POST&quot;>
<%
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);
%>
<INPUT name=valid type=submit value=Search>
</body></html>

and a servlet

GetCookie.java
---------------------------------
protected void doGet (javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws ServletException, IOException {

String value=&quot;tata&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;results : &quot; + value);

}

-------------------------


when i click on the submit button it launches an empty web page with the url :
any idea og why it's emty ?

Best regards X-),
Elise
 
Couple of things...

1) You don't need to include the scriptlet between the FORM tags. Doing this is confusing. Save the bit in the FORM tag for <input> or other form elements used to pass variables to your servlet i.e. <input type=&quot;hidden&quot; name=&quot;myvar&quot; value=&quot;test&quot;> and use getParameter() in your servlet to retrieve them.

2) Try changing the method=&quot;POST&quot; to method=&quot;GET&quot; seen as your servlet only supports doGet.

Otherwise it looks as though it should work...

Tim --
Tim <tim@planetedge.co.uk>
 

okay thanks, but with this method i won't have no more cookie and won't need a servlet.

the request.getParameter(&quot;varName&quot;) is for the jsp file isn't it ? in fact for the moment i write back the variable in the web page, but it's just for a first test, because after this work, i will have to get parameters from the jsp to update a database from a servlet. that's why i use the cookie, i haven't found an other solution.

i made a doGet and doPost method, but none seem to work.
the servlet doesn't seem to use them.

any idea ?



Best regards X-),
Elise
 
Does that servlet code even work?

You have an array of &quot;cookies&quot; and you loop thru them... but you don't specify what index to retrieve.

[tt]
Code:
protected void doGet (javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws ServletException, IOException {

    String value=&quot;tata&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++) {
            // try this not &quot;cookies.getName().equals(&quot;MyCookie&quot;)
            if (cookies[i].getName().equals(&quot;MyCookie&quot;)) {
            value = cookies.getValue();
            break;
            }
        }
    }
    out.println(&quot;results : &quot; + value);

}
[/tt]


Let me know if this works... Later.
I hope this helped! ;-)
- Casey Winans
 
oh i'm sorry it's just an error of cut and paste.

my code is :
protected void doGet (javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();
String value=&quot;tata&quot;;
Cookie[] cookies = request.getCookies();

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;);
out.println(&quot;results : &quot; + value + &quot;</body><html>&quot;);
out.close();
} Best regards X-),
Elise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top