简体   繁体   中英

redirect from one jsp to another jsp

I have a question. I have to jsp files (index.jsp and neuesSpiel.jsp). When I click on a button in index.jsp I want to redirect to neuesSpiel.jsp

Here is some Code (index.jsp):

Button:

<input type="image" src="images/neuesSpiel.png" name="neuesSpiel"   height=50%  id="neuesSpiel" alt="neuesSpiel">

onclick function

$("#neuesSpiel").click(function() {
<%response.sendRedirect("neuesSpiel.jsp");%>});

But when I open up the index.jsp I will be automatically redirected to neuesSpiel.jsp. I only want to redirect when I click the button. Whats wrong?

You can not use Java code as part of JavaScript. Following solve your problem.

$("#neuesSpiel").click(function() {
   window.location.href="neuesSpiel.jsp";
});

Also you can pass java variable to JavaScript. For Exmaple:

<%  
    String pageName = "neuesSpiel.jsp";  
%>
    <input type="button" value="redirect" onclick="redirectPage('<%=pageName%>')" />

Your javascript:

function redirectPage(pageName){
    window.location.href=pageName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM