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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how dow I pass the variable from a jsp to another jsp via a servlet?

Status
Not open for further replies.

VCNewbie

Programmer
Dec 5, 2001
8
PH
Hi!

How do I do redirection from a servlet to a jsp page while preserving the variables?

for example:

page1.jsp calls servlet then servlet calls page2.jsp

in page1:



<form action=&quot;../servlet/NextPage&quot; method=&quot;post&quot;>
Username : <input type=&quot;text&quot; name=&quot;username&quot; size=&quot;30&quot; maxlength=&quot;50&quot;>
<input type=&quot;submit&quot; value=&quot;OK&quot;>
<input type=&quot;hidden&quot; name=&quot;location&quot; value=&quot;page2.jsp&quot;></form>


In the servlet:



public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String location = null;
String path;
String query;

if ((path = req.getParameter(&quot;location&quot;)) != null)
{
location = &quot;../webtm/&quot;+decode(path);
}
if (location == null)
{
res.sendError (res.SC_INTERNAL_SERVER_ERROR, &quot;Destination not set for redirect; &quot; + &quot;please inform system admin&quot;); return;
}
res.sendRedirect(location);
}

private String decode(String encoded)
{
if (encoded.indexOf('%') == -1 )
return encoded;
StringBuffer holdstring = new StringBuffer(encoded.length());

char holdchar;
for (int count = 0; count < encoded.length(); count++) {
if (encoded.charAt(count) == '%')
{
holdstring.append((char)Integer.parseInt
(encoded.substring(count+1,count+3),16));

if (count + 2 >= encoded.length())
count = encoded.length();
else
count += 2;
} else {
holdstring.append(encoded.charAt (count));
}
}
return holdstring.toString();
}


and finally in page2:



// get the form variable from page1.jsp
String username = request.getParameter(&quot;username&quot;);
String mysession = request.getParamete(&quot;mysession&quot;);

problem is by the time it reaches page2, the username parameter is not available or something, so i get a null pointer exception. what am I missing here? thanks in advance!


Raymond





 
Like I said before the correct method is to use a RequestDispatcher and the forward() method. This is how things should be done. If you whatever reason this is not to your liking then you will need to create a Session and save those parameters in the Session to be retrieved by the second JSP page.

If you don't want the URL to reflect the servlet then why aren't you just directly submitting to the second JSP page? It is a waste save everything in the Session when there is an adequate and better method. Like I said everyone using MVC does it with a RequestDispatcher. Ultimately the client doesn't even need to know that they are using JSP pages or whatever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top