Thmstec
asked on
Need help making simple comment form with jsp
Hello,
I'm trying to create a simple website for a friend. I'm having trouble creating a registration, login, and comment system. We're using a tomcat server for the site. I don't know much JSP, so something to help get me going would be very helpful. There isn't much need for it to be secure, only data kept will be username and password, date and comment.
Here are two images of what I'm trying to make it look like
Before Login
The register button would just capture the username and password, no need for another page.
After Login
I'm thinking I only need three services, login.jsp, logoff.jsp, and post.jsp
Any help appreciated!
-Thmstec
I'm trying to create a simple website for a friend. I'm having trouble creating a registration, login, and comment system. We're using a tomcat server for the site. I don't know much JSP, so something to help get me going would be very helpful. There isn't much need for it to be secure, only data kept will be username and password, date and comment.
Here are two images of what I'm trying to make it look like
Before Login
The register button would just capture the username and password, no need for another page.
After Login
I'm thinking I only need three services, login.jsp, logoff.jsp, and post.jsp
Any help appreciated!
-Thmstec
How are you persisting your data ? Database ? Files ?
ASKER
I'm not sure the best way to do it. I suppose files. I don't have any experience with MySQL or any other database. Unless we could somehow do it in XML?
If you want to use files, then that will work. Will have an admin to manually edit files if it becomes necessary ?
Will this be a public page ? No email address or conformation necessary ?
Will this be a public page ? No email address or conformation necessary ?
ASKER
I don't think we'll have a need to edit the files. If and when the page becomes obsolete or if the comments go awry we'll just take it down.
It'll be on a private network, no email address or other confirmation.
It'll be on a private network, no email address or other confirmation.
Do you know JSTL or do you want to use JSP for all the display logic ?
>I'm thinking I only need three services, login.jsp, logoff.jsp, and post.jsp
It might be easier to use one page to start.
Call it forum.jsp ?
>I'm thinking I only need three services, login.jsp, logoff.jsp, and post.jsp
It might be easier to use one page to start.
Call it forum.jsp ?
ASKER
Honestly, I don't know what JSTL even stands for. (until after I post this and go to google)
This "forum" page will be on the index page. I just thought I needed those little services to grab the form data.
This "forum" page will be on the index page. I just thought I needed those little services to grab the form data.
Are you going to create a separate web app in Tomcat for this ?
Do you already know how to do that ?
Do you already know how to do that ?
ASKER
I was going to keep it in the same app as everything else. And I "THINK" I know how to do that, but this is my first experience with ecplise and tomcat. I've been reading through tutorials all weekend.
Ok, lets keep it as simple as possible for a start. How is your schedule for today(tonight) ? I have some time to do some coding from now until I go to bed in 4 hours.
ASKER
I've got 3 hours wide open.
>I was going to keep it in the same app as everything else.
What is everything else ?
If we use index.jsp as the page then it will come up when you browse to the web app.
What is everything else ?
If we use index.jsp as the page then it will come up when you browse to the web app.
ASKER
The next three hours**
Do you have any HTML already coded ?
ASKER
no
Ok, I will start coding a bare bones page to get us started. I will try to post something in 30 or 40 minutes.
ASKER
Thanks a bunch!
What is your Tomcat version ? 6? 7?
ASKER
7
I'm still working.
I guess I am slower than I thought I was. Anyway I have a start. All it does so far is accept a username and password and put into the file. To test this code, make a file called passwords.properties and place it into your web app's WEB-INF folder. I am going take a break to eat something. I'll be back in a hour.
<%@ page import="java.util.*,java.io.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String logIn = request.getParameter("logIn");
String register = request.getParameter("register");
if(register != null){
if(username != null && !"".equals(username.trim()) && password != null && !"".equals(password.trim())){
FileWriter fw = new FileWriter(application.getRealPath("/WEB-INF/passwords.properties"), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(username + "=" + password);
bw.close();
fw.close();
}
}
Properties props = new Properties();
props.load(new FileInputStream(application.getRealPath("/WEB-INF/passwords.properties")));
String storedPassword = null;
if(username != null){
storedPassword = props.getProperty("username");
}
if(username != null && password != null){
if(password.equals(storedPassword))session.setAttribute("loggedIn", "yes");
}
%>
<html>
<body>
<%
if(session.getAttribute("loggedIn") == null && register == null){
out.print("<form>");
out.print("username<input type='text' name='username' />");
out.print("password<input type='text' name='password' />");
out.print("username<input type='submit' name='logIn' value='LOGIN' />");
out.print("username<input type='submit' name='register' value='REGISTER' />");
out.print("</form>");
} else if("yes".equals((String)session.getAttribute("loggedIn"))){
out.print("Hello" + (String)session.getAttribute("username"));
}
%>
</body>
</html>
ASKER
Just got back from a food break myself. Ok, I had no idea about this filewriter stuff. I'll tinker around with this, not 100% where to go to get the comments going. I suppose we'll use another filewriter for that though?
I am still working on the password, log in, log out part. I will post new version soon.
Here is what I have so far. Please test it. Remember you have to have the file in place.
How long are you going to be online ?
How long are you going to be online ?
<%@ page import="java.util.*,java.io.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String logIn = request.getParameter("logIn");
String register = request.getParameter("register");
String logOff = request.getParameter("logOff");
boolean registered = false;
if(logOff != null){
session.removeAttribute("loggedIn");
session.removeAttribute("username");
}
if(register != null){
if(username != null && !"".equals(username.trim()) && password != null && !"".equals(password.trim())){
FileWriter fw = new FileWriter(application.getRealPath("/WEB-INF/passwords.properties"), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(username + "=" + password);
bw.close();
fw.close();
registered = true;
}
}
Properties props = new Properties();
props.load(new FileInputStream(application.getRealPath("/WEB-INF/passwords.properties")));
String storedPassword = null;
if(username != null){
storedPassword = props.getProperty(username);
}
if(username != null && password != null){
if(password.equals(storedPassword)){
session.setAttribute("loggedIn", "yes");
session.setAttribute("username", username);
}
}
%>
<html>
<body>
<%
out.print("<form method='post'>");
if(session.getAttribute("loggedIn") == null && !registered){
out.print("username<input type='text' name='username' />");
out.print("password<input type='text' name='password' />");
out.print(" <input type='submit' name='logIn' value='LOGIN' />");
out.print(" <input type='submit' name='register' value='REGISTER' />");
} else if("yes".equals((String)session.getAttribute("loggedIn"))){
out.print("Hello" + (String)session.getAttribute("username"));
out.print("Log Off<input type='submit' name='logOff' value='Log Off' />");
}
out.print("</form>");
%>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'll be on for another 20 min or so. Thanks for all your work thus far.
Did you test ?
ASKER
Yea the login, logout, and register work just the way i wanted.
Good.
I am designing the comment part now.
Do you want it to be just a simple file ?
Maybe just
username [boundary]date [boundary] comment
newline
username [boundary]date [boundary] comment
newline
where boundary could be something simple like $$*$$
I am designing the comment part now.
Do you want it to be just a simple file ?
Maybe just
username [boundary]date [boundary] comment
newline
username [boundary]date [boundary] comment
newline
where boundary could be something simple like $$*$$
ASKER
yea, simple is good.
Ok I'll finish in the morning. (8-10 hours from now).
ASKER
Thanks for your help tonight!
Thanks for the points. Does that mean I done here ? I am working on comment part.
Almost got it. I have it working for a one line text box. I guess you want a textarea with multilines ?
What are your target browsers ? I am testing in IE8
ASKER
Target browsers: Latest chrome, ie, and firefox. text area with space for 300 char
ASKER
You can stop if you want, I think I've figured out how to do the rest, just need to sit down for a couple hours and do it. Though you can post whatever code you have now so I can use it if I hit anymore roadblocks.
I finished what I was trying to do. The biggest problem I had was with linebreaks. I couldn't figure out how to use CSS to tell the textarea element not to send linebreaks entered by user. So I removed them at the server(see line 40).
If you know how using CSS or HTML, then please share.
If you know how using CSS or HTML, then please share.
<%@ page import="java.util.*,java.io.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String logIn = request.getParameter("logIn");
String register = request.getParameter("register");
String logOff = request.getParameter("logOff");
String comment = request.getParameter("comment");
boolean registered = false;
if(logOff != null){
session.removeAttribute("loggedIn");
session.removeAttribute("username");
}
if(register != null){
if(username != null && !"".equals(username.trim()) && password != null && !"".equals(password.trim())){
FileWriter fw = new FileWriter(application.getRealPath("/WEB-INF/passwords.properties"), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(username + "=" + password);
bw.close();
fw.close();
registered = true;
}
}
Properties props = new Properties();
props.load(new FileInputStream(application.getRealPath("/WEB-INF/passwords.properties")));
String storedPassword = null;
if(username != null){
storedPassword = props.getProperty(username);
}
if(username != null && password != null){
if(password.equals(storedPassword)){
session.setAttribute("loggedIn", "yes");
session.setAttribute("username", username);
}
}
if(session.getAttribute("loggedIn") != null && comment != null && !"".equals(comment.trim())){
FileWriter fw2 = new FileWriter(application.getRealPath("/WEB-INF/comments.txt"), true);
BufferedWriter bw2 = new BufferedWriter(fw2);
comment = comment.replaceAll(System.getProperty("line.separator")," ");// remove line breaks
bw2.newLine();
bw2.write(session.getAttribute("username") + "$$b$$" + new Date() + "$$b$$" + comment);
bw2.close();
fw2.close();
}
ArrayList<String[]> entrys = new ArrayList<String[]>();
FileReader fr = new FileReader(application.getRealPath("/WEB-INF/comments.txt"));
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine()) != null){
String[] entryArray = line.split("\\$\\$b\\$\\$", 3); //split line at $$b$$
entrys.add(entryArray);
}
%>
<html>
<body>
<%
out.print("<form method='post'>");
if(session.getAttribute("loggedIn") == null && !registered){
out.print("username<input type='text' name='username' />");
out.print("password<input type='text' name='password' />");
out.print(" <input type='submit' name='logIn' value='LOGIN' />");
out.print(" <input type='submit' name='register' value='REGISTER' />");
} else if("yes".equals((String)session.getAttribute("loggedIn"))){
out.print("Hello " + (String)session.getAttribute("username"));
out.print("<input type='submit' name='logOff' value='Log Off' /><br/>");
}
out.print("</form>");
out.print("<hr/>");
for(int x=0; x < entrys.size(); x++){
String[] arrayLine = entrys.get(x);
if(arrayLine.length == 3){
out.print(arrayLine[0] + "<br/>");
out.print(arrayLine[1] + "<br/>");
out.print(arrayLine[2] + "<hr/>");
}
}
if("yes".equals((String)session.getAttribute("loggedIn"))){
out.print("<form method='post'>");
out.print("Enter comment: <textarea name='comment' cols='25' rows='5' wrap='soft'></textarea>");
out.print("<input type='submit' name='postComment' value='POST' />");
out.print("</form>");
}
%>
</body>
</html>
I should have added a check during registration to see if a username was already being used.