Chat Server Implementation Using JSP
Start.jsp:
<h2>Chat
Server</h2>
<form
method="post" action="login.jsp" >
UserName
: <input type="text" name="usr"
/><br><br>
Password
: <input type="password" name="pass"
/><br><br>
<input
type="button" value="Log In"
onclick="submit()"/>
</form>
Login.jsp:
<%@
page import="java.sql.*" %>
<%
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
String
usr=request.getParameter("usr");
String
pass=request.getParameter("pass");
try
{
Connection
con
=DriverManager.getConnection("jdbc:odbc:server","scott","tiger");
PreparedStatement
stmt=con.prepareStatement("select * from login where
usr='"+usr+"' and pass='"+pass+"'");
ResultSet
rs=stmt.executeQuery();
if(rs.next())
{ %>
<form
method="post" action="cboxs.jsp">
<input
type="hidden" name="flag"
value="<%=usr%>"/>
<input
type="button" value="Go to CHAT BOX - - >"
onclick="submit()"/>
</form>
<%}
else
{
%>
Login
failed<br>
<form
method="post" action="start.jsp" >
<input
type="button" value="< - - Go Back"
onclick="submit()"/>
</form>
<%
}
stmt.close();
}
catch(SQLException
se)
{
%>
Login
ERROR<br>
<form
method="post" action="start.jsp" >
<input
type="button" value="< - - Go Back"
onclick="submit()"/>
</form>
<%
}
%>
Cboxs.jsp:
<%@
page import="java.sql.*" %>
<%
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
String
flag=request.getParameter("flag");
try
{
Connection
con =DriverManager.getConnection("jdbc:odbc:server","scott","tiger");
PreparedStatement
stmt=con.prepareStatement("select * from msg where
uto='"+flag+"'");
ResultSet
rs=stmt.executeQuery();
%>
<form
method="post" action="writes.jsp" >
To :
<select name="uto">
<option value="abc"/>abc
<option
value="xyz"/>xyz
<option value="pqr"/>pqr
<option value="mno"/>mno
</select>Message : <input
type="text" name="msg" />
<input type="hidden"
name="flag" value="<%=flag%>" />
<input
type="button" value="Send Message"
onclick="submit()"/>
</form>
<form method="post"
action="start.jsp" >
<input type="button"
value="LogOut" onclick="submit()"/>
</form>
<form method="post"
action="cboxs.jsp" >
<input type="hidden"
name="flag" value="<%=flag%>" />
<input
type="button" value="Refresh Messages"
onclick="submit()"/>
</form>
<h3>Messages</h3><%
while(rs.next())
{
out.println(rs.getString(1)+"
: "+rs.getString(3)+"<br>");
}
stmt.close();
}
catch(SQLException
se)
{
%>
Database
ERROR
<%
}
%>
Writes.jsp:
<%@
page import="java.sql.*" %>
<%
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
String
flag=request.getParameter("flag");
String
msg=request.getParameter("msg");
String
uto=request.getParameter("uto");
try
{
Connection
con =DriverManager.getConnection("jdbc:odbc:server","scott","tiger");
PreparedStatement
stmt=con.prepareStatement("insert into msg
values('"+flag+"','"+uto+"','"+msg+"')");
int
rs=stmt.executeUpdate();
if(rs == 1)
out.println("written successfully");
%>
<br><form
method="post" action="cboxs.jsp" >
<input
type="hidden" name="flag"
value="<%=flag%>"/>
<input
type="button" value="< - - Go back to CHATBOX"
onclick="submit()"/>
</form>
<%
stmt.close();
}
catch(SQLException
se)
{
%>
Database
ERROR
<%
}
%>
Output:
Procedure to create and
insert values into table login:
SQL>
create table login (usr varchar2(20),pass varchar2(20));
Table
created.
SQL>
insert into login values('abc','abc');
1
row created.
SQL>
insert into login values('xyz','xyz');
1
row created.
SQL>
commit;
Commit
complete.
Procedure to create and
insert values into table msg:
SQL>
create table msg(ufrom varchar2(20),uto varchar2(20),msg varchar2(30));
Table
created.
SQL>
commit;
Commit
complete.
Output
in msg table:
SQL> select * from
msg;
UFROM UTO MSG
--------------------
-------------------- --------------------
abc xyz hie