0

I want to retrieve the data from this table given below and insert that data in the database in the next servlet. Should i use array here or something else PLease help. As I am trying to retrieving the data through request.getParameter() it is retrieving only one value and I want all the values

package com.sk; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class crea extends HttpServlet { 
private static final long serialVersionUID = 1L; 

/** 
* @see HttpServlet#HttpServlet() 
*/ 
public crea() { 
super(); 
// TODO Auto-generated constructor stub 
} 

/** 
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
// TODO Auto-generated method stub 
String tn=request.getParameter("tn"); 
//  System.out.println(tn); 
String np=request.getParameter("nc"); 
//  System.out.println(np); 
int n=Integer.parseInt(np); 
PrintWriter out=response.getWriter(); 
out.print("<html><body>"); 
out.print("<form action='creat'method='get'>"); 
out.print("<table border='1'"); 
out.print("<tr><th>NAME</TH><TH>TYPE</TH><TH>LENGTH</TH></TR>"); 
for (int i=0;i<n;i++) 
{ 
out.print("<tr><td><input type='text'name='na'></td><td><input type='text' name='type'></td><td><input type='text' name='length'></td>"); 
} 
out.print("<input type='submit' value='create'>"); 
out.print("</table>"); 
out.print("</body></html>"); 

} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
// TODO Auto-generated method stub 
} 

} 
shubham
  • 11
  • 1
  • 2
  • then how i will create the table as I have to create it according to the given number for (int i=0;i"); } – shubham Apr 01 '15 at 18:04
  • Firstly, source code in comments is typically unpolite. Please move it to your question. Secondly, this style of java coding was abandoned with the advent of JSTL. This technique is from 1997, when Perl ruled CGI scripts. Try to rewrite your code using JSTL and at least if you still have problems, you won't get down votes. – avgvstvs Apr 25 '15 at 06:07

2 Answers2

1

You should use the method getParameterValues

More info in the java api:

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.String)

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25
1

Use

request.getParameterValues();

instead. It will return array of string which have same name in input type

HTML file: Both Input text have same name like 'td' and may have different values like 'a,b'.
Servlet:

String td[]=request.getParameterValues("td");

this will give you array like ['a','b']

Hiren
  • 1,427
  • 1
  • 18
  • 35