user1506919
user1506919

Reputation: 2607

Method to open another jsp page in Javascript

I am trying to use buttons in an html/javascript program to redirect someone to another one of my pages. Basically, I created a .jsp page that gives the user some buttons to click. If the user clicks a button, it calls a method that is supposed to open a new .jsp page that I created under the same project. However, I have no clue how to do this as I am brand new to Javascript and HTML. I provided an example below:

Sample Page

<html>
<title>Web Page</title>
<body>

    <p>Please click a button to redirect you to the page you wish to go to.</p>

    <br>

    <form>
        <input type="button" id="idname" value = "General Info " onclick="goToInfo()"/><br>
    </form>

    <br>

    <form>
        <input type="button" id="idname" value = "Other Information" onclick="goToOther()"/><br>
    </form>



    <script type="text/javascript">
        function goToInfo(){

        }

        function goToOther(){

        }

        </script>
</body>
</html>

*For example, I have another page in my NetBeans project that is called "GeneralInfo.jsp" and I want goToInfo() to be able to redirect the person to that page. Thank you for your help.

Upvotes: 3

Views: 47690

Answers (3)

cmb28
cmb28

Reputation: 421

you can use this method,

 <input type="button" value="General Info" name="INfoPage"
        onclick="document.forms[0].action = 'infoPage.jsp';" />

or the easy way is,

<a href="InfoPage.jsp">General Info</a>

Upvotes: 0

user1558223
user1558223

Reputation: 197

this should open new tab with GeneralInfo.jsp when you click on the General Info button..

function goToInfo(){
window.location = "GeneralInfo.jsp";
 }

Upvotes: 0

jamesmortensen
jamesmortensen

Reputation: 34038

You could use window.location to redirect the user to the corresponding JSP pages; however, you'll need to make sure your paths in the code below match the actual paths to your JSP page as mapped by your servlet or based on the absolute path relative to the application.

   function goToInfo(){
       window.location = '/GeneralInfo.jsp';
   }

   function goToOther(){
       window.location = '/someOtherJSPPage.jsp';
   }

If you get 404 errors when trying to redirect to your JSP page, try turning up your logging level to ALL or DEBUG so that you can see the logs from the framework or Java container, these will hopefully show you the real file paths so that you can then adjust the URL to match the actual target location.

Upvotes: 7

Related Questions