Results 1 to 10 of 10
  1. #1
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default File IO with Servlet

    Hello!!

    I need help, I want to write a txt file from the data of a jsp. I have the servlet, but this one only show a white window after the save button. I know the "response" will do it, but I dont know how to do it. Can anyone help me

    thanks

    this is the code of the servlet
    Java Code:
    import java.io.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class FileServlet extends HttpServlet{ 
    	 
    	 public void doPost(HttpServletRequest request, 
    	  HttpServletResponse response) throws ServletException,IOException{
    	  response.setContentType("text/html");
    	  PrintWriter out = response.getWriter();
    
    	  String ID="";
    	  String name="";
    	  String Fname="";
    	  String Address="";
    	  String Age="";
    	  String City="";
    	  
    	  ID=request.getParameter("FID");
    	  name=request.getParameter("Fname");
    	  Fname=request.getParameter("FFname");
    	  Address=request.getParameter("FAddress");
    	  Age=request.getParameter("Fage");
    	  City=request.getParameter("Fcity");
    	
    	
    	  if(request.getParameter("SAVE") != null)
    		  		 
    		  
    		  
    	  try {							 													
    		  PrintWriter fileWriter = new PrintWriter(new FileOutputStream("c:/ejemplo/filedata.txt",true));
    		  fileWriter.println( ID + "," + name + "," + Fname + "," + Address + "," + Age + "," + City);
    		 
    		  
    		  
    		  //response(fileWriter);
    		   
    		 
    		  fileWriter.close();  
    	   }
          catch(FileNotFoundException fnfe) {  
              
          }
    	  finally{
    		  out.println("<html><body>");
    		  out.println(ID+": file saved");
    		  out.println("</body></html>");
    
    		  }
    	 }
    }

  2. #2
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default reply

    hi Lanaizen,

    why didn't you do anything in the catch block....just add

    fnfe.printStackTrace();

    in the catch block...you'll get the full stack trace with the error message and I hope you'll yourself come to know what's the error once you do it....give it a try and if u still cannot find out...post the stacktrace here...we'll be glad to help you out..

    good luck :)

  3. #3
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks
    Let me try a few more times.

  4. #4
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Hello!
    Well, I'm here one more time, this time with some progress (First A basic error I forget to add a key "{" where "if" is, now the console , localhost, save the data after the save button, but I still dont have data in the txt file.

    before :

    XML Code:
    http://localhost:8080/PIntranet/file.do
    After:
    XML Code:
    http://localhost:8080/PIntranet/file.do?FID=01&Fname=alen&FFname=mester&FAddress=terra&Fage=27&Fcity=Dallas&B1=SAVE
    Can anyone remember me what else this servlet needs ? And how the "response" works

    thanks
    Java Code:
    import java.io.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class FileServlet extends HttpServlet{ 
    
    	  String ID="";
    	  String name="";
    	  String Fname="";
    	  String Address="";
    	  String Age="";
    	  String City="";
    
    public void doGet(HttpServletRequest request, 
    	  HttpServletResponse response) throws ServletException,IOException
    	  {
    		PrintWriter out = response.getWriter();
    		response.setContentType("text/html");
    
    		ID=request.getParameter("FID");
    		name=request.getParameter("Fname");
    		Fname=request.getParameter("FFname");
    		Address=request.getParameter("FAddress");
    		Age=request.getParameter("Fage");
    		City=request.getParameter("Fcity");
    	
    	
    	  if(request.getParameter("SAVE") != null){
    		  
    	    		   
    	  try {							 													
    		  PrintWriter fileWriter = new PrintWriter(new FileOutputStream("c:/ejemplo/filedata.txt",true));
    		  fileWriter.println( ID + "," + name + "," + Fname + "," + Address + "," + Age + "," + City);
    		
    		  fileWriter.close();  
    	   }
          catch(FileNotFoundException fnfe) {  
        	  	fnfe.printStackTrace();
          }
    	  finally{
    		  out.println("<html><body>");
    		  out.println(ID+": file saved");
    		  out.println("</body></html>");
    
    		  }
    	 }
       }
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Java Code:
    if(request.getParameter("[B]SAVE[/B]") != null)
    http://localhost:8080/PIntranet/file.do?...B1=SAVE
    Your parameter in the request seems to be called B1, not SAVE.

    Also, you should not give your servlet any attributes. There will be a single instance of this servlet on your server, which will serve all requests in a multi-threaded environment. So request A will execute, possibly pause in the middle of your doGet to allow thread B to run...which would overwrite those attributes, thus mucking up thread A.

  6. #6
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    What do you mean? you should not give your servlet any attributes.
    I fix the B1=SAVE then I remove the request from the servelt.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    I explained why you should not have any attributes on your servlet.
    These:
    Java Code:
    	  String ID="";
    	  String name="";
    	  String Fname="";
    	  String Address="";
    	  String Age="";
    	  String City="";
    should be local to the doGet method otherwise your code will fail when dealing with more than one request at a time.

  8. #8
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for help this junior, I remove the attributes as you say. Bu I have the same answer. I have nothing in the text file

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Have you changed the parameter?
    Can you show us your new code, and the request you are sending?

  10. #10
    Lanaizen is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Finally!! I found the error, it was a red mark in the servlet I didnt see as well the name of the value on the jsp.
    Here is the servelt and jsp.
    XML Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="file.do" method="GET">
    
    <center>PERSONAL DATA</center>
    ID:<input type="text" name="FID" size="14"><br>
    Name:<input type="text" name="Fname" size="14"><br>
    Full Name:<input type="text" name="FFname" size="14"><br>
    Address:<input type="text" name="FAddress" size="14"><br>
    Age:<input type="text" name="Fage" size="14"><br>
    City:<input type="text" name="Fcity" size="14"><br>
    <p><input type="submit" value="SAVE" name="B1"></p> 
    </form>
    </body>
    </html>
    Java Code:
    import java.io.*;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class FileServlet extends HttpServlet{ 
    
    public void doGet(HttpServletRequest request, 
    	  HttpServletResponse response) throws ServletException,IOException
    	  {
    	
    	  //String ID="";
    	  //String name="";
    	  //String Fname="";
    	  //String Address="";
    	  //String Age="";
    	  //String City="";
    	  
    	PrintWriter out = response.getWriter();
    	response.setContentType("text/html");
    
    		String ID=request.getParameter("FID");
    		String name=request.getParameter("Fname");
    		String Fname=request.getParameter("FFname");
    		String Address=request.getParameter("FAddress");
    		String Age=request.getParameter("Fage");
    		String City=request.getParameter("Fcity");
    		
    	  if(request.getParameter("B1") != null){
    		  PrintStream p = null;
    	    		   
    	  try {
    
    		  PrintWriter fileWriter = new PrintWriter(new FileOutputStream("c:/ejemplo/filedata.txt",true));
    		  fileWriter.println( ID + "," + name + "," + Fname + "," + Address + "," + Age + "," + City);
    		                                      //response(fileWriter);
    		  fileWriter.close();  
    	   }
          catch(FileNotFoundException fnfe) {  
        	  	fnfe.printStackTrace();
          }
    	  finally{
    		  out.println("<html><body>");
    		  out.println(ID+": file saved");
    		  out.println("</body></html>");
    
    		  }
    	 }
       }
    }
    Thanks
    Last edited by Lanaizen; 07-28-2011 at 06:03 PM.

Similar Threads

  1. load xml file in servlet help please
    By khdani in forum Java Servlet
    Replies: 2
    Last Post: 10-05-2009, 10:28 PM
  2. Replies: 6
    Last Post: 06-04-2009, 11:09 PM
  3. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  4. Uploading a file with Servlet
    By farakhkhan@yahoo.com in forum New To Java
    Replies: 0
    Last Post: 03-10-2008, 08:02 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •