Author
Link to a JSP on click of a button
Ria Dev
Greenhorn
Joined: Aug 18, 2010
Posts: 6
posted Aug 11, 2011 12:07:27
0
Please tell me, how to link to a jsp on click of a button?
I tried to use the below code, which isnt working,
.jsp
<input type ="button" name="edit" value="Edit Details" onClick="fwdToUpdatePage();">
.js
function fwdToUpdatePage(){
document.location.href(update.jsp);
}
Also let me know if there is any other method to do this.
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
You have a question about that Javascript? Then let's move it to the forum which is about Javascript.
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
document.location.href(update.jsp);
This is not even close to being valid syntax. Open the script console and it will show you syntax errors.
[Smart Questions ] [JSP FAQ ] [Books by Bear ] [Bear's FrontMan ] [About Bear ]
Sharon Melissa
Greenhorn
Joined: Aug 12, 2011
Posts: 5
JSP is handled on the server side. So unless you want to "forward" the page before the user gets the page, you would need Client side scripting like JavaScript.
The JavaScript code would be something like:
Code:
<input type="button" value="Add" onClick="javascript:window.location='some.jsp';">
If you wanted the JSP to specify the page to "forward", you could have something like this;
Code:
<%
String myPage = "some.jsp";
%>
<input type="button" value="Add" onClick="javascript:window.location='<%= myPage %>';">
or something along those lines.
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
Sharon Melissa wrote:
<input type="button" value="Add" onClick="javascript:window.location='some.jsp';">
Why do people put the "javascript:" prefix there? Omit it. It's not necessary and just gums up the code.
<%
String myPage = "some.jsp";
%>
<input type="button" value="Add" onClick="javascript:window.location='<%= myPage %>';">
And modern JSP should have no Java code in it. Use JSTL and EL. Using Java scriptlets in a JSP in 2011 is simply irresponsible.
subject: Link to a JSP on click of a button