Results 1 to 3 of 3
  1. #1
    Keepcase is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Imbedding java code in a jsp

    Hello!

    I am trying to put imbed some java code into a jsp (setting cookie information) using a jsp include; but it doesn't seem to work. I can do it directly in a jsp file but when I use the include in a jsp file it doesn't seem to work. (and the include page is correct as I use includes for everything else but I'm thinking it's either you can't reference java code or setting cookies can't be done in an include.)


    Does anyone have any ideas? or better ways to implement cookies in a jsp file on a website? I'm still very new to this, any help would be greatly appreciated!

    Thanks!

    Java Code:
    <jsp:include page="cookie.jsp" />

    The following is the contents of cookie.jsp
    Java Code:
    <%
    	Cookie cookie = new Cookie("billy","password");
    	response.addCookie(cookie);
    %>

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    for setting a cookie to work, because these end up in the http response headers, it is not possible to set a cookie if the jsp has already started generating output.

    in JSP pages this is sometimes not obvious when content is generated. when you have a bunch of <%@ page include=""%>.. tags each on their own line, sometimes the end of line of these tags cause whitespaces to be printed on the servlet response.

    for example,
    Java Code:
    <%@ page import="" %>  <<< --  oops, end of line white space
    <%@ include file="" %>    <<<--  oops end of line white space
    <% 
    // set a cookie, in code, or from an include
    Cookie cookie = ...  // will fail now
    %>
    I'm sure there is a way in the server's web.xml to turn of white space generated into the rendered jsp servlet output. but I usually ended up getting in the habbit of writing my tags all messed up looking like this: where the opening tag opens on the same line after a closing tag, so no whitespace gets printed out.

    Java Code:
    <%@ page import="" %><%@ 
    include file="" %><% 
    // set a cookie, in code, or from an include, might work now
    Cookie cookie = ...  // will fail now
    %>
    I guess you could also make the part that sets the cookie into a servlet filter so its always done before jsp processing.

  3. #3
    Keepcase is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the reply, I'll try your method as soon as I can.
    I did try deleting all the whitespace and then used the jsp include to point to the code but it still failed.

    I will try setting up the servlet as well.

    Thanks! I'll let you all know how it goes

Similar Threads

  1. Imbedding Applet on JSP with netbeans 6.9
    By aaroncarpet in forum Java Applets
    Replies: 0
    Last Post: 12-19-2010, 12:05 AM
  2. Java code
    By s_dawg101 in forum New To Java
    Replies: 6
    Last Post: 12-02-2009, 08:34 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Java Code
    By g.ganiraju in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •