javascript和jsp [英] javascript and jsp

查看:51
本文介绍了javascript和jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手.我有一个带有show users按钮的HTML,单击此按钮后,我要将用户重定向到users.jsp页面.我该如何实现?这样的功能会有所帮助吗?

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/>
}

推荐答案

Java和JavaScript是两种完全不同的语言,它们各自在自己的环境中运行. Java/JSP在Web服务器计算机上运行,​​生成包括HTML/CSS/JS的模板文本,并将其作为HTTP响应通过网络发送到客户端.一旦到达客户端计算机,Web浏览器便开始显示HT​​ML,应用CSS并解释/执行JS.如果Java/JSP做得很好,那么您在获得的HTML源代码(右键单击页面> 查看源代码)中应该看不到任何Java/JSP代码行.

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).

让Java/JSP使用JavaScript进行操作的唯一方法是相应地生成/打印它,以使其在客户端以您想要的方式进行解释/执行.让JavaScript用Java/JSP代码做某事的唯一方法是让它向URL发出HTTP请求,在该URL上JSP文件或Java Servlet侦听然后相应地执行.您可以使用form.submit()window.locationnew XMLHttpRequest()(这是Ajax的基本思想)触发HTTP请求.

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).

在特定情况下,您可以使用window.location:

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

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

,或者如果此按钮是<form>的一部分,则只需在表单操作中指定它即可:

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>

使用

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

要了解有关Java/JSP和JavaScript之间的隔离墙的更多信息,您可以找到此答案且对Java Web开发很有用此答案.

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.

这篇关于javascript和jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆