tags:

views:

109

answers:

4

I am new to Java. I have a HTML which has a show users button, on clicking this button I want to redirect the user to the users.jsp page. How do I achieve that? Will a function like this help?

function msg()
{
 alert("List of Users");<br/>
 jsp:forward page="Users.jsp"<br/>
}
+1  A: 

=) first thing to learn: java != javascript

anyway, here's a nice function for you

function msg() {
    alert("List of Users");
    location.href = "users.jsp";
}

and the accompanying HTML

<button onclick="msg()">Click Me!</button>

good luck

===edit===

Full HTML page:

<html>
<head><title>Hi</title></head>
<body>

<script>
    function msg() {
        alert("List of Users");
        location.href = "users.jsp";
    }
</script>
<button onclick="msg()">Click Me!</button>

</body>
</html>

Essentially, if you print that ^ out in your servelet, you will get the desired result.

Jared Forsyth
hi jared,thanks for the quick reply. Can you tell me whats the best way to get a pop-up in jsps/servelets.
akellakarthik1254
I assume you want the popup to appear in the browser? Your servelet outputs HTML, which is then interpreted by the browser. -- see my edit for some example HTML
Jared Forsyth
+3  A: 

Java and JavaScript are two entirely distinct languages which runs each on its own environment. Java/JSP runs at the webserver machine, produces template text including HTML/CSS/JS and sends it over network to the client side as a HTTP response. Once arrived at the client machine, the webbrowser starts to display HTML, apply CSS and interpret/execute JS. If Java/JSP has done its job well, you should not see any line of Java/JSP code in the obtained HTML source (rightclick page > View Source).

The only way to let Java/JSP do something with JavaScript is to generate/print it accordingly that it get interpreted/executed in the client side the way you want. The only way to let JavaScript do something with Java/JSP code is to let it fire a HTTP request to an URL on which a JSP file or a Java Servlet listens and then exectues accordingly. You can fire a HTTP request with form.submit(), window.location and new XMLHttpRequest() (which is the base idea of Ajax).

In your particular case, you can just use window.location:

function msg() {
   alert('List of Users');
   window.location = 'Users.jsp';
}

or if this button is part of a <form>, then just specify it in the form action:

<form action="Users.jsp" onclick="msg()">
    <input type="submit">
</form>

with

function msg() {
   alert('List of Users');
}

To learn more about the wall between Java/JSP and JavaScript, you may find this article useful. As to learning Web Development in general, you may find this answer useful and for Java Web Development this answer.

BalusC
+1  A: 

If you are more comfortable with Java than Javascript, perhaps Google Web Toolkit would be a desirable option.

Jared Forsyth
A: 

You cannot embed JSP in JavaScript, because JavaScript is ran by the browser, and browser does not implement JSP.

Try window.location = "Users.jsp"; in JavaScript.

SHiNKiROU