📄 getchatdo.java
字号:
package com.lovo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
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;
import com.lovo.po.PublicChatDO;
import com.lovo.po.UserPO;
public class GetChatDO extends HttpServlet{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
ServletContext application=this.getServletContext();
HttpSession session=request.getSession();
PrintWriter out = response.getWriter();
/**得到最大显示行数*/
String s= application.getInitParameter("maxRows");
int maxRows=Integer.parseInt(s);
List<PublicChatDO> chatList=(List<PublicChatDO>)application.getAttribute("chatList");
if(chatList==null){
chatList=new ArrayList<PublicChatDO>();
application.setAttribute("chatList", chatList);
}
if(chatList.size()>maxRows){
for(int i=0;i<chatList.size()-maxRows+1;i++){
chatList.remove(i);
}
}
String currentUser=((UserPO)session.getAttribute("user")).getName();
String sendContent="";
for(int i=0;i<chatList.size();i++){
String sendUser=((UserPO)((PublicChatDO)chatList.get(i)).getSendUser()).getName();
String publicChat=((PublicChatDO)chatList.get(i)).getPublicChat();
if(currentUser.equals(sendUser)){
sendContent=sendContent+"你说: "+publicChat+"<br>";
}
else{
sendContent=sendContent+currentUser+"说: "+publicChat+"<br>";
}
}
// System.out.println(currentUser+" "+sendContent);
out.print(sendContent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -