Results 1 to 5 of 5
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default object in session

    Hi everybody, I have one elementary question. Can I save object into session and take it out in other servlet page? (specifically HttpServletRequest object) In one servlet file (file that works with the original request) I have following code:
    Java Code:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        
        session.setAttribute("request", request);
        response.sendRedirect("/another-page");
    }
    and in second file I have this:
    Java Code:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        if (!session.isNew() && (HttpServletRequest)session.getAttribute("request") != null) {
            HttpServletRequest originalRequest;
            originalRequest = (HttpServletRequest)session.getAttribute("request");
    
            String itemOfOriginalRequest = originalRequest.getParameter("itemOfOriginalRequest");
            // do something with itemOfOriginalRequest
        }
    }
    Can you give me an example please? thnx

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default

    Yes, the session is shared by all the objects in the Web application context. Look at the session object API, it should have getters and setters.

  3. #3
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    I'm little bit desperate... I've read manual of Interface HttpSession Interface HttpSession, tried to follow it and it is not functional. Here is my code. You can run it :-(.
    servlet for index page:
    Java Code:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class IndexPage extends HttpServlet {
        public final String DOCTYPE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\t\t" + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n";
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) 
               throws ServletException, IOException {
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
    
            HttpSession session = request.getSession(true);
    
            String html = DOCTYPE + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
            html += "<head>\n";
            html += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
            html += "<meta http-equiv=\"content-Style-Type\" content=\"text/css\" />\n";
            html += "<meta http-equiv=\"Content-Language\" content=\"en\" />\n";
            html += "</head>\n";
            
            html += "<body>\n";
    
            if (!session.isNew() && session.getAttribute("originalRequest") != null) {
                HttpServletRequest originalRequest;
                originalRequest = (HttpServletRequest)session.getAttribute("originalRequest");
    
                String item = originalRequest.getParameter("item");
                if (item != null && !item.isEmpty()) {
                    html += "<div>" + item + "</div>\n";
    
                    session.removeAttribute("originalRequest");
                }
            }
    
            html += "<form action=\"formProcess\" method=\"post\">\n";
                html += "<input type=\"text\" name=\"item\" />&nbsp;\n";
                html += "<input type=\"submit\" name=\"submit\" value=\"Submit\" />\n";
            html += "</form>\n";
    
            html += "</body>\n";
            html += "</html>";
    
            out.println(html);
        }
    }
    servlet for form process:
    Java Code:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class IndexPage extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response) 
               throws ServletException, IOException {
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
    
            HttpSession session = request.getSession(true);
    
            String item = request.getParameter("item");
            if (item.isEmpty()) {
                session.setAttribute("originalRequest", request);
                response.sendRedirect("/test");
            } else {
                String html = DOCTYPE + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
                html += "<head>\n";
                html += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
                html += "<meta http-equiv=\"content-Style-Type\" content=\"text/css\" />\n";
                html += "<meta http-equiv=\"Content-Language\" content=\"en\" />\n";
                html += "</head>\n";
            
                html += "<body>\n";
                    html += "<div>" + item + "</div>\n";
                html += "</body>\n";
                html += "</html>";
    
                out.println(html);
            }
        }
    }
    I hope, there are no errors (I copied it by rewritting, because I simplified my original code).
    (Btw you must make test.war)

  4. #4
    Paras Dhyani is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    I want to know the servletConfig object is made per request or per application

  5. #5
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Er... I don't know, servletConfig object is in method init as parameter, isn't it? I don't understand your question, I'm sorry :-[... but I solve my problem in different way. I save object HashMap<String, String> in session and everything is ok :-)

Similar Threads

  1. session object
    By faheem in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-04-2009, 03:14 AM
  2. session object
    By 5alive in forum New To Java
    Replies: 1
    Last Post: 06-23-2008, 12:45 AM
  3. Replies: 1
    Last Post: 08-06-2007, 11:10 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
  •