Wednesday 6 December 2017

java - How to retrieve "Grouped" items from HTML form using Servlet?

I am having an issue with retriving "grouped" data from
HTML form to servlet. I will describve the scenario
below.


In companies, they record the salary of the
employees once a month.When they record it, they do not do it by visiting each an every
employees personal "profile" (or whatever according to the system). Instead what they do
is apply the salaries of all of them in one page.


To do the
above thing they prefer excel like tabular sheets.


Now, I
have a html form, where the form content is a table. One row is dedicated to a one
employee.


Below is my
form.


<%--
Document :
index2
Created on : Mar 5, 2015, 10:04:45
AM
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
html>


http-equiv="Content-Type" content="text/html; charset=UTF-8">

JSP Page



Hello World!


method="post" action="EmployeeSampleServlet">
style="width:100%">







name="tableBody" value="1">

















value="3">

















value="5">








Name Position Salary
type="text" name="nameTxt" style="width:100%"/> style="width:100%"/> name="salaryTxt" style="width:100%"/>
style="width:100%"/> name="positionTxt" style="width:100%"/> type="text" name="salaryTxt" style="width:100%"/>
name="nameTxt" style="width:100%"/> type="text" name="positionTxt" style="width:100%"/> style="width:100%"/>
style="width:100%"/> name="positionTxt" style="width:100%"/> type="text" name="salaryTxt" style="width:100%"/>
name="nameTxt" style="width:100%"/> type="text" name="positionTxt" style="width:100%"/> style="width:100%"/>


value="Submit">





As
you can see, I have wrapped every row with a . The
value attribute of the
will contain the employee id.


Once the form is submitted,
the below servlet will capture it.


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 EmployeeSampleServlet
extends HttpServlet {
protected void processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
String[]empId =
request.getParameterValues("tableBody");
for(int
i=0;i {
out.println(empId[i]);

}
}
//

/**
* Handles the HTTP GET method.

*
* @param request servlet request
* @param response servlet
response
* @throws ServletException if a servlet-specific error
occurs
* @throws IOException if an I/O error occurs
*/

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
*
Handles the HTTP POST method.
*
* @param
request servlet request
* @param response servlet response
*
@throws ServletException if a servlet-specific error occurs
* @throws
IOException if an I/O error occurs
*/
@Override

protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

processRequest(request, response);
}
/**
* Returns a
short description of the servlet.
*
* @return a String containing
servlet description
*/
@Override
public String
getServletInfo() {
return "Short description";
}//

}

What
I was trying is get the value attribute of
(so I can identify the id of the employee) and
get the data inside that . However this didn't
work, because I ended up with NullpointerException because it
failed to read the
value.


So, how can I pass the data from table to servlet
where it can clearly understand that one row is representing data belong to a one
employee? If this is not the way to do it, I am also open for other
methods.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...