Results 1 to 13 of 13
  1. #1
    mattu36 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default Java applet+save to file

    I have a server in java-applet.
    I would do the game's record list, but writing to file is difficult. How do I write the text file? I can easily read the text file.

    Thanks.
    Last edited by mattu36; 04-01-2009 at 04:27 PM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The applet will be able to communicate with a separate server on the machine that it was downloaded from. If you don't want to write your own server, you can send POST requests to a CGI script on the existing webserver.
    Last edited by OrangeDog; 04-05-2009 at 02:02 AM. Reason: typo

  3. #3
    mattu36 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    This code does not work when I type a file server? Security problem.
    Java Code:
    try {
    		FileWriter fw = new FileWriter("file.txt");
    		PrintWriter out = new PrintWriter(fw);
    		out.println("Hello World");
    		out.close();
    		FileReader fr = new FileReader("file.txt");
    		BufferedReader in = new BufferedReader(fr);
    
    		String s = in.readLine();
    		in.close();
    		System.out.println(s);
    	}
    	catch(Exception e) {
    		System.err.println(e);
    		}
    I do not understand, that how to write a text file in the server. :(

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    What is the error given? Is this running as a standalone server application or in an applet?

  5. #5
    mattu36 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    This is ordinary java-applet.
    appletviewer test.html:
    Java Code:
    java.security.AccessControlException: access denied (java.io.FilePermission file.txt write)
    if I do I start applets Internet (browser) will not be the security warnings, but the writing does not work.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You seem to be misunderstanding the difference between a server and an applet. What you have here is an applet - a small program that is embedded in a web page and downloaded and displayed by a web client (or appletviewer).
    What you want is a server - a completely separate program that runs on a single machine that accepts and serves the requests from remote clients.
    The game applet would send scores to the server. If you try and use file operations in an applet, it will try to access files on the user's machine, which is not allowed.

  7. #7
    mattu36 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes, I see it, but the question is precisely the fact that I can write applets in my web page file and the applet is the same web page?

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    That's not a question. At least it's not one that makes sense. You need to take your server code out of the applet and put it in a separate program. Though, as I said, the simplest way is to send POST data to a CGI script using URLConnection.

  9. #9
    mattu36 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Thank you for your help :) I try (POST, CGI, URLConnection).

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    to be able to write something through your applet you must sign it. To certificate your applet use keytool and jarsigner. After that your applet will be able to act as a desktop application )
    the security exception says that your applet is not trusted (has no certificate). You know I mean? Report

  11. #11
    carloscarlos is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    I'm getting the same error, and I read what you said, but I don't think I know how to do what you're talking about.

    Do you think you could take a look at my code and help me out?

    //import packages
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.applet.*;


    public class SeniorProjectApplet2 extends Applet
    {
    //declare variables
    private char initial, tempChar;
    private String lastName, temp, letterTest, fullName, yearString, monthString, dayString, reservationData, initialString;
    private JPanel panel1, panel2, panel3;
    private JFrame frame;
    private Container container;
    private JTextField initialTf, lastNameTf, yearTf, monthTf, dayTf;
    private JLabel message1, initialL, lastNameL, message2, dateL, message3, blank;
    private JLabel[] room, period;
    private Component click;
    private JButton enter1, enter2, setReservation;
    private JButton[][] labs;
    private boolean initialIsCharacter, initialIsLetter, lastIsString, dateIsCurrent, reservationIsAvailable, monthIsValid, dayIsValid, yearIsInt, monthIsInt, dayIsInt;
    private int currentYear, yearInt, monthInt, dayInt;
    private StringBuffer tempSB;
    private ClickListener action;


    public void init()
    {
    //Step One - get user's first initial and last name
    message1 = new JLabel("Please enter your first initial and last name below. \n");
    initialL = new JLabel("First Initial: ");
    lastNameL = new JLabel("Last Name: ");
    initial = ' ';
    lastName = " ";
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    frame = new JFrame("Reservation Program");
    container = new Container();
    initialTf = new JTextField(1);
    lastNameTf = new JTextField(20);
    enter1 = new JButton("Enter Info.");
    temp = " ";
    fullName = " ";
    letterTest = "abcdefghijklmnopqrstuvwxyz";
    action = new ClickListener();

    //Step Two - get the user's desired date and period to be reserved
    message2 = new JLabel("Please enter the desired date in the appropriate text fields \n");
    dateL = new JLabel("Date (MM DD YYYY): ");
    monthTf = new JTextField(2);
    dayTf = new JTextField(2);
    yearTf = new JTextField(4);
    enter2 = new JButton("Enter Info.");

    //Step Three - have the user select which lab he would like to use
    message3 = new JLabel("Please click the button for the lab you would like to reserve \n");
    blank = new JLabel("");
    labs = new JButton[5][8];
    for(int i = 0; i < labs.length; i++)
    {
    for(int j = 0; j < labs[0].length; j++)
    {
    labs[i][j] = new JButton("Reserve");
    labs[i][j].addActionListener(action);
    }
    }
    room = new JLabel[5];
    room[0] = new JLabel("Room 2C (Tutoring Only)");
    room[1] = new JLabel("Room 222");
    room[2] = new JLabel("Room 223");
    room[3] = new JLabel("Room 229");
    room[4] = new JLabel("Room 232");
    period = new JLabel[8];
    period[0] = new JLabel("Period 1");
    period[1] = new JLabel("Period 2");
    period[2] = new JLabel("Period 3");
    period[3] = new JLabel("Period 4");
    period[4] = new JLabel("Period 5(C&D)");
    period[5] = new JLabel("Period 6(E&F)");
    period[6] = new JLabel("Period 7");
    period[7] = new JLabel("Period 8");
    reservationData = "";
    reservationIsAvailable = true;
    tempSB = new StringBuffer();

    //set font of text
    Font f1 = new Font("Comic Sans", Font.BOLD, 15);
    message1.setFont(f1);
    initialL.setFont(f1);
    lastNameL.setFont(f1);
    message2.setFont(f1);
    dateL.setFont(f1);
    message3.setFont(f1);

    //set color of background
    panel1.setBackground(Color.CYAN);
    panel2.setBackground(Color.CYAN);
    panel3.setBackground(Color.CYAN);

    //add action listener to necessary components
    enter1.addActionListener(action);
    enter2.addActionListener(action);

    //add components for Step One
    panel1.setLayout(new FlowLayout());
    panel1.add(message1);
    panel1.add(initialL);
    panel1.add(initialTf);
    panel1.add(lastNameL);
    panel1.add(lastNameTf);
    panel1.add(enter1);

    //add components for Step Two
    panel2.setLayout(new FlowLayout());
    panel2.add(message2);
    panel2.add(dateL);
    panel2.add(yearTf);
    panel2.add(monthTf);
    panel2.add(dayTf);
    panel2.add(enter2);
    panel2.add(message3);

    //add components for Step Three
    panel3.setLayout(new GridLayout(6,9));
    panel3.add(blank);
    for(int i = 0; i < period.length; i++)
    {
    panel3.add(period[i]);
    }
    for(int i = 0; i < labs.length; i++)
    {
    panel3.add(room[i]);
    for(int j = 0; j < labs[0].length; j++)
    {
    panel3.add(labs[i][j]);
    }
    }

    //disable functions until the completion of step one
    yearTf.setEnabled(false);
    monthTf.setEnabled(false);
    dayTf.setEnabled(false);
    enter2.setEnabled(false);
    for(int i = 0; i < labs.length; i++)
    {
    for(int j = 0; j < labs[0].length; j++)
    {
    labs[i][j].setEnabled(false);
    }
    }

    //add frame to container, pack it, set its size
    container = frame.getContentPane();
    container.add(panel1, BorderLayout.NORTH);
    container.add(panel2, BorderLayout.CENTER);
    container.add(panel3, BorderLayout.SOUTH);
    frame.setSize(300, 300);
    frame.pack();
    frame.setVisible(true);
    }

    class ClickListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    //assign the variable click to the source of the ClickListener
    click = (Component)e.getSource();

    //responds appropriately if ClickListener was called by the enter1 button
    if(click == enter1)
    {
    //sets boolean values to false for error checking
    initialIsCharacter = false;
    initialIsLetter = false;

    //gets text from initial textfield and sets it to a lower case letter
    temp = initialTf.getText();
    temp = temp.toLowerCase();

    //checks to see if the text entered in the initial text field is more or less than one character
    if(temp.length() != 1)
    {
    message1.setText("Try Again. Enter only your first initial in the \"First Initial\" text field");
    initialTf.setText("");
    }
    else
    {
    initialIsCharacter = true;
    initial = temp.charAt(0);
    }

    //if text from initial text field is indeed one character, checks to see that that character is a letter and not a number
    if(initialIsCharacter == true)
    {
    for(int i = 0; i < letterTest.length(); i++)
    {
    if(initial == letterTest.charAt(i))
    {
    initialIsLetter = true;
    break;
    }
    }
    if(initialIsLetter == false)
    {
    message1.setText("Try Again. Enter a single letter in the \"First Initial\" text field");
    initialTf.setText("");
    }
    }

    //gets text from last name text field
    lastName = lastNameTf.getText();

    //checks to see that there is text in the last name text field
    if(lastName.length() == 0)
    {
    message1.setText("Please enter your last name in the \"Last Name\" text field");
    }
    else
    {
    lastIsString = true;
    }

    //if the values entered in the two text fields are valid, disables step one and enables step two, formats user's name and welcomes them to the program
    if(initialIsLetter == true && initialIsCharacter == true && lastIsString == true)
    {
    temp = Character.toString(initial);
    temp = temp.toUpperCase();
    initialString = temp;
    tempChar = lastName.charAt(0);
    temp = Character.toString(tempChar);
    temp = temp.toUpperCase();
    tempSB.delete(0, tempSB.length());
    tempSB.append(temp);
    tempSB.append(lastName);
    tempSB.deleteCharAt(1);
    temp = tempSB.toString();
    lastName = temp;
    fullName = (initialString + ". " + lastName);
    message1.setText("Welcome to the lab reservation program " + fullName + ".");
    initialTf.setEnabled(false);
    lastNameTf.setEnabled(false);
    enter1.setEnabled(false);
    yearTf.setEnabled(true);
    monthTf.setEnabled(true);
    dayTf.setEnabled(true);
    enter2.setEnabled(true);
    }
    }

    //responds appropriately if ClickListener is called by enter2 button
    if(click == enter2)
    {

    //sets boolean values to false for error checking
    dateIsCurrent = false;
    monthIsValid = false;
    dayIsValid = false;
    yearIsInt = false;
    monthIsInt = false;
    dayIsInt = false;

    //creates Calendar to test that the date entered has not already passed and is in the current school year, taking the end of the school year to be June 30th
    Calendar now = Calendar.getInstance();
    Calendar end = Calendar.getInstance();
    if(now.get(Calendar.MONTH) > 6)
    {
    end.set(now.get(Calendar.YEAR) + 1, 6, 30);
    }
    else
    {
    end.set(now.get(Calendar.YEAR), 6, 30);
    }

    //gets text from date text fields and parses them to integers
    yearString = yearTf.getText();
    monthString = monthTf.getText();
    dayString = dayTf.getText();

    //attempts to parse text to integers and sets booleans to false if not parsable
    //year
    try
    {
    yearInt = Integer.parseInt(yearString);
    yearIsInt = true;
    }
    catch(NumberFormatException n)
    {
    yearIsInt = false;
    }
    //month
    try
    {
    monthInt = Integer.parseInt(monthString);
    monthIsInt = true;
    }
    catch(NumberFormatException n)
    {
    monthIsInt = false;
    }
    //day
    try
    {
    dayInt = Integer.parseInt(dayString);
    dayIsInt = true;
    }
    catch(NumberFormatException n)
    {
    dayIsInt = false;
    }

    //tests to see that the integer entered for month is a valid value
    if(monthInt > 12 || monthInt < 1)
    {
    monthIsValid = false;
    }
    else
    {
    monthIsValid = true;
    }

    //tests to see that the integer entered for day is a valid value depending on the month value entered
    if(monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10 || monthInt == 12)
    {
    if(dayInt > 31 || dayInt < 0)
    {
    dayIsValid = false;
    }
    else
    {
    dayIsValid = true;
    }
    }
    if(monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11)
    {
    if(dayInt > 30 || dayInt < 0)
    {
    dayIsValid = false;
    }
    else
    {
    dayIsValid = true;
    }
    }
    if(monthInt == 2 && yearInt% 4 == 0)
    {
    if(dayInt > 29 || dayInt < 0)
    {
    dayIsValid = false;
    }
    else
    {
    dayIsValid = true;
    }
    }
    if(monthInt == 2 && yearInt % 4 != 0)
    {
    if(dayInt > 29 || dayInt < 0)
    {
    dayIsValid = false;
    }
    else
    {
    dayIsValid = true;
    }
    }

    //creates a Calendar with entered integers and tests to see that it is in the current school year
    Calendar cl = Calendar.getInstance();
    cl.set(yearInt, monthInt, dayInt);
    if(cl.compareTo(now) > 0 && cl.compareTo(end) < 0)
    {
    dateIsCurrent = true;
    }
    else
    {
    dateIsCurrent = false;
    }

    //series of if statements that check the booleans and either lock the data or identify the problem to the user and resets the text field to blank
    if(yearIsInt == true && dateIsCurrent == true && monthIsInt == true && monthIsValid == true && dayIsInt == true && dayIsValid == true)
    {
    yearTf.setEnabled(false);
    monthTf.setEnabled(false);
    dayTf.setEnabled(false);
    enter2.setEnabled(false);
    message2.setText("Date Set");
    for(int i = 0; i < labs.length; i++)
    {
    for(int j = 0; j < labs[0].length; j++)
    {
    labs[i][j].setEnabled(true);
    }
    }
    try
    {
    File file = new File("reservations.txt");
    Scanner sc = new Scanner(file);
    tempSB.delete(0, tempSB.length());
    tempSB.append(yearString);
    if(monthString.length() == 1)
    {
    monthString = ("0" + monthString);
    }
    tempSB.append(monthString);
    if(dayString.length() == 1)
    {
    dayString = ("0" + dayString);
    }
    tempSB.append(dayString);
    reservationData = tempSB.toString();
    while(sc.hasNext())
    {
    if(reservationData.equals(sc.next()))
    {
    int i = Integer.parseInt(sc.next());
    int j = Integer.parseInt(sc.next());
    labs[i][j].setEnabled(false);
    }
    }
    sc.close();
    }
    catch(IOException x)
    {
    message3.setText("File Not Found");
    }
    }
    else
    {
    if(yearIsInt == false)
    {
    message2.setText("Please enter a four digit integer in the year text field");
    yearTf.setText("");
    }
    if(dateIsCurrent == false)
    {
    message2.setText("Please enter a date in the current school year");
    yearTf.setText("");
    monthTf.setText("");
    dayTf.setText("");
    }
    if(monthIsInt == false)
    {
    message2.setText("Please enter a two digit integer in the month text field");
    monthTf.setText("");
    }
    if(monthIsValid == false)
    {
    message2.setText("Please enter a month value between 01 and 12");
    monthTf.setText("");
    }
    if(dayIsInt == false)
    {
    message2.setText("Please enter a two digit integer in the day text field");
    dayTf.setText("");
    }
    if(dayIsValid == false)
    {
    message2.setText("Please enter a day value that is valid with respect to the given month value");
    dayTf.setText("");
    }
    }
    }
    for(int i = 0; i < labs.length; i++)
    {
    for(int j = 0; j < labs[0].length; j++)
    {
    if(click == labs[i][j])
    {
    try
    {
    File file = new File("reservations.txt");
    FileWriter fw = new FileWriter(file, true);
    PrintWriter pw = new PrintWriter(fw);
    pw.println(fullName);
    pw.println(reservationData);
    pw.println(i + " " + j);
    pw.close();
    }
    catch(IOException x)
    {
    message3.setText("File Not Found");
    }
    }
    }
    }
    }
    }
    }



    and the html file

    <html>
    <object code = "SeniorProjectApplet2.class" width = 375 height = 300></object>
    </html>


    The purpose of the program is to allow teachers to reserve computer labs at my school. The user enters their name and a date and the program scans a file for matching dates. It then disables the buttons representing the reserved rooms. When the user clicks an enabled button to reserve a particular room for a particular time, the program is supposed to write the info to the file but again I am getting the security error that mattu36 was getting.

    This is for a project for school and its due Wednesday so any help you can give me before then would be really appreciated.

    Thanks a mil

    carlos

  12. #12
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Remove ALL file operations from the applet. As repeatedly stated, file operations will not work in an applet. Even if you signed the applet, you do not want to be using file operations, because they access files on the CLIENT machine. To access files on the server either:

    For read access only, add them to the jar and use getResourceAsStream().
    Connect to the server using a Socket or URLConnection.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  13. #13
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You should better use just InputStream or OutputStream Actually applets are quite limited if they are not signed. Why don't you create a JWS application instead?

Similar Threads

  1. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  2. how to save file..
    By jont717 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 12:33 AM
  3. Java Applet + File Uploading
    By Moncleared in forum New To Java
    Replies: 4
    Last Post: 02-02-2009, 07:33 PM
  4. How to revoke permission to save or copy a PDF file in java
    By priyanka.dandekar in forum Advanced Java
    Replies: 11
    Last Post: 10-05-2008, 07:06 PM
  5. JFileUpload 2.0 released - Java file upload applet
    By jfileupload in forum Java Software
    Replies: 0
    Last Post: 03-09-2008, 03:04 PM

Posting Permissions

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