使用servletcontent记录当前登录用户信息

第一张jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
      <%@page import="java.util.Vector"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="a1servlet" method="get">
<input type="text" name="first"/>
<input type="hidden" name="th"value="a"/>
<input type="submit" value="发表"/>
</form>
<br/>


</body>

</html>



第二张


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.util.Vector"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>


<script>
function comm(a){
  document.getElementsByName("ifr1").style.display="show";
  a.style.display="block";

}


</script>


<style>
body{
width:1000px;
}
.left{
width:50%;
float:left;
}
.right{
width:50%;
float:left;
}
</style>
</head>
<body>
<div class="left">
登陆成功<br/>
当前在线用户<br/>
<%
//response.setIntHeader("refresh", 5);


ServletContext context=getServletContext();
Vector hab=(Vector)context.getAttribute("body");
for(int i=0;i<hab.size();i++){
%>
<a href="NewFile2.jsp?name=<%=hab.get(i)%>" target="ifr1"><%=hab.get(i) %></a>
<br/>
<%


}
%>
</div>
<div class="right">
<iframe name="ifr1" id="ifr1" width="50%" align="top" scrolling="no" frameborder="0">


</iframe>
</div>

</body>


servlet操作类·



import java.io.IOException;
import java.util.Vector;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class a1servlet
 */  


public class a1servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public a1servlet() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("GBK");
//response.setContentType("GBK");
ServletContext hab = this.getServletContext();
String first=request.getParameter("first");

Vector go = (Vector)hab.getAttribute("body");

 
go.add(first);

       //request.getRequestDispatcher("NewFile1.jsp").forward(request, response);
       response.sendRedirect("NewFile1.jsp");

}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}


@Override
public void destroy() {
// TODO Auto-generated method stub
//super.destroy();


}


@Override
public void init() throws ServletException {
// TODO Auto-generated method stub


     
        ServletContext context=this.getServletContext();     
        //判断信息列表存在
        if(context.getAttribute("body")==null){
            Vector message=new Vector();
            context.setAttribute("body", message);
        }
       
}


}

猜你喜欢

转载自blog.csdn.net/qq_37069064/article/details/79588383