Barbara69
asked on
trying to use request.getParameter in a JSP
I'm trying to direct the submit button to one jsp if the parameter = ? or go to another jsp if it doesn't = ?. I'm trying to this using request.getParameter in a JSP to get a string variable from a servlet and I'm getting request undefined.
function page()
{
if (request.getParameter("str Page")=="R egisterNew Projects")
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
function page()
{
if (request.getParameter("str
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
This is just a shot in the dark...
Sorry, it should be...
if (<% out.println("\""+request.g etParamete r("strPage ")+"\"") %>=="RegisterNewProjects")
if (<% out.println("\""+request.g
ASKER
Got an error on the out of the code you submitted. I forgot to tell you I have the code in a script and I'm using
<FORM name="myform" onSubmit="return page();"> to call it
<SCRIPT language="JavaScript">
function page()
{
if (request.getParameter("str Page")=="R egisterNew Projects")
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>
<FORM name="myform" onSubmit="return page();"> to call it
<SCRIPT language="JavaScript">
function page()
{
if (request.getParameter("str
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>
hi Barbara69,
as venkateshwarr said, you are mixing jsp and javascript.
in your case must be like this:
========================== =
<FORM name="myform" onSubmit="return page();"> to call it
<SCRIPT language="JavaScript">
<% out.println("var strPage='" + request.getParameter("strP age"))+"'; " %>
function page()
{
if (strPage=="RegisterNewProj ects")
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>
as venkateshwarr said, you are mixing jsp and javascript.
in your case must be like this:
==========================
<FORM name="myform" onSubmit="return page();"> to call it
<SCRIPT language="JavaScript">
<% out.println("var strPage='" + request.getParameter("strP
function page()
{
if (strPage=="RegisterNewProj
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>
oops
must be <% out.println("var strPage='" + request.getParameter("strP age")+"';" ) %>
must be <% out.println("var strPage='" + request.getParameter("strP
ASKER
when i saved it i got this error:
JavaCompile: Syntax error on token "out", "++", "--"
JavaCompile: Syntax error on token "out", "++", "--"
did you test with my last correction?
ASKER
yes
i have never programmed in JSP, but this must be correct sytax:
========================== ========== ==
<SCRIPT language="JavaScript">
<%
out.println("var strPage=\"" + request.getParameter("strP age") + "\";");
%>
function page()
{
if (strPage=="RegisterNewProj ects")
{
document.myform.action = "/registerNewProjects.jsp" ;
}
else
{
document.myform.action = "/index.jsp";
}
return true;
}
</SCRIPT>
==========================
<SCRIPT language="JavaScript">
<%
out.println("var strPage=\"" + request.getParameter("strP
%>
function page()
{
if (strPage=="RegisterNewProj
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp";
}
return true;
}
</SCRIPT>
ASKER
i deleted the <% %> and it corrected the compile error but now i'm getting out is undefined when i hit the submit button
I think that there is still mixture of client-side and server side code going on here.
Perhaps something like this:
<form action="<%if (request.getParameter("str Page")=="R egisterNew Projects") {"/registe rNewProjec ts.jsp" }else{"/index.jsp"}%>" >
Fritz the Blank
Perhaps something like this:
<form action="<%if (request.getParameter("str
Fritz the Blank
My logic is as follows--
Setting the action like this: ~~ document.forms[0].action= ~~ is a client side procedure.
Capturing a value from the request collection is server side code.
So, the two can't work together like that. The code that I provided above is all server-side.
Fritz the Blank
Setting the action like this: ~~ document.forms[0].action= ~~ is a client side procedure.
Capturing a value from the request collection is server side code.
So, the two can't work together like that. The code that I provided above is all server-side.
Fritz the Blank
ok, i understand.
we have code that runs on the server and on the client.
all what you see inside <%....%> runs on the server
so for test make only
make a new page test.jsp and write only code that i posted above.
then call this page in browser with parameter --> http://localhost/test.jsp?strPage=RegisterNewProjects
then in Browser View Source, to see what do you send to the client.
must be this, as result:
<SCRIPT language="JavaScript">
var strPage="RegisterNewProjec ts";
function page()
{
if (strPage=="RegisterNewProj ects")
{
document.myform.action = "/registerNewProjects.jsp" ;
}
else
{
document.myform.action = "/index.jsp";
}
return true;
}
</SCRIPT>
===============
@ Fritz the Blank
look i send js variable from server
we have code that runs on the server and on the client.
all what you see inside <%....%> runs on the server
so for test make only
make a new page test.jsp and write only code that i posted above.
then call this page in browser with parameter --> http://localhost/test.jsp?strPage=RegisterNewProjects
then in Browser View Source, to see what do you send to the client.
must be this, as result:
<SCRIPT language="JavaScript">
var strPage="RegisterNewProjec
function page()
{
if (strPage=="RegisterNewProj
{
document.myform.action = "/registerNewProjects.jsp"
}
else
{
document.myform.action = "/index.jsp";
}
return true;
}
</SCRIPT>
===============
@ Fritz the Blank
look i send js variable from server
ASKER
How about trying something like this using a bean to capture the string from the servlet:
form action="if (<%=Page%>=="RegisterNewPr ojects"){" /BMAP/jsps /registerN ewProjects .jsp" }else{"/BMAP/jsps/index.js p"}" >
form action="if (<%=Page%>=="RegisterNewPr
Barbara--
We should try to think a little bit about the order of things. At what point in the process does the Java servlet pass the value to your page: while the page is being constructed on the server or after the page renders on the client's browser?
BTW, did my suggestion above work or throw a different sort of error?
Fritz the Blank
We should try to think a little bit about the order of things. At what point in the process does the Java servlet pass the value to your page: while the page is being constructed on the server or after the page renders on the client's browser?
BTW, did my suggestion above work or throw a different sort of error?
Fritz the Blank
no this is not correct.
if you want direct write the action must be as said Fritz the Blank
<form action="<%if (request.getParameter("str Page")=="R egisterNew Projects") {"/registe rNewProjec ts.jsp" }else{"/index.jsp"}%>" >
if you want direct write the action must be as said Fritz the Blank
<form action="<%if (request.getParameter("str
ASKER
value get passed to the jsp on request dispatcher from the servlet and wth this code i'm getting broken link and compile error
<form action="<%if (request.getParameter("str Page")=="R egisterNew Projects") {"/registe rNewProjec ts.jsp" }else{"/index.jsp"}%>" >
<form action="<%if (request.getParameter("str
Please excuse my ignorance, but I still don't follow.
At sompe point, the servlet will pass a value to your page. The question is does this happen server-side while the page is being interpreted at the server side or later after the page arrives on the client's browser.
Also, did you get errors with my code like this:
form action="<%if (request.getParameter("str Page")=="R egisterNew Projects") {"/registe rNewProjec ts.jsp" }else{"/index.jsp"}%>" >
Fritz the Blank
At sompe point, the servlet will pass a value to your page. The question is does this happen server-side while the page is being interpreted at the server side or later after the page arrives on the client's browser.
Also, did you get errors with my code like this:
form action="<%if (request.getParameter("str
Fritz the Blank
Barbara69, in your case i would do simple tests.
for example, does the code below work, if you open page with parameter http://localhost/test.jsp?strPage=RegisterNewProjects?
<hr>
<%
out.println(request.getPar ameter("st rPage"));
%>
<hr>
for example, does the code below work, if you open page with parameter http://localhost/test.jsp?strPage=RegisterNewProjects?
<hr>
<%
out.println(request.getPar
%>
<hr>
ASKER
i get error on out as not be defined
Okay, now I am completely confused. Are you getting that error with Devic's code or mine? Any more information about my question about the sequence of events?
FtB
FtB
ASKER
not with the open parameter, but when i use it from a link i get null
ASKER
i'm getting error with both
oops, if so, you must stop all and find why does not work simple out.println(request.getPar ameter("st rPage"));
i forgot to say, that the page where do you test this, must have ONLY this code.
i forgot to say, that the page where do you test this, must have ONLY this code.
If you get an error with my code, then there is something very wrong....
My guess is that this:
request.getParameter("strP age")
is not defined.
Fritz the Blank
My guess is that this:
request.getParameter("strP
is not defined.
Fritz the Blank
ASKER
i'm sorry i'm now completely confused.
ASKER
this is a snippet from the servlet
strPage = "RegisterNewProjects";
strResponsePage = "jsps/error.jsp";
RequestDispatcher reqDispatch = req.getRequestDispatcher(s trResponse Page);
reqDispatch.forward(req,re sp);
and what i'm trying to do is in the error.jsp
strPage = "RegisterNewProjects";
strResponsePage = "jsps/error.jsp";
RequestDispatcher reqDispatch = req.getRequestDispatcher(s
reqDispatch.forward(req,re
and what i'm trying to do is in the error.jsp
i would open a new Q in JSP area or point to this Q
ASKER
i don't understand what you're saying, i'm not experience in this taking a class and doing a project.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I guess you are mixing jsp and javascript...
if (<% out.println(request.getPar