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="../servlet/NextPage" method="post">
Username : <input type="text" name="username" size="30" maxlength="50">
<input type="submit" value="OK">
<input type="hidden" name="location" value="page2.jsp"></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("location"
) != null)
{
location = "../webtm/"+decode(path);
}
if (location == null)
{
res.sendError (res.SC_INTERNAL_SERVER_ERROR, "Destination not set for redirect; " + "please inform system admin"
; 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("username"
;
String mysession = request.getParamete("mysession"
;
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
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="../servlet/NextPage" method="post">
Username : <input type="text" name="username" size="30" maxlength="50">
<input type="submit" value="OK">
<input type="hidden" name="location" value="page2.jsp"></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("location"
{
location = "../webtm/"+decode(path);
}
if (location == null)
{
res.sendError (res.SC_INTERNAL_SERVER_ERROR, "Destination not set for redirect; " + "please inform system admin"
}
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("username"
String mysession = request.getParamete("mysession"
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