Results 1 to 5 of 5
Thread: object in session
- 03-09-2010, 05:30 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
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:
and in second file I have this: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"); }Can you give me an example please? thnxJava 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 } }
- 03-09-2010, 05:35 PM #2
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.
- 03-09-2010, 07:11 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
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:servlet for form process: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\" /> \n"; html += "<input type=\"submit\" name=\"submit\" value=\"Submit\" />\n"; html += "</form>\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).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); } } }
(Btw you must make test.war)
- 03-29-2010, 08:15 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
I want to know the servletConfig object is made per request or per application
- 03-29-2010, 01:07 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
session object
By faheem in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-04-2009, 03:14 AM -
session object
By 5alive in forum New To JavaReplies: 1Last Post: 06-23-2008, 12:45 AM -
problems with assigning a value to object session
By osval in forum New To JavaReplies: 1Last Post: 08-06-2007, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks