如何在没有提交按钮的情况下将值从一个jsp页面传递到另一个jsp?

时间:2014-04-25 07:20:41

标签: jsp

这是我的demo1.jsp页面,

<body>
    <form id="myform" name="myform" method="post" action="demo2.jsp">-->
    <input type="text"  name="usnername" />
    <input type="text" name="password"/>        
    <input type="submit" value="go" onclick="window.location.href='demo2.jsp'" />
</form>

这是我的demo2.jsp

<body>
    <%
    String Uname=request.getParameter("usnername");
    String Usecret=request.getParameter("password");
    out.println(Uname);
    out.println(Usecret);
    %>

这里的代码运行正常但是,如何在不使用sumbit按钮的情况下从demo1到demo2获取值?即,<input type="submit" value="go" onclick="window.location.href='demo2.jsp'" /> bt使用input type =“button”我们可以发送值。

6 个答案:

答案 0 :(得分:4)

您只需要包含以下脚本。

<script language="javascript" type="text/javascript">  
        var xmlHttp  
        function showState(str)
        {
            //if you want any text box value you can get it like below line. 
            //just make sure you have specified its "id" attribute
            var name=document.getElementById("id_attr").value;
            if (typeof XMLHttpRequest != "undefined")
            {
              xmlHttp= new XMLHttpRequest();
            }
            var url="forwardPage.jsp";
            url +="?count1=" +str+"&count2="+name";
            xmlHttp.onreadystatechange = stateChange;
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        }
        function stateChange()
        {   
            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
            {   
                document.getElementById("div_id").innerHTML=xmlHttp.responseText   
            }   
        }
      </script>

所以如果你有代码,让我告诉你,div_id将是div标签的id,你必须显示你的结果。 通过使用此代码,您将参数传递到另一个页面。无论处理是什么,都将反映在id为“div_id”的div标签中。 您可以在任何控件的“onChange”事件上调用showState(this.value),或者不提交按钮的“onClick”事件。 进一步的询问将不胜感激。

答案 1 :(得分:2)

我正在尝试理解您的问题,似乎您希望第一个JSP中的值在第二个JSP中可用。

  1. 在JSP文件中放置Java代码片段是非常糟糕的习惯,因此代码片段应该转到servlet。

  2. 选择servlet中的值,即

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    
  3. 然后将值存储在会话中:

    HttpSession sess = request.getSession(); 
    sess.setAttribute("username", username);
    sess.setAttribute("password", password);
    
  4. 只要会话有效,这些值就可以在应用程序的任何位置使用。

    HttpSession sess = request.getSession(false); //use false to use the existing session
    sess.getAttribute("username");//this will return username anytime in the session
    sess.getAttribute("password");//this will return password Any time in the session
    
  5. 我希望这是你想知道的,但请不要在JSP中使用代码片段。您始终可以使用JSP中的jstl将值获取到JSP中:

     ${username}//this will give you the username in the JSP
     ${password}// this will give you the password in the JSP
    

答案 2 :(得分:2)

注意:在Test.jsp中为Form1.jsp提供准确的路径详细信息 1. Test.jsp和2.Form1.jsp

test.jsp的

<body>
<form method ="get" onsubmit="Form1.jsp">
<input type="text" name="uname">
<input type="submit" value="go" ><br/>
</form>
</body>

Form1.jsp

</head>
<body>
<% String name=request.getParameter("uname"); out.print("welcome "+name); %>  
</body>

答案 3 :(得分:0)

您可以通过以下任何一种方式执行此操作,在此类表单按钮上触发onclick

<form id="myform" name="myform" method="post" action="demo2.jsp">
    <input type="text"  name="usnername" />
    <input type="text" name="password"/>        
    <input type="button" value="go" onclick="submitForm" />
</form>

并使用javascript

        function submitForm() {                
            document.forms[0].submit();
            return true;
        }

或者您也可以尝试Ajax发布您的信息页

这是链接jQueryAjax

还有很好的启动示例using Ajaxhere

希望这会有所帮助!!

答案 4 :(得分:0)

我不知道你想要做什么。但是你不能使用另一种形式的提交按钮发送一个表格的数据。

您可以使用会话或使用具有提交按钮的隐藏字段来执行一项操作。您可以使用javascript / jquery将值从第一个表单传递到第二个表单的隐藏字段。然后您可以提交表单。

否则最简单的就是使用会话。

<form>
<input type="text" class="input-text " value="" size="32" name="user_data[firstname]" id="elm_6">
<input type="text" class="input-text " value="" size="32" name="user_data[lastname]" id="elm_7">
</form>

<form action="#">
<input type="text" class="input-text " value="" size="32" name="user_data[b_firstname]" id="elm_14">
<input type="text" class="input-text " value="" size="32" name="user_data[s_firstname]" id="elm_16">

<input type="submit" value="Continue" name="dispatch[checkout.update_steps]">
</form>


$('input[type=submit]').click(function(){
$('#elm_14').val($('#elm_6').val());
$('#elm_16').val($('#elm_7').val());
});

这是它的jsfiddle http://jsfiddle.net/FPsdy/102/

答案 5 :(得分:0)

您也可以尝试这种方式,

HTML:

<form action="javascript:next()" method="post">
<input type="submit" value=Submit /></form>

使用Javascript:

      function next(){
        //Location where you want to forward your values
        window.location.href = "http://localhost:8563/And/try1.jsp?dymanicValue=" + values; 
        }
相关问题