/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
//import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author nmg
*/
public class Chat extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Chat</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Chat at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);???????????????????????
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
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(""))
{//如果没有输入名字则重新转到LoginChat
RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/LoginChat");
dispatcher.forward(request, response);
}
}
session.setAttribute("username", user);
//保存名字到Session
PrintWriter out=response.getWriter();
String words=(String)getServletConfig().getServletContext().getAttribute("Words");
//获取系统中原来的ServletContext
//如果ServletContext为空,则设置为系统提示:可以聊天
if (words==null)
{
getServletConfig().getServletContext().setAttribute("Words", "系统提示:可以聊天");
}
out.println("<html>");
out.println("<head>");
out.println("<title>聊天室</title>");
out.println("</head>");
out.println("<body>");
//注意类型转换
//不使用直接输出
//out.println((String)getServletConfig().getServletContext().getAttribute("Words"));
//使用文本框显示
out.println("<center>");
out.println("<form id=\"form1\" name=\"form1\" method=\"post\" action=\"Chat\">");
//发送信息使用本页面的post
out.println("<textarea name=\"textarea\" id=\"textarea\" cols=\"100\" rows=\"30\">");
out.println((String)getServletConfig().getServletContext().getAttribute("Words"));
out.println("</textarea>");
out.println("<hr />");
out.println("<input type=\"text\" name=\"newwords\" id=\"textfield\" />");
out.println("<input type=\"submit\" name=\"button\" id=\"button\" =\"提交\" />");
out.println("提示:不输入任何内容直接点击<提交>可以查看最新信息!");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("utf-8");
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")+" 说:");
String words=say+request.getParameter("newwords")+"\n"+getServletConfig().getServletContext().getAttribute("Words");
getServletConfig().getServletContext().setAttribute("Words", words);
String user=(String)session.getAttribute("username");
session.setAttribute("username", user);
//保存名字到Session
PrintWriter out=response.getWriter();
if (words==null)
{
getServletConfig().getServletContext().setAttribute("Words", "系统提示:可以聊天");
}
out.println("<html>");
out.println("<head>");
out.println("<title>聊天室</title>");
out.println("</head>");
out.println("<body>");
//注意类型转换
//不使用直接输出
//out.println((String)getServletConfig().getServletContext().getAttribute("Words"));
//使用文本框显示
out.println("<center>");
out.println("<form id=\"form1\" name=\"form1\" method=\"post\" action=\"Chat\">");
//发送信息使用本页面的post
out.println("<textarea name=\"textarea\" id=\"textarea\" cols=\"100\" rows=\"30\">");
out.println((String)getServletConfig().getServletContext().getAttribute("Words"));
out.println("</textarea>");
out.println("<hr />");
out.println("<input type=\"text\" name=\"newwords\" id=\"textfield\" />");
out.println("<input type=\"submit\" name=\"button\" id=\"button\" =\"提交\" />");
out.println("提示:不输入任何内容直接点击<提交>可以查看最新信息!");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
//doGet(request,response);
//}
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}