package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Chat extends HttpServlet {
public Chat() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gb2312");
HttpSession session=request.getSession(true);
String user=(String)session.getAttribute("username");
if (user==null || user.equals(""))
{
user=(String)request.getParameter("username");
if(user==null || user.equals(""))
{
RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/WebProject/servlet/TestServlet");
dispatcher.forward(request, response);
}
}
session.setAttribute("username", user);
PrintWriter out=response.getWriter();
String words=(String)getServletConfig().getServletContext().getAttribute("Words");
if (words==null)
{
getServletConfig().getServletContext().setAttribute("Words", "提示:大家可以畅所欲言了,但要注意说话文明哦!");
}
out.println("<html>");
out.println("<head>");
out.println("<title>欢迎进入徐聪聊天室</title>");
out.println("</head>");
//out.println("<meta http-equiv =\"refresh\" content=\"15; url= http://localhost:8888/WebProject/servlet/Chat\">");
out.println("<body bgcolor=\"#0000ff\" text=\"#ff0000\">");
out.println("<h2 align=\"center\">欢迎进入徐聪聊天室</h2>");
out.println("<center>");
out.println("<form id=\"form1\" name=\"form1\" method=\"post\" action=\"Chat\">");
out.println("<textarea name=\"textarea\" id=\"textarea\" cols=\"100\" rows=\"27\">");
out.println((String)getServletConfig().getServletContext().getAttribute("Words"));
out.println("</textarea>");
out.println("<hr />");
out.println("<textarea name=\"newwords\" id=\"textarea\" cols=\"100\" rows=\"10\">");
out.println("</textarea>");
out.println("<br />");
out.println("<input type=\"submit\" name=\"button\" id=\"button\" value=\"查 看\" />");
out.println("<input type=\"submit\" name=\"button\" id=\"button\" value=\"发 送\" />");
out.println("提示:在聊天之前请先查看一下信息可直接点击查看按钮!");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
HttpSession session=request.getSession(true);
if (request.getParameter("newwords")==null || request.getParameter("newwords").equals(""))
{
doGet(request,response);
}
else
{
String say=new String(session.getAttribute("username").toString().getBytes("iso8859_1"),"gbk")+" 说:";
String words=getServletConfig().getServletContext().getAttribute("Words")+"\n"+say+request.getParameter("newwords");
getServletConfig().getServletContext().setAttribute("Words", words);
doGet(request,response);
}
}
public void init() throws ServletException {
}
}