Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Link to forward session info instead of RequestDispatcher

Status
Not open for further replies.

dreampolice

Technical User
Dec 20, 2006
85
US
I have objects that I pass from a Servlet to JSP using RequestDispatcher type. Now, I was wondering if I can do the same with a link but not sure how I do that:

Servlet called MyServlet.java (or MyServlet in web.xml mapping):
Code:
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String city= request.getParameter("city");
String state= request.getParameter("state");
.....
HttpSession session = request.getSession();
session.setAttribute("firstname", firstname);
session.setAttribute("lastname", lastname);
session.setAttribute("city", city);
session.setAttribute("state", state);
RequestDispatcher dispatcher = getServletContex ().getRequestDispatcher("start.jsp");
dispatcher.forward(request, response);
...


The above servlet automatically goes to the JSP and shows the firstname,lastname, city and state values that were submitted from a form. What is the equivalent of the RequestDispatcher where I can put a link instead so the user can click on the link and that will take them to the JSP with the session values:

This is the part I cant figure out:
Code:
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String city= request.getParameter("city");
String state= request.getParameter("state");
.....
HttpSession session = request.getSession();
session.setAttribute("firstname", firstname);
session.setAttribute("lastname", lastname);
session.setAttribute("city", city);
session.setAttribute("state", state);
...
response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML>\n" +
                "<HEAD><TITLE>Hello [URL unfurl="true"]WWW</TITLE></HEAD>\n"[/URL] +
                "<BODY>\n" +
                "<p><a href=start.jsp and RequestDispatcher info here??>Link to JSP</a></p>\n" +
                "</BODY></HTML>");
.....


Please advise.
 
I'm not entirely sure, but this is what I would try. Have the link reference your servlet, and add an additional parameter for the jsp. So you'd end up with something like

Code:
String jsp = request.getParameter("jsp");
if (jsp == null || "".equalsIgnoreCase(jsp){ 
response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML>\n" +
                "<HEAD><TITLE>Hello [URL unfurl="true"]WWW</TITLE></HEAD>\n"[/URL] +
                "<BODY>\n" +
                "<p><a href="MyServlet?jsp=start.jsp">Link to JSP</a></p>\n" +
                "</BODY></HTML>");
}
else {
RequestDispatcher dispatcher = getServletContex ().getRequestDispatcher(jsp);
}

Let me know if that works.
 
Thanks, I tried that and it gave me a null exception pointer. Any other ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top