• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send message servlet

 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing my coursework, it is web site like facebook. Now I need to make it to exchange with messages between different users but unfortunalely I have no Idea how to write code for this. If anyone have done it before, please give me the code!
Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone have done it before, please give me the code!

No, that's not how JavaRanch works. What have you done so far? If you don't have anything yet, what ideas have you had about implementing it?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remember coderanch is NotACodeMill. We particularly discourage supplying canned answers for course work.

So, what have you written so far and where are you stuck?
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
If anyone have done it before, please give me the code!

No, that's not how JavaRanch works. What have you done so far? If you don't have anything yet, what ideas have you had about implementing it?


I have done a lot, I can send the site would like to.
I ve doene login/logour, registration and search (search DOESNT WORK yet actually), database is ready, working properly. I just dont know how to do it, how sending messages works,
I have some idea that I write message and send it to aanother user's message database but notifications and stuff like this I don't know!
I just wanted some help, not just get code and by doing nothing get marks!
Thank you!
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For messaging, you dont need to develop a protocol like SMTP. The simplest concept would be. When a user sends a message to another, store the message in a database, if you want notifications, send a mail to the receiving user about new message. If you have developed other parts like login/search and other stuff, you should have no issue implementing this.
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no problem with it, I mean If I need to use some java classes, import something.
I don't really understand this: "if you want notifications, send a mail to the receiving user about new message."
What do you mean? Email? It s good point if I can't make other notification, but for that I need a servlet which sends this notification another problem.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ibragim Gapuraev wrote:Yes, there is no problem with it, I mean If I need to use some java classes, import something.
I don't really understand this: "if you want notifications, send a mail to the receiving user about new message."
What do you mean? Email? It s good point if I can't make other notification, but for that I need a servlet which sends this notification another problem.


Have you developed other parts of your application, login/search in Java ?
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have. I write it above!
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ibragim Gapuraev wrote:Yes I have. I write it above!

Then what's your issue ? Do you have issues saving the messages to database, or writing servlets, or what ?
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! New question about the topic! I wrote next java classes and jsp:
These codes for inbox messages: I wanna that when user goes to inbox link he sees inbox messages, but it keeps giving me nex error:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /inbox.jsp at line 80

77: site.Message[] mess = (Message[]) request.getAttribute("table");
78:
79: int num = 0;
80: for (int i = 0; i < mess.length; i++) {
81: num++;
82: %>
83: <%= "<tr>"%>

Here is the code:
Database acsess for messages:

public Message[] readMessage(Message mess) throws ServletException {

{
try {

PreparedStatement b = this.connection.prepareStatement(
"SELECT"
+ " fromname,"
+ " toname, "
+ " date, "
+ " messagetext, "
+ " FROM sas.messages WHERE username=?");


b.setString(1, mess.getFromuser());
b.setString(2, mess.getTouser());
b.setString(3, mess.getDate());
b.setString(4, mess.getMessage());


ResultSet resultSet = b.executeQuery();

ArrayList<Message> collecMess = new ArrayList<Message>();


while (resultSet.next()) {
Message sqlMess = new Message();

sqlMess.setFromuser(resultSet.getString("fromname"));
sqlMess.setTouser(resultSet.getString("toName"));
sqlMess.setDate(resultSet.getString("date"));
sqlMess.setMessage(resultSet.getString("message"));



collecMess.add(sqlMess);
}



Message[] resultMess = collecMess.toArray(new Message[collecMess.size()]);


return resultMess;
} catch (SQLException e) {
throw new ServletException("sql exception", e);
}
}
}
servlet readMess:
public class readMess extends HttpServlet {



@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Message mess = new Message();

mess.setUsername(request.getParameter("username"));

mess.setFromuser(request.getParameter("fromuser"));

mess.setTouser(request.getParameter("touser"));

mess.setDate(request.getParameter("date"));

mess.setMessage((request.getParameter("message")));



DatabaseAccess DataBaseCon = new DatabaseAccess();

RequestDispatcher view = request.getRequestDispatcher("inbox.jsp");

request.setAttribute("ifTrue", DataBaseCon.readMessage(mess));

view.forward(request, response);
}
and jsp:
<form id="read_mess" action="readMess" method="post"></form>
<table border="1">
<tr>

<th>Username</th>
<th>From</th>
<th>To</th>
<th>Message</th>

</tr>


<%
site.Message[] mess = (Message[]) request.getAttribute("table");

int num = 0;
for (int i = 0; i < mess.length; i++) {
num++;
%>
<%= "<tr>"%>
<%= "<td align=\"center\">"%><%= mess[i].getUsername()%><%= "</td>"%>
<%= "<td align=\"center\">"%><%= mess[i].getFromuser()%><%= "</td>"%>
<%= "<td align=\"center\">"%><%= mess[i].getTouser()%><%= "</td>"%>
<%= "<td align=\"center\">"%><%= mess[i].getMessage()%><%= "</td>"%>
<%= "</tr>"%>
<% }%>
</table>


</div>


</body>

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could checkout the java code generated for your JSP to find the problem. The java file gets placed here(assuming you are using tomcat):

<tomcat>/work/Catalina/localhost/WebApp/org/apache/jsp/web
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya Maheshwari wrote:You could checkout the java code generated for your JSP to find the problem. The java file gets placed here(assuming you are using tomcat):

<tomcat>/work/Catalina/localhost/WebApp/org/apache/jsp/web

Sorry, but I did not understand, you saw JSP page, it seems there is no error.
I did the same when I did search, it gets inform from database in the same way. but, in this case it doesnt, work!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic