Solved

trying to use request.getParameter in a JSP

Posted on 2004-03-27
31
1,524 Views
Last Modified: 2012-08-13
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;
}
0
Comment
Question by:Barbara69
  • 12
  • 10
  • 6
  • +1
31 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10694712

I guess you are mixing jsp and javascript...

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

Expert Comment

by:venkateshwarr
ID: 10694715

This is just a shot in the dark...
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10694731
Sorry, it should be...

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

Author Comment

by:Barbara69
ID: 10694790
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>      
0
 
LVL 25

Expert Comment

by:devic
ID: 10694814
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>  
0
 
LVL 25

Expert Comment

by:devic
ID: 10694821
oops
must be <% out.println("var strPage='" + request.getParameter("strPage")+"';") %>
0
 

Author Comment

by:Barbara69
ID: 10694835
when i saved it i got this error:


JavaCompile: Syntax error on token "out", "++", "--"
0
 
LVL 25

Expert Comment

by:devic
ID: 10694842
did you test with my last correction?
0
 

Author Comment

by:Barbara69
ID: 10694848
yes
0
 
LVL 25

Expert Comment

by:devic
ID: 10694889
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>    
0
 

Author Comment

by:Barbara69
ID: 10694893
i deleted the <% %> and it corrected the compile error but now i'm getting out is undefined when i hit the submit button
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10694906
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

0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10694914
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
0
 
LVL 25

Expert Comment

by:devic
ID: 10694916
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
0
 

Author Comment

by:Barbara69
ID: 10694948
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"}" >
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10694960
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
0
 
LVL 25

Expert Comment

by:devic
ID: 10694964
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"}%>" >
0
 

Author Comment

by:Barbara69
ID: 10694989
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"}%>" >
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10695002
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
0
 
LVL 25

Expert Comment

by:devic
ID: 10695013
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>
0
 

Author Comment

by:Barbara69
ID: 10695015
i get error on out as not be defined
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10695026
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
0
 

Author Comment

by:Barbara69
ID: 10695033
not with the open parameter, but when i use it from a link i get null
0
 

Author Comment

by:Barbara69
ID: 10695038
i'm getting error with both
0
 
LVL 25

Expert Comment

by:devic
ID: 10695047
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.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10695082
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

0
 

Author Comment

by:Barbara69
ID: 10695095
i'm sorry i'm now completely confused.
0
 

Author Comment

by:Barbara69
ID: 10695117
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
0
 
LVL 25

Expert Comment

by:devic
ID: 10695122
i would open a new Q in JSP area or point to this Q
0
 

Author Comment

by:Barbara69
ID: 10695132
i don't understand what you're saying, i'm not experience in this taking a class and doing a project.
0
 
LVL 25

Accepted Solution

by:
devic earned 500 total points
ID: 10695145
i mean you need to ask experts from JSP area
http://oldlook.experts-exchange.com/Web/Web_Languages/JSP/

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now