2013-06-26
展开全部
先做一个用户交流界面 参考代码 你可以参考<body>里面的关键代码request.getContextPath();
<head>
<title></title>
</head>
<body>
<form action="servlet/serverMyJsp" method="post">
<textarea rows="20" cols="50" ><%=application.getAttribute("message") %></textarea><br>
<input type="text" name="message">
<input type="submit" value="发送 ">
</form>
</body>
</html>
再做一个程序来处理客户的交流信息可以参考dopost()方法里的代码
package server;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 serverMyJsp extends HttpServlet {
public serverMyJsp() {
super();
}
public void destroy() {
super.destroy();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setCharacterEncoding("gbk");
request.setCharacterEncoding("gbk");
//String demo=request.getParameter("message");
// String str=new String(demo.getBytes("Iso-885-1"),"gbk()");
ServletContext sc=this.getServletContext();
String uname=(String) request.getSession().getAttribute("aa");
System.out.println(uname);
String amessage=(String) sc.getAttribute("message");
sc.setAttribute("message", amessage+uname+"说:"+request.getParameter("message")+"\r\n");
response.sendRedirect("../MyJsp.jsp");
}
public void init() throws ServletException {
}
}
<head>
<title></title>
</head>
<body>
<form action="servlet/serverMyJsp" method="post">
<textarea rows="20" cols="50" ><%=application.getAttribute("message") %></textarea><br>
<input type="text" name="message">
<input type="submit" value="发送 ">
</form>
</body>
</html>
再做一个程序来处理客户的交流信息可以参考dopost()方法里的代码
package server;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 serverMyJsp extends HttpServlet {
public serverMyJsp() {
super();
}
public void destroy() {
super.destroy();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setCharacterEncoding("gbk");
request.setCharacterEncoding("gbk");
//String demo=request.getParameter("message");
// String str=new String(demo.getBytes("Iso-885-1"),"gbk()");
ServletContext sc=this.getServletContext();
String uname=(String) request.getSession().getAttribute("aa");
System.out.println(uname);
String amessage=(String) sc.getAttribute("message");
sc.setAttribute("message", amessage+uname+"说:"+request.getParameter("message")+"\r\n");
response.sendRedirect("../MyJsp.jsp");
}
public void init() throws ServletException {
}
}
2013-06-26
展开全部
用AJAX比较好,其实直接用AJAX底层XmlHttpRequest实现最好
也不难,纯JAVASCRIPT就搞掂
聊天如果不断刷新那就真是。。
也不难,纯JAVASCRIPT就搞掂
聊天如果不断刷新那就真是。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-26
展开全部
用session和application能实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询