R
Rune Runnestø
Hi, I have made a small program that doesn't work quite the way it should.
It is a guestbook for the web, where visitors can write back their
greetings. The program consists of 3 files:
- guestbook.jsp -> this is the form
- writeToFile -> writing the captured data from the form to a file
- readFromFile -> reading all the greetings to the file guestbook.jsp
Here is the file Guestbook.jsp:
------------------------------
<!-- Guestbook.jsp -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor="wheat" text="darkgreen" link="red" vlink="steelblue"
alink="darkblue">
<h1>Guestbook</h1>
<form action="WriteToFile.jsp" method="POST" >
<p>
Name: <input type="text" name="namn" size="30" ><br><br>
Email: <input type="text" name="email" size="30" >
</p>
<p>
Write your comments here: <br>
<textarea type="textarea" name="comments" rows="4" cols="40">
</textarea>
</p>
<p>
<input type="submit" namn="Send" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>
<!-- This file below should not be included unless it is created.
How do I write a try-catch to assure this ?
-->
<jsp:include page="ReadFromFile.jsp" />
</body>
</html>
Here is the file WriteToFile.jsp:
------------------------------
<!--
WriteToFile.jsp
This file is activated by 'Guestbook.jsp'.
It catches data from the form, and write them to
the file. New messages are written as appendixes to
the existing ones.
-->
<%@ page import="java.io.*" %>
<%!
String relFilname = "allGreetings.txt";
String greeting = "";
%>
<a href="mailto:[email protected]">
<%
if (request.getParameter("Send") != null && session.getAttribute("saved")
== null) {
String filname = application.getRealPath(relFilname);
if(request.getAttribute("email") != null){ //the mail address is not
empty
greeting =
"From: " + request.getParameter("namn") +
" date received: " + new java.util.Date().toString()+ "\n" +
request.getParameter("comments") + "\n" +
"*****" + "\n" + "\n";
}else{ //the mail address is empty
greeting =
"From: " + request.getParameter("namn") +
" date received: " + new java.util.Date().toString()+ "\n" +
request.getParameter("comments") + "\n" +
"*****" + "\n" + "\n";
}
FileWriter writeConnToFile = new FileWriter(filname, true); //append
PrintWriter writer = new PrintWriter(new
BufferedWriter(writeConnToFile));
writer.print(greeting);
writer.close();
session.setAttribute("saved", "ok");
out.println("<p>Your greeting is now saved in the guestbook.</p>");
}
%>
<P><A HREF = "Guestbook.jsp">Back to the guestbook</A>
Here is the file ReadFromFile.jsp:
------------------------------
<!--
ReadFromFile.jsp
This file reads all the messages in the guestbook.
The file is called from the file 'Guestbook.jsp'.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>All the greetings in the guestbook</title>
</head>
<body bgcolor="wheat" text="darkgreen" link="red" vlink="steelblue"
alink="darkblue">
<%@ page import="java.io.*" %>
<p><strong>Registered visitors so far</strong><br><br></p>
<%!
String relFilname = "allGreetings.txt";
%>
<!-- open the file for reading -->
<%
String filname = application.getRealPath(relFilname);
FileReader readConnToFile = new FileReader(filname);
BufferedReader reader = new BufferedReader(readConnToFile);
String line = reader.readLine();
while(line != null){
out.println(line + "<br>");
line = reader.readLine();
}
reader.close();
%>
</body>
</html>
------------------
The problems encountered are:
1) The message "Your greeting is now saved in the Guestbook" does not
appear. How is that ?
2) The jsp:include of the file ReadFromFile.jsp to the file Guestbook.jsp
should only take place when the file "allGreetings.txt" is created. How do I
make the code for that ?
3) When the name of a person appears in the included file in the bottom of
the file "Guestbook.jsp", under "Registered visitors so far", it should be
possible to send an email to the person it the person has written the
emailaddress in the form. Just by cliking on the name. How do I code this
link. (something with <href mailto: I guess )?
Regards
Rune
It is a guestbook for the web, where visitors can write back their
greetings. The program consists of 3 files:
- guestbook.jsp -> this is the form
- writeToFile -> writing the captured data from the form to a file
- readFromFile -> reading all the greetings to the file guestbook.jsp
Here is the file Guestbook.jsp:
------------------------------
<!-- Guestbook.jsp -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor="wheat" text="darkgreen" link="red" vlink="steelblue"
alink="darkblue">
<h1>Guestbook</h1>
<form action="WriteToFile.jsp" method="POST" >
<p>
Name: <input type="text" name="namn" size="30" ><br><br>
Email: <input type="text" name="email" size="30" >
</p>
<p>
Write your comments here: <br>
<textarea type="textarea" name="comments" rows="4" cols="40">
</textarea>
</p>
<p>
<input type="submit" namn="Send" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>
<!-- This file below should not be included unless it is created.
How do I write a try-catch to assure this ?
-->
<jsp:include page="ReadFromFile.jsp" />
</body>
</html>
Here is the file WriteToFile.jsp:
------------------------------
<!--
WriteToFile.jsp
This file is activated by 'Guestbook.jsp'.
It catches data from the form, and write them to
the file. New messages are written as appendixes to
the existing ones.
-->
<%@ page import="java.io.*" %>
<%!
String relFilname = "allGreetings.txt";
String greeting = "";
%>
<a href="mailto:[email protected]">
<%
if (request.getParameter("Send") != null && session.getAttribute("saved")
== null) {
String filname = application.getRealPath(relFilname);
if(request.getAttribute("email") != null){ //the mail address is not
empty
greeting =
"From: " + request.getParameter("namn") +
" date received: " + new java.util.Date().toString()+ "\n" +
request.getParameter("comments") + "\n" +
"*****" + "\n" + "\n";
}else{ //the mail address is empty
greeting =
"From: " + request.getParameter("namn") +
" date received: " + new java.util.Date().toString()+ "\n" +
request.getParameter("comments") + "\n" +
"*****" + "\n" + "\n";
}
FileWriter writeConnToFile = new FileWriter(filname, true); //append
PrintWriter writer = new PrintWriter(new
BufferedWriter(writeConnToFile));
writer.print(greeting);
writer.close();
session.setAttribute("saved", "ok");
out.println("<p>Your greeting is now saved in the guestbook.</p>");
}
%>
<P><A HREF = "Guestbook.jsp">Back to the guestbook</A>
Here is the file ReadFromFile.jsp:
------------------------------
<!--
ReadFromFile.jsp
This file reads all the messages in the guestbook.
The file is called from the file 'Guestbook.jsp'.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>All the greetings in the guestbook</title>
</head>
<body bgcolor="wheat" text="darkgreen" link="red" vlink="steelblue"
alink="darkblue">
<%@ page import="java.io.*" %>
<p><strong>Registered visitors so far</strong><br><br></p>
<%!
String relFilname = "allGreetings.txt";
%>
<!-- open the file for reading -->
<%
String filname = application.getRealPath(relFilname);
FileReader readConnToFile = new FileReader(filname);
BufferedReader reader = new BufferedReader(readConnToFile);
String line = reader.readLine();
while(line != null){
out.println(line + "<br>");
line = reader.readLine();
}
reader.close();
%>
</body>
</html>
------------------
The problems encountered are:
1) The message "Your greeting is now saved in the Guestbook" does not
appear. How is that ?
2) The jsp:include of the file ReadFromFile.jsp to the file Guestbook.jsp
should only take place when the file "allGreetings.txt" is created. How do I
make the code for that ?
3) When the name of a person appears in the included file in the bottom of
the file "Guestbook.jsp", under "Registered visitors so far", it should be
possible to send an email to the person it the person has written the
emailaddress in the form. Just by cliking on the name. How do I code this
link. (something with <href mailto: I guess )?
Regards
Rune