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

发布于 2024-11-02 04:40:02 字数 49 浏览 6 评论 0原文

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

Can anyone tell me how to pass JavaScript values to Scriptlet in JSP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

时光礼记 2024-11-09 04:40:02

我可以提供两种方式,

a.jsp

<html>
    <script language="javascript" type="text/javascript">
        function call(){
            var name = "xyz";
            window.location.replace("a.jsp?name="+name);
        }
    </script>
    <input type="button" value="Get" onclick='call()'>
    <%
        String name=request.getParameter("name");
        if(name!=null){
            out.println(name);
        }
    %>
</html>

b.jsp

<script>
    var v="xyz";
</script>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>

I can provide two ways,

a.jsp,

<html>
    <script language="javascript" type="text/javascript">
        function call(){
            var name = "xyz";
            window.location.replace("a.jsp?name="+name);
        }
    </script>
    <input type="button" value="Get" onclick='call()'>
    <%
        String name=request.getParameter("name");
        if(name!=null){
            out.println(name);
        }
    %>
</html>

b.jsp,

<script>
    var v="xyz";
</script>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>
我一直都在从未离去 2024-11-09 04:40:02

您的 javascript 值在客户端,您的 scriptlet 在服务器端运行。因此,如果您想在 scriptlet 中使用 javascript 变量,则需要提交它们。

要实现此目的,请将它们存储在输入字段中并提交表单,或者执行 ajax 请求。我建议你研究一下 JQuery。

Your javascript values are client-side, your scriptlet is running server-side. So if you want to use your javascript variables in a scriptlet, you will need to submit them.

To achieve this, either store them in input fields and submit a form, or perform an ajax request. I suggest you look into JQuery for this.

泼猴你往哪里跑 2024-11-09 04:40:02

很简单,你不能!

JSP 是服务器端,javascript 是客户端,这意味着在评估 javascript 时不再有“jsp 代码”。

simple, you can't!

JSP is server side, javascript is client side meaning at the time the javascript is evaluated there is no more 'jsp code'.

冰之心 2024-11-09 04:40:02

我将这个问题解释为:

“谁能告诉我如何为 JavaScript 传递值以便在 JSP 中使用?”

如果是这种情况,该 HTML 文件会将服务器计算的变量传递给 JSP 中的 JavaScript。

<html>
    <body>
        <script type="text/javascript">
            var serverInfo = "<%=getServletContext().getServerInfo()%>";
            alert("Server information " + serverInfo);
        </script>
    </body>
</html>

I've interpreted this question as:

"Can anyone tell me how to pass values for JavaScript for use in a JSP?"

If that's the case, this HTML file would pass a server-calculated variable to a JavaScript in a JSP.

<html>
    <body>
        <script type="text/javascript">
            var serverInfo = "<%=getServletContext().getServerInfo()%>";
            alert("Server information " + serverInfo);
        </script>
    </body>
</html>
删除→记忆 2024-11-09 04:40:02

您不能这样做,但您可以执行相反的操作:

在您的 jsp 中您可以:

String name = "John Allepe";    
request.setAttribute("CustomerName", name);

访问 js 中的变量:

var name = "<%= request.getAttribute("CustomerName") %>";
alert(name);

You cannot do that but you can do the opposite:

In your jsp you can:

String name = "John Allepe";    
request.setAttribute("CustomerName", name);

Access the variable in the js:

var name = "<%= request.getAttribute("CustomerName") %>";
alert(name);
唐婉 2024-11-09 04:40:02

如果您说要将 javascript 值从一个 jsp 传递到 javascript 中的另一个 jsp,则使用 URLRewriting 技术将 javascript 变量传递到下一个 jsp 文件,并在请求对象的下一个 jsp 中访问该变量。

否则你不能做到这一点。

If you are saying you wanna pass javascript value from one jsp to another in javascript then use URLRewriting technique to pass javascript variable to next jsp file and access that in next jsp in request object.

Other wise you can't do it.

隔岸观火 2024-11-09 04:40:02

正如您所期望的那样,这是不可能的。但你可以做这样的事情。将您的 java 脚本值传递给 servlet/控制器,进行处理,然后根据您的要求将该值放入某个对象中,从而将该值传递给 jsp 页面。然后您可以根据需要使用该值。

Its not possible as you are expecting. But you can do something like this. Pass the your java script value to the servlet/controller, do your processing and then pass this value to the jsp page by putting it into some object's as your requirement. Then you can use this value as you want.

残疾 2024-11-09 04:40:02

这是为其他登陆这里的人准备的。
首先,您需要一个 servlet。我使用了@POST 请求。
现在,在您的 jsp 文件中,您有两种方法可以执行此操作:

  1. 使用 AJAX 的复杂方法,以防您不熟悉 jsp:
    您需要使用要在 java 类中使用的 javascript var 进行发布,并使用 JSP 从请求中调用 java 函数:

    $(文档).ready(function() {
        var sendVar = "你好";
        $('#domId').click(函数(e)
        {                               
            $.ajax({
                类型:“帖子”,
                url: "/", //或者任何你的url
                数据:“var=”+sendVar,
                成功:函数(){      
                        console.log("成功:" + sendVar );                            
                      <% 
                          收到的字符串= request.getParameter("var");
    
                          if(收到== null ||收到.isEmpty()){
                              收到=“一些默认值”;
                          }
                          MyJavaClass.processJSvar(收到); 
                      %>;                            
    
                }
            });
        });
    
    });
    
  2. 使用 JSP 的简单方法:

    <输入类型=“隐藏”名称=“输入名称”值=“”/> <% String pg = request.getParameter("inputName"); if(pg == null || pg.isEmpty()){ pg = "一些默认值"; } DatasyncMain.changeToPage(pg); %>;

当然,在这种情况下,您仍然必须从 JS 加载输入值(到目前为止我还没有找到另一种加载它的方法)。

This is for other people landing here.
First of all you need a servlet. I used a @POST request.
Now in your jsp file you have two ways to do this:

  1. The complicated way with AJAX, in case you are new to jsp:
    You need to do a post with the javascript var that you want to use in you java class and use JSP to call your java function from inside your request:

    $(document).ready(function() {
        var sendVar = "hello";
        $('#domId').click(function (e)
        {                               
            $.ajax({
                type: "post",
                url: "/", //or whatever your url is
                data: "var=" + sendVar ,
                success: function(){      
                        console.log("success: " + sendVar );                            
                      <% 
                          String received= request.getParameter("var");
    
                          if(received == null || received.isEmpty()){
                              received = "some default value";
                          }
                          MyJavaClass.processJSvar(received); 
                      %>;                            
    
                }
            });
        });
    
    });
    
  2. The easy way just with JSP:

    <form id="myform" method="post" action="http://localhost:port/index.jsp">
         <input type="hidden" name="inputName" value=""/>
                   <% 
                          String pg = request.getParameter("inputName");
    
                          if(pg == null || pg.isEmpty()){
                              pg = "some default value";
                          }
                          DatasyncMain.changeToPage(pg); 
                      %>;     
    </form>
    

Of course in this case you still have to load the input value from JS (so far I haven't figured out another way to load it).

请远离我 2024-11-09 04:40:02

我使用了 scriptlet、声明和表达式标签的组合...

<%! 
   public String st; 
%>
<%
   st= "<html> <script> document.writeln('abc') </script> </html>";
%>
<%=
   " a " + st + " <br> "
%>

上面的代码在我的例子中工作得完全正常。

I Used a combination of the scriptlet, declaration, and expression tags...

<%! 
   public String st; 
%>
<%
   st= "<html> <script> document.writeln('abc') </script> </html>";
%>
<%=
   " a " + st + " <br> "
%>

The above code is working completely fine in my case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文