• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could somebody give some ideas?  RSS feed

Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote several servlets in the first(main), I have:
When your choice is insert you insert employes data like, name, birthdate and SSN.
When your choice is report you will see the all employe data.
My problem is in the process_delete. I wrote the delete servlet I put input type = "checkbox" and input type = "submit" value="Delete".When I use del servlet and choice to delete some employe data I click the delete button and he submit the information to process_delete and here is the problem because I don't no how to put this in my code process_servlet.
My del servlet :
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletInputStream.*;
public class del extends HttpServlet
public void doGet (
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException
PrintWriter out;
out = response.getWriter();
try {
FileReader file1 = new FileReader("c:\\s_test\\data.txt");
BufferedReader buff = new BufferedReader(file1);
out.println("<Page for delete"> );
out.println("<table border=\"1\" width=\"100%\">");
out.println("<Form action=\"http://localhost/servlets/process_del\"method=GET>");
out.println("<td width=\"25%\">NAME</td>");
out.println("<td width=\"25%\">SSN</td>");
out.println("<td width=\"25%\">BIRTHDATE</td></tr>");
out.println("<td><input type=\"reset\" Value=\"Cancel\" name=\"B2\"></tr>");
out.println("<td><input type=\"submit\" Value=\"Delete\" name=\"B2\"></tr>");
String data_line = new String(buff.readLine());
while ((data_line = buff.readLine()) != null) {
String name1 = data_line.substring(0,20).trim();
String SSN1 = data_line.substring(20,30).trim();
String birthdate1= data_line.substring(30).trim();
out.println("<td width=\"25%\"><input type=\"checkbox\" name=\"C1\" value=\"on\"></td>");
out.println("<td>"+ name1 + "<td>" + SSN1 + "<td>" + birthdate1 + "</td></td></td></tr>");
}catch (IOException e) {
out.println("<br> :<ahref+\"http://localhost/servlets/main\">");
out.println("Go to main");
// then--------------
The problem is in the process_del servlet where I have to define this process when your option is delete somebody from the table.
Thanks in advance.

Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Janet,
A couple of comments first. The way you are reading in data.txt
String data_line = new String(buff.readLine());
while ((data_line = buff.readLine()) != null) {
Means that the first line in the file is never added to the output. I don't know if this is the expected behavior or not, I just figured I'd better point that out. If you want the first line simply change this to
String data_line = "";
while ((data_line = buff.readLine()) != null) {

Now for the question you had. I'm going to talk through this problem using psydo-code in the manner I would solve this.
1) get the parameter C1 from the request object
2) get the parameter C2 from the request object
3) if the C2 is equal to cancel create a request dispatch and forward the request to main
4) open the data.txt file
5) read in data.txt fully into a Vector
6) close the data.txt
7) open data.txt as a Writer(this will clear data.txt)
8) write any element in the Vector that did not equal c1 to data.txt
9) close data.txt
10) create a request dispatcher and forward the request to main.

Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited June 28, 2001).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!