Can I pass session data or ID from servlet to JSP without using a forward?
I have one servlet that makes one or more calls to the same JSP with slightly different params (on the same server) and then concatinates the results. Thus, I don't think I can use a RequestDispatcher.forward(). The servlet makes a URL request to each requested report JSP (http://localhost/.../X.jsp?param=...) and passes parameters via GET parameters. I now have a requirement to pass a lot more data to the JSP that has already been computed and stored in the user session. Instead of having the JSP recompute all of this, can each call to the JSP get at the same session data of the servlet that called it?
I see that HttpServletRequest has a getRequestedSessionId() method, but I don't see how I can get the JSP to use that session ID instead of it's own, which would have no data since the servlet is what made the connection to the JSP. Also, HttpSessionContext.getSession(sessionId) is deprecated for security reasons.
Is my only option to serialize the needed data in the session and then make a POST and then recreate the object? This seems inefficient since both servlet and JSP are on the same machine under the same web server (Tomcat 6)!
Thanks for any help on this!
James
>Servlet and JSP are in the same application.
In this case, store your data in the user session:
Servlet:
HttpServletRequest request ...
HttpSesion session = request.getSession(true);
session.setAttribute("AttributeName", AtributeObject);
JSP
<%
HttpSesion session = request.getSession(false);
Object obj = session.getAttribute("AttributeName");
...
%>
If you want to send a PDF, so create it first in your servlet using iText (http://www.lowagie.com/iText/) for example, then send it.
Here a sample pdf sender servlet:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UrlServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get the 'file' parameter
String fileName = (String) request.getParameter("file");
if (fileName == null || fileName.equals(""))
throw new ServletException(
"Invalid or non-existent file parameter in UrlServlet servlet.");
if (fileName.indexOf(".pdf") == -1)
fileName = fileName + ".pdf";
URL pdfDir = null;
URLConnection urlConn = null;
ServletOutputStream stream = null;
BufferedInputStream buf = null;
try {
pdfDir = new URL(getServletContext().getInitParameter(
"remote-pdf-dir")
+ fileName);
} catch (MalformedURLException mue) {
throw new ServletException(mue.getMessage());
}
try {
stream = response.getOutputStream();
//set response headers
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName);
urlConn = pdfDir.openConnection();
response.setContentLength((int) urlConn.getContentLength());
buf = new BufferedInputStream(urlConn.getInputStream());
int readBytes = 0;
//read from the file; write to the ServletOutputStream
while ((readBytes = buf.read()) != -1)
stream.write(readBytes);
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} finally {
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
See also this: http://itextdocs.lowagie.com/tutorial/general/webapp/index.html
Hope That Helps