Link to home
Start Free TrialLog in
Avatar of Barbara69
Barbara69Flag for United States of America

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("strPage")=="RegisterNewProjects")

{            
      document.myform.action = "/registerNewProjects.jsp"      
}
else  
{      
      document.myform.action = "/index.jsp"
}
return true;
}
Avatar of venkateshwarr
venkateshwarr


I guess you are mixing jsp and javascript...

if (<% out.println(request.getParameter("strPage")) %>=="RegisterNewProjects")

This is just a shot in the dark...
Sorry, it should be...

if (<% out.println("\""+request.getParameter("strPage")+"\"") %>=="RegisterNewProjects")
Avatar of Barbara69

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("strPage")=="RegisterNewProjects")
{                  
      document.myform.action = "/registerNewProjects.jsp"      
}
else  
{      
      document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>      
Avatar of devic
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("strPage"))+"';" %>

function page()
{
       if (strPage=="RegisterNewProjects")
{              
     document.myform.action = "/registerNewProjects.jsp"    
}
else  
{    
     document.myform.action = "/index.jsp"
}
return true;
}
</SCRIPT>  
oops
must be <% out.println("var strPage='" + request.getParameter("strPage")+"';") %>
when i saved it i got this error:


JavaCompile: Syntax error on token "out", "++", "--"
did you test with my last correction?
yes
i have never programmed in JSP, but this must be correct sytax:

======================================
<SCRIPT language="JavaScript">
<%
out.println("var strPage=\"" + request.getParameter("strPage") + "\";");
%>

function page()
{
      if (strPage=="RegisterNewProjects")
      {              
           document.myform.action = "/registerNewProjects.jsp";  
      }
      else  
      {    
           document.myform.action = "/index.jsp";
      }
      return true;
}
</SCRIPT>    
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("strPage")=="RegisterNewProjects"){"/registerNewProjects.jsp"  }else{"/index.jsp"}%>" >

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
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="RegisterNewProjects";


function page()
{
      if (strPage=="RegisterNewProjects")
      {              
           document.myform.action = "/registerNewProjects.jsp";  
      }
      else  
      {    
           document.myform.action = "/index.jsp";
      }
      return true;
}
</SCRIPT>  

===============

@ Fritz the Blank
look i send js variable from server
How about trying something like this using a bean to capture the string from the servlet:

form action="if (<%=Page%>=="RegisterNewProjects"){"/BMAP/jsps/registerNewProjects.jsp"  }else{"/BMAP/jsps/index.jsp"}" >
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
no this is not correct.
if you want direct write the action must be as said Fritz the Blank

<form action="<%if (request.getParameter("strPage")=="RegisterNewProjects"){"/registerNewProjects.jsp"  }else{"/index.jsp"}%>" >
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("strPage")=="RegisterNewProjects"){"/registerNewProjects.jsp"  }else{"/index.jsp"}%>" >
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("strPage")=="RegisterNewProjects"){"/registerNewProjects.jsp"  }else{"/index.jsp"}%>" >

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.getParameter("strPage"));
%>
<hr>
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
not with the open parameter, but when i use it from a link i get null
i'm getting error with both
oops, if so, you must stop all and find why does not work simple out.println(request.getParameter("strPage"));

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("strPage")

is not defined.

Fritz the Blank

i'm sorry i'm now completely confused.
this is a snippet from the servlet

strPage = "RegisterNewProjects";
strResponsePage = "jsps/error.jsp";  

RequestDispatcher reqDispatch = req.getRequestDispatcher(strResponsePage);
reqDispatch.forward(req,resp);      

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
i don't understand what you're saying, i'm not experience in this taking a class and doing a project.
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial