如何在JSP中将JavaScript值传递给Scriptlet?

谁能告诉我如何在JSP中将JavaScript值传递给Scriptlet?



茅侃侃
浏览 460回答 3
3回答

慕慕森

您的javascript值在客户端,您的scriptlet在服务器端运行。因此,如果要在scriptlet中使用javascript变量,则需要提交它们。为此,可以将它们存储在输入字段中并提交表单,或者执行ajax请求。我建议您对此进行研究

梵蒂冈之花

我可以提供两种方式a.jsp,<html>&nbsp; &nbsp; <script language="javascript" type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; function call(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var name = "xyz";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.replace("a.jsp?name="+name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; &nbsp; <input type="button" value="Get" onclick='call()'>&nbsp; &nbsp; <%&nbsp; &nbsp; &nbsp; &nbsp; String name=request.getParameter("name");&nbsp; &nbsp; &nbsp; &nbsp; if(name!=null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println(name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; %></html>b.jsp,<script>&nbsp; &nbsp; var v="xyz";</script><%&nbsp;&nbsp; &nbsp; String st="<script>document.writeln(v)</script>";&nbsp; &nbsp; out.println("value="+st);&nbsp;%>

aluckdog

我把这个问题解释为:“谁能告诉我怎么传值的的JavaScript在JSP用?”如果是这种情况,则此HTML文件会将服务器计算的变量传递给JSP中的JavaScript。<html>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var serverInfo = "<%=getServletContext().getServerInfo()%>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Server information " + serverInfo);&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JavaScript