• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

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
 before loginThe register button would just capture the username and password, no need for another page.

After Login
 after login
I'm thinking I only need three services, login.jsp, logoff.jsp, and post.jsp

Any help appreciated!
-Thmstec
0
Thmstec
Asked:
Thmstec
  • 22
  • 16
1 Solution
 
rrzCommented:
How are you persisting your data ? Database ? Files ?
0
 
ThmstecAuthor Commented:
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?
0
 
rrzCommented:
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  ?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ThmstecAuthor Commented:
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.
0
 
rrzCommented:
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  ?
0
 
ThmstecAuthor Commented:
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.
0
 
rrzCommented:
Are you going to create a separate web app in Tomcat for this ?
Do you already know how to do that ?
0
 
ThmstecAuthor Commented:
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.
0
 
rrzCommented:
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.  

0
 
ThmstecAuthor Commented:
I've got 3 hours wide open.
0
 
rrzCommented:
>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.
0
 
ThmstecAuthor Commented:
The next three hours**
0
 
rrzCommented:
Do you have any  HTML already coded ?
0
 
ThmstecAuthor Commented:
no
0
 
rrzCommented:
Ok, I will start coding a bare bones page to get us started. I will try to post something in 30 or 40 minutes.  
0
 
ThmstecAuthor Commented:
Thanks a bunch!
0
 
rrzCommented:
What is your Tomcat version ?  6? 7?
0
 
ThmstecAuthor Commented:
7
0
 
rrzCommented:
I'm still working.  
0
 
rrzCommented:
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>

Open in new window

0
 
ThmstecAuthor Commented:
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?
0
 
rrzCommented:
I am still working on the password, log in, log out part.  I will post new version soon.
0
 
rrzCommented:
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 ?
<%@ 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>

Open in new window

0
 
rrzCommented:
Try this one. I still haven't finished the comment storage and display.
<%@ 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("<input type='submit' name='logOff' value='Log Off' /><br/>");
             out.print("Enter comment: <input type='text' name='comment' />");
             out.print("<input type='submit' name='postComment' value='POST' />");
         }
  out.print("</form>");
%>
</body>
</html>

Open in new window

0
 
ThmstecAuthor Commented:
I'll be on for another 20 min or so. Thanks for all your work thus far.
0
 
rrzCommented:
Did you test ?
0
 
ThmstecAuthor Commented:
Yea the login, logout, and register work just the way i wanted.
0
 
rrzCommented:
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   $$*$$
0
 
ThmstecAuthor Commented:
yea, simple is good.
0
 
rrzCommented:
Ok I'll finish in the morning. (8-10 hours from now).
0
 
ThmstecAuthor Commented:
Thanks for your help tonight!
0
 
rrzCommented:
Thanks for the points. Does that mean I done here ? I am working on comment part.
0
 
rrzCommented:
Almost got it.  I have it working for a one line text box. I guess you want a textarea  with multilines ?
0
 
rrzCommented:
What are your target browsers ? I am testing in IE8
0
 
ThmstecAuthor Commented:
Target browsers: Latest chrome, ie, and firefox. text area with space for 300 char
0
 
ThmstecAuthor Commented:
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.
0
 
rrzCommented:
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.
<%@ 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>

Open in new window

0
 
rrzCommented:
I should have added a check during registration to see if a username was already being used.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 22
  • 16
Tackle projects and never again get stuck behind a technical roadblock.
Join Now