-
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: 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: 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
-
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.
-
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: 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);
}
}
servlet for form process: 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)
-
I want to know the servletConfig object is made per request or per application
-
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 :-)