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.

[726 byte] By [PirateManiaca] at [2007-11-26 16:25:39]
# 1
If you want to access the form elements on the client side, you will have to use Javascript. The approach that you are using (i.e. request.getParameter() ) will work only on the server side, when the form is submitted.
jaikirana at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

What i would like to do is:

in the jsp i print a table of my database with chechboxes near everyone of it's lines. I want when user submits the form, to pass in another jsp file, the selected table lines, or save them in a session.

How that can be achieved with javascript i don't know, because javascript cannot handle arrays created by jsp.

Thanks for your help.

PirateManiaca at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
i have a sample code for that if you are still interested
jgalacambraa at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
I would be grateful if you would be willing to share this code with me.Because i am trying to build something great for my project, and this would surely give an excellent tone :)Thanks in advance,
PirateManiaca at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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;

}

}

>

jgalacambraa at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thanks for your help jgalacambra. I did the same thing in another way. But thanks again for your kind help.
PirateManiaca at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
hope that code helps (',')
jgalacambraa at 2007-7-8 22:49:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...