程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JavaEE在耳目數治理體系

JavaEE在耳目數治理體系

編輯:關於JAVA

JavaEE在耳目數治理體系。本站提示廣大學習愛好者:(JavaEE在耳目數治理體系)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaEE在耳目數治理體系正文


這篇博客是filter、listener和servlet技巧的相干總結,完成了簡略的顯示在耳目數、在耳目具體信息、治理員踢人功效

上面是具體代碼

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name> 
 <filter>
 <filter-name>character</filter-name>
 <filter-class>cn.hncu.filter.CharacterFilter</filter-class>
 <init-param>
  <param-name>charset</param-name>
  <param-value>utf-8</param-value>
 </init-param>
 </filter>
 <filter>
 <filter-name>login</filter-name>
 <filter-class>cn.hncu.filter.LoginFilter</filter-class>
 </filter>

 <filter-mapping>
 <filter-name>character</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>login</filter-name>
 <url-pattern>/servlet/*</url-pattern>
 <url-pattern>/jsps/*</url-pattern>
 <dispatcher>REQUEST</dispatcher>
 <dispatcher>FORWARD</dispatcher>
 </filter-mapping>


 <listener>
 <listener-class>cn.hncu.listener.MySessionListener</listener-class>
 </listener>

 <servlet>
 <servlet-name>LoginServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.LoginServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>ShowServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.ShowServlet</servlet-class>
 </servlet>
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>KickOutServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.KickOutServlet</servlet-class>
 </servlet>
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>LoginOutServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.LoginOutServlet</servlet-class>
 </servlet>



 <servlet-mapping>
 <servlet-name>LoginServlet</servlet-name>
 <url-pattern>/LoginServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>ShowServlet</servlet-name>
 <url-pattern>/servlet/ShowServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>KickOutServlet</servlet-name>
 <url-pattern>/servlet/KickOutServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>LoginOutServlet</servlet-name>
 <url-pattern>/servlet/LoginOutServlet</url-pattern>
 </servlet-mapping>


 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>在耳目信息治理</title>
 </head>

 <body>
 <h2>在耳目信息治理</h2>
 <h3>會員登錄</h3>
 <c:if test="${ empty sessionScope.user.name}" var="boo">
  <form action='<c:url value="/LoginServlet"></c:url>'>
   姓名:<input type="text" name="name"><br/>
    <input type="submit" value="登錄"><br/>
  </form>
 </c:if>
 <c:if test="${!boo}" >
  迎接回來,${ sessionScope.user.name}
  <c:if test="${ sessionScope.user.admin}" var="bo">
   治理員
  </c:if>
  <c:if test="${!bo}" >
   會員
  </c:if>
 </c:if>
 <br/>
  <c:if test="${!boo}" var="boo">
  <a href='<c:url value="/servlet/ShowServlet"/>'>檢查在耳目信息</a><br/>
  <a href='<c:url value="/servlet/LoginOutServlet"/>'>平安加入</a><br/>
  </c:if>

 </body>
</html>

/jsps/show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在耳目信息</title>
<style type="text/css">
 table {
  color: green;
  border: 1px solid blue;
  border-collapse: collapse;
  width: 500px;
  margin: auto;
 }
 td {
  border: 1px solid blue;
 }
 th {
  border: 1px solid blue;
 }
 body {
  text-align: center;
 }
</style>
 <meta http-equiv="refresh" content="3">
</head>

<body>
 <table>
  <tr>
   <th>姓名</th>
   <th>上線時光</th>
   <th>最初拜訪時光</th>
   <th>ip</th>
   <th>操作</th>
  </tr>

  <c:forEach items="${requestScope.onlines}" var="online">
  <tr>
   <td>
    <c:if test="${!empty online.user }" var="boo">
      ${online.user.name }
    </c:if>
    <c:if test="${empty online.user }">
      旅客
    </c:if>
   </td>
   <td>${online.createTime }</td>
   <td>${online.lastAccessedTime }</td>
   <td>${online.ip }</td>
   <td>
    <c:if test="${online.user!=sessionScope.user }"> 
     <c:if test="${!online.user.admin&&boo}">
       <a href='<c:url value="/servlet/KickOutServlet?id=${online.id }"/>' >踢出</a>
     </c:if>
    </c:if>
   </td>
  </tr>
  </c:forEach>
 </table>
</body>
</html>

User.java(值對象)

package cn.hncu.domain;

public class User {
 private String name;
 private boolean admin;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public boolean isAdmin() {
  return admin;
 }
 public void setAdmin(boolean admin) {
  this.admin = admin;
 }
 @Override
 public String toString() {
  return "User [name=" + name + ", admin=" + admin + "]";
 }
}

MySessionListener.java(監聽器)

package cn.hncu.listener;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener{

 @Override
 public void sessionCreated(HttpSessionEvent se) {
  Map<String, HttpSession> onlines= (Map<String, HttpSession>) se.getSession().getServletContext().getAttribute("onlines");
  if(onlines==null){
   onlines=Collections.synchronizedMap(new HashMap<String, HttpSession>());//對map停止加上同步鎖
   se.getSession().getServletContext().setAttribute("onlines",onlines);
  }
  onlines.put(se.getSession().getId(), se.getSession());
 }

 @Override
 public void sessionDestroyed(HttpSessionEvent se) {
  Map<String, HttpSession> onlines= (Map<String, HttpSession>) se.getSession().getServletContext().getAttribute("onlines");
  if(onlines.containsKey(se.getSession().getId())){
   onlines.remove(se.getSession().getId());
  }
 }

}

CharacterFilter.java(字符過濾器)

package cn.hncu.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CharacterFilter implements Filter {
 private String charset;
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
  charset=filterConfig.getInitParameter("charset");
 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  request.setCharacterEncoding(charset);
  response.setCharacterEncoding(charset);
  HttpServletRequest req=(HttpServletRequest) request;
  if(req.getSession().getAttribute("ip")==null){
   req.getSession().setAttribute("ip", req.getRemoteAddr());
  }
  chain.doFilter(request, response);
 }

 @Override
 public void destroy() {
 }

}

LoginFilter.java(避免旅客亂拜訪過濾器)

package cn.hncu.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

 @Override
 public void init(FilterConfig filterConfig) throws ServletException {

 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  HttpServletRequest req=(HttpServletRequest) request;
  if(req.getSession().getAttribute("user")==null){
   ((HttpServletResponse)response).sendRedirect(req.getContextPath()+"/index.jsp");
   return;
  }
  chain.doFilter(request, response);
 }

 @Override
 public void destroy() {
 }

}

LoginServlet.java

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hncu.domain.User;

public class LoginServlet extends HttpServlet {


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String name=request.getParameter("name");
  User user=(User) request.getSession().getAttribute("user");
  if (user==null&&name!=null&&name.trim().length()>0) {//封裝User
   user = new User();
   user.setName(name);
   if(name.equals("admin")){
    user.setAdmin(true);
   }
  }
  request.getSession().setAttribute("user", user);
  response.sendRedirect(request.getContextPath()+"/index.jsp");
 }

}

ShowServlet.java

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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 ShowServlet extends HttpServlet {


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }


 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Map<String, HttpSession> onlines=(Map<String, HttpSession>) request.getServletContext().getAttribute("onlines");
  List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();//吧Online封裝成list
  if (onlines!=null) {
   Iterator<HttpSession> it = onlines.values().iterator();
   while (it.hasNext()) {
    HttpSession s = it.next();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", s.getId());
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    map.put("createTime", sdf.format(new Date(s.getCreationTime())));
    map.put("lastAccessedTime",sdf.format(new Date(s.getLastAccessedTime())) );
    map.put("ip", s.getAttribute("ip"));//ip信息在characterFilter中補
    map.put("user", s.getAttribute("user"));
    list.add(map);
   }
  }
  request.setAttribute("onlines", list);
  request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
 }

}

KickOutServlet.java

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

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 KickOutServlet extends HttpServlet {


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String id=request.getParameter("id");
  Map<String, HttpSession> onlines=(Map<String, HttpSession>) request.getServletContext().getAttribute("onlines");
  if (onlines!=null) {
   if (onlines.containsKey(id)) {
    onlines.get(id).invalidate();
   }
  }
  request.getRequestDispatcher("/servlet/ShowServlet").forward(request, response);
 }

}

LoginOutServlet.java

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginOutServlet extends HttpServlet {


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doPost(request, response);
 }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   request.getSession().invalidate();
   response.sendRedirect(request.getContextPath()+"/index.jsp");

 }

}

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved