Discussions

Web tier: servlets, JSP, Web frameworks: pass a string from the jsp to the servlet

  1. pass a string from the jsp to the servlet (2 messages)

    hi all, i'm new to jsp i want to pass from a jsp a string to a servlet with a cookie. i've seen examples in sun web site, so i made my class like they explained :
    public void getCookie (HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException
        {
        String value="";
        Cookie[] cookies = request.getCookies();
        response.setContentType("text/html");
           PrintWriter out = response.getWriter();
        if (cookies != null) {
            for (int i=0; i < cookies.length; i++) {
                if (cookies.getName().equals("MyCookie")) {
                value = cookies.getValue();
                break;
                }
            }
        }
        out.println("<html><body>" + value + "</body></html>");

    }


    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 ("MyCookie", "toto");
            info.setPath("/");
            info.setMaxAge(10*24*60*60);
            response.addCookie(info);
                    %>

                   <form name = "myForm" ACTION="????" METHOD="POST">
                     .... (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="????" because i don't know what to call.. my method getCookie ? nothing ?

    an other question 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....
    I must say also that for the moment i can't use BEANS, only servlets, so can i anyway use <jsp:include> action ?

    i'm sorry for my coarse english.

    Best regards , Elise
  2. pass a string from the jsp to the servlet[ Go to top ]

    okay i've found some begun of solutions.

    i made this :
    i do have a jsp file :

    myJSP.jsp
    ---------------------------
    <html>
    <BODY>
    <form name = "myForm" ACTION="GetCookie" METHOD="POST">
            <%
            Cookie info = null;
            info = new Cookie ("MyCookie", "toto");
            info.setPath("/");
            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="tata";
        Cookie[] cookies = request.getCookies();
        response.setContentType("text/html");
           PrintWriter out = response.getWriter();
        if (cookies != null) {
            for (int i=0; i < cookies.length; i++) {
                if (cookies.getName().equals("MyCookie")) {
                value = cookies.getValue();
                break;
                }
            }
        }
        out.println("results : " + value);

    }

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


    when i click on the submit button it launches an empty web page with the url : http://websphere/MyWebApp/GetCookie

    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 ,
    Elise
  3. pass a string from the jsp to the servlet[ Go to top ]

    It's been a while since I did my own Cookie logic, but do you have to set the host that the cookie should apply to. That's what tells the browser that it must pass this cookie for all requests. I think it's the setPath() you have wrong, but like I said it's been a while.

    It would explain why the servlet does nothing. If the browser isn't told to pass the cookie for your entire site, then it won't and you won't get the value.

    Chz

    Tony