过滤器和监听器



实现网站计数器



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>人数统计</title>
</head>
<body>
<h2>
欢迎光临,<br>
您是本站的第【 
<%=application.getAttribute("count") %>
】位访客!
</h2>
</body>
</html>
















添加图书信息




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>添加图书信息</title>
</head>
<body>
<form action="AddServlet" method="post">
<table align="center" border="1" width="350">
<tr>
<td class="2" align="center" colspan="2">
<h2>添加图书信息</h2>
</td>
</tr>
<tr>
<td align="right">图书编号:</td>
<td>
<input type="text" name="id">
</td>
</tr>
<tr>
<td align="right">图书名称:</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td align="right">作  者:</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td align="right">价  格:</td>
<td>
<input type="text" name="price">
</td>
</tr>
<tr>
<td class="2" align="center" colspan="2">
<input type="submit" value="添 加">
</td>
</tr>
</table>
</form>
</body>
</html>














应用Servlet监视器统计在线人数






<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用监听查看在线用户</title>
</head>
<script language="javascript">
function checkEmpty(form){
for(i=0;i<form.length;i++){
if(form.elements[i].value==""){
alert("表单信息不能为空");
return false;
}
}
}
</script>
<link href="css/style.css" rel="stylesheet" type="text/css">
<body>
<div align="center">
  <table width="506" height="170" border="0" cellpadding="0" cellspacing="0" background="image/background1.jpg">
    <tr>
      <td align="center">
   <form name="form" method="post" action="showUser.jsp" onSubmit="return checkEmpty(form)">
    <input type="text" name="user"><br><br>
    <input type="submit" name="Submit" value="登录">
  </form>
 
 
 
 </td>
    </tr>
  </table>
</div>
</body>
</html>









<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<%
         session.invalidate();
 out.println("<script>parent.location.href='index.jsp';</script>");
%>










统计在线人数界面



<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="com.listener.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用监听查看在线用户</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<%
UserInfoList list=UserInfoList.getInstance();
UserInfoTrace ut=new UserInfoTrace();
String name=request.getParameter("user");
ut.setUser(name);
session.setAttribute("list",ut);
list.addUserInfo(ut.getUser());
session.setMaxInactiveInterval(10);
%>
<body>
<div align="center">




<table width="506" height="246" border="0" cellpadding="0" cellspacing="0" background="image/background2.jpg">
  <tr>
    <td align="center"><br>
 
 <textarea rows="8" cols="20">
<%
Vector vector=list.getList();
if(vector!=null&&vector.size()>0){
for(int i=0;i<vector.size();i++){
  out.println(vector.elementAt(i));
}
}
%>
</textarea><br><br>
 <a href="loginOut.jsp">返回</a>
 
 </td>
  </tr>
</table>
</div>
</body>
</html>














JSP与Servlet实现用户注册




<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<link href="css/style.css" type="text/css">
<style type="text/css">
body{
margin: 0px;font-size: 12px;
}
.box{
border: 1px solid #D1DEB2; width: 150px; height: 20px;
}
.div1{
background-image: url(images/bg.jpg);
height: 600px;
width: 803px;
padding-left:20px;
padding-top:220px;
text-align:left;
}
</style>
<script type="text/javascript">
    function reg(form){
        if(form.username.value == ""){
        alert("用户不能为空!");
        return false;
        }
        if(form.password.value == ""){
        alert("密码不能为空!");
        return false;
        }
        if(form.repassword.value == ""){
        alert("确认密码不能为空!");
        return false;
        }
        if(form.password.value != form.repassword.value){
        alert("两次密码输入不一致!");
        return false;
        }
        if(form.question.value == ""){
        alert("密码找回问题不能为空!");
        return false;
        }
        if(form.answer.value == ""){
        alert("密码找回答案不能为空!");
        return false;
        }
        if(form.email.value == ""){
        alert("电子邮箱不能为空!");
        return false;
        }
    }
   </script>
</head>
<body>
<div align="center">
<div class="div1">
<form action="SaveServlet" method="post" onSubmit="return reg(this);">
<table align="center" border="0" width="500">
<tr>
<td align="right" width="30%" size ="9pt">用户名:</td>
<td><input type="text" name="username" class="box"></td>
</tr>
<tr>
<td align="right">密 码:</td>
<td><input type="password" name="password" class="box"></td>
</tr>
<tr>
<td align="right">确认密码:</td>
<td><input type="password" name="repassword" class="box"></td>
</tr>
<tr> 
<td align="right">性 别:</td>
<td>
<input type="radio" name="sex" value="男" checked="checked">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td align="right">密码找回问题:</td>
<td><input type="text" name="question" class="box"></td>
</tr>
<tr>
<td align="right">密码找回答案:</td>
<td><input type="text" name="answer" class="box"></td>
</tr>
<tr>
<td align="right">邮 箱:</td>
<td><input type="text" name="email" class="box"></td>
</tr>
<tr>
<td colspan="2" align="center" height="40">
<input type="submit" value="注 册">
<input type="reset" value="重 置">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>


















应用Servlet实现文件上传



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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="UploadServlet" enctype="multipart/form-data" method ="post" >
    选择文件<input type="file" name="file1" id= "file1"/>   
    <input type="submit" name="upload" value="上传" />
 </form>
</body>
</html>













<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>意见反馈</title>
<style type="text/css">
body {
margin: 0px;
font-size: 12px;
}
td{
font-size: 12px;
}
.div1{
width: 1003px;
height: 707px;
background-image: url("images/bg.jpg");
}
.div2{
width: 500px;
margin-top:245px;
margin-left:180px;
text-align:left;
}
.tl{
width: 500px;
height: 20px;
font-weight: bold;
background: #A3C0C6;
padding: 5px;
}
.ct{
width: 500px;
padding-left: 30px;
padding-top: 5px;
padding-bottom: 5px;
}
.tt{
margin-left:5px;
width: 70px;
background: #A3C0C6;
padding: 5px;
font-weight: bold;
font-size: 13px;
}
</style>
</head>


<body>
<div align="center">
<div class="div1">
<div class="div2">
<%
String title = (String)request.getAttribute("title");
String content = (String)request.getAttribute("content");
if(title != null && !title.isEmpty()){
out.println("<span class='tl'>" + title + "</span>");
}
if(content != null && !content.isEmpty()){
out.println("<span class='ct'>" + content + "</span>");
}
%>
<span class="tl">谢谢你们</span>
<span class="ct">你们的公司服务态度非常好,谢谢你们!</span>
<span class="tl">谢谢你们</span>
<span class="ct">你们的公司服务态度非常好<br>但部分客服服务态度还要加强!</span>
<form action="MessageServlet" method="post">
<span class="tt">意见反馈</span>
<table border="0" width="500" align="center">
<tr>
<td align="right">标 题:</td>
<td><input type="text" name="title" size="30"></td>
</tr>
<tr>
<td align="right">内 容:</td>
<td>
<textarea rows="5" cols="40" name="content"></textarea>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="提 交">
</td>
</tr>
</table>
</form>
</div>
</div>
</div>

</body>
</html>















//

利用监听器使服务器端免登入




<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>利用监听使服务器端机器免登录</title>
</head>
<%


String login=(String)request.getAttribute("login");
if(login.equals("true")){
response.sendRedirect("show.htm");
}




%>
<body><div align="center">


 <table width="335" height="225">
   <tr>
     <td background="leftsearch.gif" align="center">
 <form name="form1" method="post" action="show.htm">
   <input type="submit" name="Submit" value="登录">
 </form>
 
</td>
   </tr>
 </table>
</div>
</body>
</html>











<!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=gb2312">
<title>利用监听使服务器端机器免登录</title>
</head>


<body>
<div align="center">
  <table width="335" height="225">
    <tr>
      <td background="leftsearch.gif" align="center">这是你要访问的页面<br>
如果您远程访问一定去过登录页面吧。</td>
    </tr>
  </table>
</div>
</body>
</html>












//


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Servlet过滤器监听器是Java Web开发中的两个重要组件,可以在请求到达Servlet之前或者之后对请求和响应进行拦截、处理和修改,从而实现一些自定义的功能。 下面是Servlet过滤器监听器的配置及应用: 1. 过滤器的配置和应用: (1)在web.xml文件中配置过滤器: ```xml <filter> <filter-name>MyFilter</filter-name> <filter-class>com.example.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` (2)编写过滤器类: ```java public class MyFilter implements Filter { @Override public void init(FilterConfig config) throws ServletException { // 过滤器初始化代码 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 过滤器逻辑代码 chain.doFilter(request, response); } @Override public void destroy() { // 过滤器销毁代码 } } ``` (3)应用过滤器过滤器可以应用于所有的Servlet请求,或者只应用于特定的URL模式。在上面的配置中,我们将过滤器应用于所有的URL模式。 2. 监听器的配置和应用: (1)在web.xml文件中配置监听器: ```xml <listener> <listener-class>com.example.MyListener</listener-class> </listener> ``` (2)编写监听器类: ```java public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent event) { // 应用初始化代码 } @Override public void contextDestroyed(ServletContextEvent event) { // 应用销毁代码 } } ``` (3)应用监听器监听器可以监听应用程序的生命周期事件,比如应用程序的初始化、销毁等事件。在上面的配置中,我们将监听器应用于应用程序的初始化和销毁事件。 以上就是Servlet过滤器监听器的配置及应用的基本介绍。它们可以为我们提供很多灵活的机制来扩展和定制我们的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值