Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2007

Tip: Looking for answers? Try searching our database.

when filewriter(file, true) second attribute is true, the output is written twice.?

Thread view: 
jiepie2@gmail.com - 10 Sep 2007 20:56 GMT
why does my out put file "log.txt" has the output written twice?

here is my code:

package productsupport;

import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import java.util.GregorianCalendar;
public class setAtt extends HttpServlet {
    private String driver, dburl;

    public void doGet(HttpServletRequest request,
                                HttpServletResponse response)
                       throws IOException, ServletException
   {
        BufferedWriter  log = new BufferedWriter (new FileWriter("log.txt",
true));
        response.setContentType("text/html");
       response.setHeader("pragma", "no-cache");
       String ip = request.getParameter("ip");
       String port = request.getParameter("port");
       String sid = request.getParameter("sid");
       String user = request.getParameter("user");
       String password = request.getParameter("password");
       String schema = request.getParameter("schema");
       String parkId = request.getParameter("parkId");
       String attId= request.getParameter("attId");
       String attValue= request.getParameter("attValue");
       String query = "";
       String siteId="";
       Calendar now = new GregorianCalendar();

       for (int i=0; i< request.getParameterValues("siteId").length ;
i++ ){
           if (i != (request.getParameterValues("siteId").length -1) ){
               siteId = siteId + request.getParameterValues("siteId")[i] +
",";
           }else {
               siteId = siteId + request.getParameterValues("siteId")[i];
           }

       }

       driver = "oracle.jdbc.driver.OracleDriver";
       dburl = "jdbc:oracle:thin:" + user + "/" + password +
                               "@" + ip + ":" + port + ":" + sid;
       PrintWriter out = response.getWriter( );
       out.println("<HTML>");
       out.println("<HEAD>");

       /* java scripts*/
       out.println("<script type=\"text/javascript\">");
       /**
        * function sumbitForm
        */
       out.println("function submitForm(actionPage) {");

out.println("document.getElementById('setAttForm').action=actionPage;");

out.println("document.getElementById('setAttForm').submit();");
       out.println("return 0; ");
       out.println("}");
       out.println("</script>");

       out.println("<TITLE>");
       out.println("Please Select the Park you want to update.");
       out.println("</TITLE>");
       out.println("</HEAD>");
       out.println("<BODY>");
       out.println("<H3>");
       out.println("Please select the park you want to update.");
       out.println("</H3>");
       out.println("Site id: " + siteId);
       /* connection details*/
       out.println("<form id='setAttForm'>");
       out.println("<br> Host or IP: <input type=text  id=ip size=15
name=ip  value="+ip+" / >" );
       out.println("Port: <input type=text id=port name=port size=5
value="+ port +" />");
       out.println( "sid: <input type=text  id=sid name=sid size=3
value=" + sid +" />");
       out.println( "User: <input type=text  id=user  name=user
value="+user+" />");
       out.println( "Password: <input type=text  id=password
name=password value=" + password+" />");
       out.println( "Schema: <input type=text  id=schema
name=schema  value=" + schema+" /><br>");
       out.println( "Park id: <input type=text  id=parkId
name=parkId  value=" + parkId+" /><br>");
       out.println("<br> Attribute ID: " + attId);
       out.println("<br> Attribute Value: " + attValue);

       /*check which attribute to update,
        * and only update attribute when attribute value was
provided
        */

       log.newLine();
       log.append("log file opened");
       if (attId.equals("218")&& !attValue.equals("")){

           for (int i=0; i< request.getParameterValues("siteId").length;
i++ ){
               out.println("<br><br> Update site id: " +
request.getParameterValues("siteId")[i]);
               query = "update "+schema+".p_prd_attr set
attr_value='"+attValue+"' where attr_id=" +
                       attId +" and prd_id=" +
request.getParameterValues("siteId")[i]+ " ";
               out.println("<br>query: "+    query);
               try{

                   out.println("<br>start logging ");
                   log.newLine();
                   log.write(now.get(Calendar.HOUR) +":"+
now.get(Calendar.MINUTE)+":"+
                           now.get(Calendar.SECOND)+": "+ query);
                   out.println("<br>end logging ");

                   dbAccess dbase = new dbAccess (driver, dburl);
                   //ResultSet rs =
                   dbase.conn.createStatement().executeQuery(query);

                   } catch (Exception e){
                       e.printStackTrace();
                       out.println("<br>Error: "  + e);
               }
           log.newLine();
           log.write("closing log");
           log.close();
           }

       }

       /* display Change Schema Button */
        out.println("<br> <input type='submit' value='Change Schema' " +
                    "onClick=javascript:submitForm('login'); >" );

        /* display Change Park Button */
        out.println("<br> <input type='submit' value='Change Park' " +
        "onClick=javascript:submitForm('selectPark'); >" );

        /* display Change Site Button */
        out.println("<br> <input type='submit' value='Display Sites' " +
                    "onClick=javascript:submitForm('selectSites'); >" );
        out.println("</form>");

       out.println("</BODY>");
       out.println("</HTML>");

       out.close();
   }
}
Flo 'Irian' Schaetz - 10 Sep 2007 20:59 GMT
And thus spoke jiepie2@gmail.com...

> why does my out put file "log.txt" has the output written twice?

Probably because the 2nd argument is "append", so every time you start
it, it appends the text to the file...?

Flo
Roedy Green - 11 Sep 2007 08:31 GMT
>BufferedWriter  log = new BufferedWriter (new FileWriter("log.txt",
>true));

should read:

BufferedWriter  log = new BufferedWriter (new
FileWriter("log.txt",false));

see http://mindprod.com/applet/fileio.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

jiepie2@gmail.com - 12 Sep 2007 13:44 GMT
On Sep 11, 3:31 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 10 Sep 2007 12:56:31 -0700, jiep...@gmail.com wrote, quoted or
> indirectly quoted someone who said :
[quoted text clipped - 11 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

hi thanks for replying.

however, i need to the append function, because i want to add more
text the existing file. so therefore i need to do this:
BufferedWriter  log = new BufferedWriter (new>
FileWriter("log.txt",true));

one weird thing i found is, my program works in Firefox, but not in IE
7. IE7 simply invoke the servlet twice. do you have solution to this
problem?
Roedy Green - 13 Sep 2007 01:52 GMT
>however, i need to the append function, because i want to add more
>text the existing file. so therefore i need to do this:
>BufferedWriter  log = new BufferedWriter (new>
>FileWriter("log.txt",true));

Your problem is the browser is free to invoke and shutdown your applet
as many times as it wants.  Your code does an append every time it
starts up., so you get "duplicates".

To understand what is going on add some debug code to dump out the
contents of the log.txt just before you open it, or perhaps just the
last few lines if it is voluminous.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

kcwong - 13 Sep 2007 08:55 GMT
On Sep 13, 8:52 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> Your problem is the browser is free to invoke and shutdown your applet
> as many times as it wants.  Your code does an append every time it
> starts up., so you get "duplicates".

Er... I went through the OP's first post several times, but I didn't
see anything about an applet... it's a servlet...
Roedy Green - 25 Sep 2007 08:48 GMT
>Er... I went through the OP's first post several times, but I didn't
>see anything about an applet... it's a servlet...

You have the same problem with Servlets.  The Servlet womb is free to
shut down and restart the Servlet any time it wants to conserve RAM.

You can track this by putting in some static init code to print out a
message every time the class is loaded, and in the constructor to
monitor every time the Servlet is instantiated.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 25 Sep 2007 15:22 GMT
>> Er... I went through the OP's first post several times, but I didn't
>> see anything about an applet... it's a servlet...
[quoted text clipped - 5 lines]
> message every time the class is loaded, and in the constructor to
> monitor every time the Servlet is instantiated.

You can also make use of the servlet's lifecycle methods, like init().

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.