I am creating small web apps and I am facing the following problem. I have 2 JSPs and when I click the submit button, it repeats the value every time. What I want is that when I click on the submit button it should give only the corresponding value.
我正在创建小型网络应用程序,我面临以下问题。我有2个JSP,当我单击提交按钮时,它每次都重复该值。我想要的是,当我点击提交按钮时,它应该只给出相应的值。
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*,java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Class Video</title>
</head>
<body>
<form action="second.jsp" method="post">
<table>
<%
File f=new File("C:/Users/SHAKTI/Desktop/video");
File[] list=f.listFiles();
if(list.length!=0){
String s[]=new String[list.length];
for(int i=0;i<list.length;i++){
s[i]=list[i].toString();
String fi=list[i].getName();
%>
<tr><td><%=fi %></td>
<td><input type="text" name="file" value="<%=s[i] %>">
</td>
<td><input type="submit" name="play" value="Play"></td>
</tr>
<%}}
else{
%>
<tr>
<td>There is no any files in the database...</td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
second.jsp
<form action="" method="post">
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
String id=request.getParameter("file");
out.println("id = "+id);
%>
<input type="submit" name="submit" value="submit>
</form>
6 个解决方案
#1
0
First Jsp : Set Value in First Page
第一个Jsp:在第一页设置值
request.setAttribute("name",somevalue);
Second Jsp : Retrieve it in Second page
第二个Jsp:在第二页中检索它
request.getAttribute("name")
#2
0
use queryString
URl: http://myaddress.com/xyz?name=jill&sex=f
String someName = request.getParameter("name") ;
String sex = request.getParameter("sex") ;
#3
0
One way is to use session as described by javaBeginner.
一种方法是使用javaBeginner描述的会话。
you can also create a from on the fly and submit it.
您也可以动态创建并提交。
write a function similar to this one and use it in your success:
写一个类似于这个的函数,并在你的成功中使用它:
function submitValues(url, params) {
var form = [ '<form method="POST" action="', url, '">' ];
for(var key in params)
form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
form.push('</form>');
jQuery(form.join('')).appendTo('body')[0].submit();
}
#4
0
There are many options to pass a value from one jsp to another, Here are some
将值从一个jsp传递到另一个jsp有很多选项,以下是一些选项
1) Adding that as a hidden variable and specifying the value
1)将其添加为隐藏变量并指定值
<input name="file" type="hidden" value=""/>
2)Adding it to the session and retrieving the session variable
2)将其添加到会话并检索会话变量
session.setAttribute("file", value);
3)Passing that as a queryParameter
3)将其作为queryParameter传递
http://......one.jsp?file=""
#5
0
It is beacause you are iterating the loop here,
这是因为你在这里迭代循环,
for(int i=0;i<list.length;i++){
s[i]=list[i].toString();
String fi=list[i].getName();
so it will print the last element from the loop, to get the name u clicked on the button try this .. Change this line as
所以它将打印循环中的最后一个元素,以获得名称u点击按钮试试这个..改变这一行为
<input type="text" name="file" value="<%=s[i] %>">
as,
<td><input type="button" class="btn" data-reqno=value="<%=s[i] %>" value="file name">
And handle it using jQuery like this , so that you can pass the value to next JSP,
并使用这样的jQuery处理它,以便您可以将值传递给下一个JSP,
<script type="text/javascript">
$(document).ready(function () {
$(".btn").click(
function() {
var selectedFileName = $(this).attr("data-reqno");
var urlToApprove = "/yourApp/req/filename?name=" + selectedFileName;
}
);
});
</script>
And also try to avoid Scriptlets in JSP , you can use JSTL or El for the same purpose . Hope it helps !!
并且还尝试在JSP中避免使用Scriptlet,您可以将JSTL或El用于相同的目的。希望能帮助到你 !!
#6
0
there are many method to pass variable such as
传递变量的方法有很多种
session.setAttribute()
session.getAttribute
action "xxx.jsp?variable=1"
#1
0
First Jsp : Set Value in First Page
第一个Jsp:在第一页设置值
request.setAttribute("name",somevalue);
Second Jsp : Retrieve it in Second page
第二个Jsp:在第二页中检索它
request.getAttribute("name")
#2
0
use queryString
URl: http://myaddress.com/xyz?name=jill&sex=f
String someName = request.getParameter("name") ;
String sex = request.getParameter("sex") ;
#3
0
One way is to use session as described by javaBeginner.
一种方法是使用javaBeginner描述的会话。
you can also create a from on the fly and submit it.
您也可以动态创建并提交。
write a function similar to this one and use it in your success:
写一个类似于这个的函数,并在你的成功中使用它:
function submitValues(url, params) {
var form = [ '<form method="POST" action="', url, '">' ];
for(var key in params)
form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
form.push('</form>');
jQuery(form.join('')).appendTo('body')[0].submit();
}
#4
0
There are many options to pass a value from one jsp to another, Here are some
将值从一个jsp传递到另一个jsp有很多选项,以下是一些选项
1) Adding that as a hidden variable and specifying the value
1)将其添加为隐藏变量并指定值
<input name="file" type="hidden" value=""/>
2)Adding it to the session and retrieving the session variable
2)将其添加到会话并检索会话变量
session.setAttribute("file", value);
3)Passing that as a queryParameter
3)将其作为queryParameter传递
http://......one.jsp?file=""
#5
0
It is beacause you are iterating the loop here,
这是因为你在这里迭代循环,
for(int i=0;i<list.length;i++){
s[i]=list[i].toString();
String fi=list[i].getName();
so it will print the last element from the loop, to get the name u clicked on the button try this .. Change this line as
所以它将打印循环中的最后一个元素,以获得名称u点击按钮试试这个..改变这一行为
<input type="text" name="file" value="<%=s[i] %>">
as,
<td><input type="button" class="btn" data-reqno=value="<%=s[i] %>" value="file name">
And handle it using jQuery like this , so that you can pass the value to next JSP,
并使用这样的jQuery处理它,以便您可以将值传递给下一个JSP,
<script type="text/javascript">
$(document).ready(function () {
$(".btn").click(
function() {
var selectedFileName = $(this).attr("data-reqno");
var urlToApprove = "/yourApp/req/filename?name=" + selectedFileName;
}
);
});
</script>
And also try to avoid Scriptlets in JSP , you can use JSTL or El for the same purpose . Hope it helps !!
并且还尝试在JSP中避免使用Scriptlet,您可以将JSTL或El用于相同的目的。希望能帮助到你 !!
#6
0
there are many method to pass variable such as
传递变量的方法有很多种
session.setAttribute()
session.getAttribute
action "xxx.jsp?variable=1"