form input value
Hi all,
I have this forms declared inside a jsp.
</FORM>
<FORM name="temp" METHOD="GET" ACTION="basket.jsp">
<INPUT name="i" value="23" TYPE="hidden">
<INPUT name="special" value="" TYPE="hidden">
</FORM>
Then inside the <% tag i want to access the variable i. I have tried
request.getAttribute("i") or getParameter but no success so far.
Please help as i am in great need of this.
Thanks in advance.
# 5
it is a sample but maybe not very related to what you need:
warning it is not the best practice
anyname.jsp
<%@ page contentType="text/html;charset=windows-1252" import="com.mycompany.*,java.util.*"%>
<%
HelperClass helperClass = new HelperClass();
Collection col = helperClass.fetchRecords();
Iterator iter = col.iterator();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>untitled</title>
<script language="javascript">
function checkAll(bookId){
var checkLen = parseFloat("<%=col.size()%>");
if(bookId.checked){
for(var x=1;x<=(checkLen);x++){
document.form1.bookId[x].checked=true;
}
}else{
for(var x=1;x<=(checkLen);x++){
document.form1.bookId[x].checked=false;
}
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="delete.jsp">
<table cellspacing="0" cellpadding="0" border="1" width="400">
<tr>
<td><input type="checkbox" name="bookId" onclick="checkAll(this)"/></td>
<td>TITLE</td>
<td>AUTHOR</td>
<td>BORROWED BY</td>
</tr>
<%while(iter.hasNext()){
BookBean bookBean = (BookBean)iter.next();
%>
<tr>
<td><input type="checkbox" name="bookId" value="<%=bookBean.getId()%>"/></td>
<td><%=bookBean.getTitle()%></td>
<td><%=bookBean.getAuthor()%></td>
<td><%=bookBean.getBorrowedBy()%></td>
</tr>
<%}%>
</table>
<input type="submit" value="delete"/>
</form>
</body>
</html>
delete.jsp
<%@ page contentType="text/html;charset=windows-1252" import="com.mycompany.*,java.util.*"%>
<%
String bookId[] = request.getParameterValues("bookId")!=null?request.getParameterValues("bookId"):new String[0];
HelperClass helperClass = new HelperClass();
helperClass.deleteRecords(bookId);
response.sendRedirect("anyname.jsp");
%>
HelperClass.java
package com.mycompany;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class HelperClass
{
public HelperClass()
{
}
public Collection fetchRecords(){
Collection bookCollection= new ArrayList();
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bookstore","root","tree");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select id,title,author,borrowedby from book");
while(rs.next()){
BookBean bookBean = new BookBean();
bookBean.setId(rs.getInt(1));
bookBean.setAuthor(rs.getString(3));
bookBean.setTitle(rs.getString(2));
bookBean.setBorrowedBy(rs.getString(4));
bookCollection.add(bookBean);
}
rs.close();
conn.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
return bookCollection;
}
public void deleteRecords(String bookId[]){
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bookstore","root","jherald");
PreparedStatement pstmt = conn.prepareStatement("delete from book where id=?");
for(int x=1;x<bookId.length;x++){
pstmt.setInt(1,Integer.parseInt(bookId[x]));
pstmt.executeUpdate();
}
pstmt.close();
conn.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
BookBean.java
package com.mycompany;
public class BookBean
{
private int id;
private String title;
private String author;
private String borrowedBy;
public BookBean()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getBorrowedBy()
{
return borrowedBy;
}
public void setBorrowedBy(String borrowedBy)
{
this.borrowedBy = borrowedBy;
}
}
>