Java 如何在jsp中传递多个变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24256702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:09:00  来源:igfitidea点击:

How to pass value of more than one variable in jsp?

javajsp

提问by cooljohny

I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:

我想在单击链接到另一个页面时传递两个变量的值我正在使用查询参数,但我只能通过它发送一个变量。我知道 session.setAttribute() 但不知道如何根据链接使用它......例如:

<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value1);%>" class="classname" style=" margin-top:20px;margin-left:800px;"><p> < </p></a>
            <a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>

This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT: suppose my code is this:

这是我的代码,我知道它错了。如果我尝试通过会话传递第二个参数,则只有最终值,即第二个初始化值才算数?该怎么办?
编辑:假设我的代码是这样的:

 <form class="navbar-form navbar-right" action="Search.jsp" method="get">
            <input type="text" class="form-control" placeholder="Search..." name="search">

Here one variable search is passes through form How can I pass another variable value?should it be like:

这里的一个变量搜索是通过表单我如何传递另一个变量值?应该是这样的:

<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
                <input type="text" class="form-control" placeholder="Search..." name="search">

采纳答案by Subir Kumar Sao

You can send multiple parameters like,

您可以发送多个参数,例如,

href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."

Also to add <%session.setAttribute("val",value1);%>will be executed at server side irrespective of the click of the hyperlink.

<%session.setAttribute("val",value1);%>无论点击超链接,添加都将在服务器端执行。

For the form you can add another input parameter in the form,

对于表单,您可以在表单中添加另一个输入参数,

<input type="text" name="item1" value="<%=value%>">

<input type="text" name="item1" value="<%=value%>">

回答by Ninad Pingale

You need to add some separator in between two values e.g.#

您需要在两个值之间添加一些分隔符,例如#

And while reading at server side you can split those values based on that separator

在服务器端阅读时,您可以根据该分隔符拆分这些值

回答by Subir Kumar Sao

You may try using this example to send multiple values:

您可以尝试使用此示例发送多个值:

<a href="same.jsp?item=cap&item=pen&item=ball">With same name</a>
<a href="diff.jsp?gender=male&fname=Adman&lname=Smith">With diff name</a>

回答by Java Enthusiast

Separate the two values by using &:

使用 分隔两个值&

<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">

On your search.jspfetch values as :

在您的search.jsp提取值上:

request.getParameter("item");
request.getParameter("item2");

回答by Pranav Maniar

You need to call session.getAttributein the place where you are calling session.setAttributeand session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2

你需要session.getAttribute在你调用的地方调用session.setAttributesession.setAttribute 应该在控制器或jsp中的link标签之前调用来设置值。请分开值,如Search.jsp?item1=value1&item2=value2