How to Write and Read file in JSP ?

Hi Experts;

I'm new in JSP but not in Java. I have a webserver for my JSP, now I created
a simple forum which temporarily displays the message in the other TextArea what
I wanna do is to be able to write those messages into a file and read them back
like a normal forum.

How can I do this in jsp here are my html codes :

<html>
<head>
<title>::: GuestBook :::</title>
</head><body>

<!--JavaScript to filter certain words !-->
<script language="javascript" src="Codes.js"></script>

<center><form name="frmx">
<br><br><br>
<font face="arial" size="3" color="black">
<b>Thank you for visiting !</b></font>
<br><font face="arial" size="2"><br>
Pls. Take a moment to sign this guessbook<br>
Always hoping for your next visit thanks.<br><br>
Name : <input type=field name="fld1" cols="30"><br><br>
<textarea name="txt1" cols="30" rows="8">
</textarea><br>Previous Comments :<br>
<textarea readonly name="txt2" cols="30" rows="8"></textarea><br><br>
<input type="button" name="cmdbtn1" onclick="testMe()" value="Sign the Guestbook">
<input type="button" name="cmdbtn2" onclick="clearMe()" value="Clear Me">
</form></center>
</body>
</html>

Hoping for your responce :
simple21
simple21Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
You can use the following,  if you are using JSSE 1.4      
FileWriter fw = new FileWriter(filename,true);
0
simple21Author Commented:
I tried this but it does not work :

<html>
<head>
<title>::: GuestBook :::</title>
</head><body>

<script language="javascript" src="Codes.js"></script>
<center><form name="frmx">
<br><br><br>
<font face="arial" size="3" color="black">
<b>Thank you for visiting !</b></font>
<br><font face="arial" size="2"><br>
Pls. Take a moment to sign this guessbook<br>
Always hoping for your next visit thanks.<br><br>
Name : <input type=field name="fld1" cols="30"><br><br>
<textarea name="txt1" cols="30" rows="8">
</textarea><br>Previous Comments :<br>
<textarea readonly name="txt2" cols="30" rows="8"></textarea><br><br>
<input type="button" name="cmdbtn1" onclick="testMe()" value="Sign the Guestbook">
<input type="button" name="cmdbtn2" onclick="clearMe()" value="Clear Me">
</form></center>

<%@ page import="java.io.*"%>
<%
  String text = request.getParameter("txt1");
  try {
    FileWriter fw = new FileWriter(System.getProperty("user.dir") + "\\Sample.txt", true);
    fw.write(text);
    fw.flush();
    fw.close();
  }
  catch (IOException ioe) {
    out.println(ioe.toString());
  }
%>

</body>
</html>

How to a write the file after pressing the cmdbtn1 button?
What em I doing wrong ?
0
Mick BarryJava DeveloperCommented:
i'd implement the writing of the file in a servlewt and have your jsp form call that servlet.
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

sudhakar_koundinyaCommented:
i THINK YOU ARE EXPECTING SOME THING LIKE THIS.




<%@ page import="java.io.*"%>
<html>
<head>
<title>::: GuestBook :::</title>
</head><body>

<%!
   public String GetText(String file)
   {
         try
         {
            FileInputStream fin=new FileInputStream(file);
            if(fin.available()>0)
         {
            byte array[]=new byte[fin.available()];
            fin.read(array);
            fin.close();
            return new String(array);
         }
         else
         {
               return "";
         }
         }
         catch(Exception ex)
         {
               return "";
         }

   }
%>
<%
String textPath=getServletContext().getRealPath("/Sample.txt");
%>


<%
  String text = request.getParameter("txt1");
 String text2 = request.getParameter("fld1");
 if(text2==null) text2="Unknown";
 if(text==null)
text=  "";
text=text.trim();

{
  try {
        String text1=GetText(textPath).trim();

      FileOutputStream fout=new FileOutputStream(textPath);
      text=(text1+"\r\n"+text2 +" Says :"+text+"\r\n");
    fout.write(text.getBytes());
    fout.close();
  }
  catch (IOException ioe) {
    out.println(ioe.toString());
  }
  }
%>

<script language="javascript" src="Codes.js"></script>
<center><form name="frmx" action="a.jsp" method="post">
<br><br><br>
<font face="arial" size="3" color="black">
<b>Thank you for visiting !</b></font>
<br><font face="arial" size="2"><br>

Pls. Take a moment to sign this guessbook<br>
Always hoping for your next visit thanks.<br><br>
Name : <input type=field name="fld1" cols="30"><br><br>
<textarea name="txt1" cols="30" rows="8" >
</textarea><br>Previous Comments :<br>
<textarea readonly name="txt2" cols="30" rows="8" value=''><%=text%></textarea><br><br>

<input type="submit" name ="submit" value "submit">
</form></center>




</body>
</html>
0
rrzCommented:
Here is some quick and dirty JSP. I just want to demonstrate my idea. Please ask us any follow up questions you have about this.   Depending on your system,you might(I did not) have to manually create  the "Sample.txt" file.  "Sample.txt" will be located in your context's root directory.      rrz

<%@ page import="java.io.*"%>
<%
  String text = request.getParameter("txt1");
  String name = request.getParameter("fld1");
  if(name==null||name.trim().equals(""))name="anonymous";
  if(text!=null&&!text.trim().equals("")){
      try{
          FileWriter fw = new FileWriter(new File(application.getRealPath("Sample.txt")), true);
                fw.write(name + ": " + text + "\r\n");
                fw.flush();
                fw.close();
         } catch (IOException ioe) {}
  }
%>
<html>
<head>
<title>::: GuestBook :::</title>
</head><body>
<center><form name="frmx">
<font face="arial" size="3" color="black">
<b>Thank you for visiting !</b></font>
<br><font face="arial" size="2"><br>
Pls. Take a moment to sign this guestbook<br/>
Always hoping for your next visit thanks.<br/>
Name : <input type=field name="fld1" cols="30"/><br/>
<textarea name="txt1" cols="30" rows="8"/></textarea><br/>
Previous Comments :<br/>
<textarea readonly name="txt2" cols="30" rows="8">
<jsp:include page="Sample.txt"/></textarea><br/><br/>
<input type="submit" name="cmdbtn1" value="Sign the Guestbook"/>
</form></center>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simple21Author Commented:
rrz@871311;

Thanks but where do I put my Sample.txt ? I manually created it and put it on the same folder as my jsp file
it accepts the message but it doesnt display the message and it doesnt write the message in Sample.txt
I'm using Blazix as my Java Server, thanks.
0
simple21Author Commented:
sudhakar_koundinya;

It did not work it gave me an error :

Error(s) in compiling C:\Software\Blazix\webfiles\test\job4\Sample.jsp
C:\Software\Blazix\jspdir\test\job4\Sample_jsp.java:71: cannot resolve symbol
symbol  : method getServletContext ()
location: class desisoft_jsp_Sample_jsp1082849647292
String textPath=getServletContext().getRealPath("/Sample.txt");
                ^
1 error
0
rrzCommented:
>where do I put my Sample.txt ?  
I put it in my context's root folder. I am using Tomcat.
I used the following line to get it's path.
application.getRealPath("Sample.txt")  
This is where I put it.
Tomcat\webapps\myContext\Sample.txt  

I don't know anything about  Blazix    rrz
0
rrzCommented:
>C:\Software\Blazix\webfiles\test\job4\Sample.jsp
Is "test"  your context ?  
If so, then put  Sample.txt in the test folder.    

>cannot resolve symbol
>symbol  : method getServletContext ()  
this is a servlet method that returns the "application" implicit JSP object.  
rrz
0
simple21Author Commented:
>C:\Software\Blazix\webfiles\test\job4\Sample.jsp
>Is "test"  your context ?  

Its writing now but it does not display the word on the textarea
after submitting it. Its should display it right :

// This one should call it right but it doesnt ?
<textarea readonly name="txt2" cols="30" rows="8">
<jsp:include page="Sample.txt"/>

So, whats the problem ?

0
Mick BarryJava DeveloperCommented:
is Sample.txt in the same directory as your jsp?
0
simple21Author Commented:
Haha I figured it out . . .
0
Mick BarryJava DeveloperCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.