- 浏览: 900445 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (537)
- Java SE (114)
- Struts (18)
- Hibernate (25)
- Spring (3)
- Page_Tech (41)
- Others (87)
- Database (29)
- Server (24)
- OpenSource_Tools (15)
- IDE_Tool (22)
- Algorithm (28)
- Interview (22)
- Test (28)
- Hardware (1)
- Mainframe (25)
- Web application (4)
- Linux (3)
- PHP (17)
- Android (1)
- Perl (6)
- ubuntu (1)
- Java EE (9)
- Web Analysis (5)
- Node.js (2)
- javascript (2)
最新评论
-
一键注册:
request.getRequestURL()和request.getRequestURI() -
SuperCustomer:
...
SED的暂存空间和模式空间 -
juyo_ch:
讲得挺好理解的,学习了
java 死锁及解决 -
chinaalex:
最后一题答案正确,但是分析有误.按照如下过程,上一行为瓶,下一 ...
zz智力题 -
liaowuxukong:
多谢博主啦,弱弱的了解了一点。
C++/Java 实现多态的方法(C++)
这句话的真正意思,我到现在才体会出来。
他的作用是将请求转发给过滤器链上下一个对象。这里的“下”指的是哪里 ?
值得是下一个filter,如果没有filter那就是你请求的资源。
一般filter都是一个链,web.xml 里面配置了几个就有几个。一个一个的连在一起
request -> filter1 -> filter2 ->filter3 -> .... -> request resource.
下面举一个例子:
input.jsp是用来提交输入的,当提交后,过滤器检测姓名和年龄,如果整常的话会提交给output.jsp,如果不正常提交给erroroutput.jsp.在此同时也有一个过滤器,它是用来检测一个页面是否设置了字符编码,如果没有则进行设置。(防止乱码问题存在)。
myfilter.java
======================
package servletbean;
import javax.swing.JOptionPane;
public class myfilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
response.setContentType("text/html");
response.setCharacterEncoding("GB2312");
PrintWriter out=response.getWriter();
String name="";
String age="";
int age1;
name=request.getParameter("name");
age=request.getParameter("age");
RequestDispatcher dispatch=request.getRequestDispatcher("erroroutput.jsp"); if(name==null||name==""||name==" "||age==null)
{
JOptionPane.showMessageDialog(null,"用户名和年龄输入错误!");
dispatch.forward(request, response);
return;
}
else
{
try
{
age1=Integer.parseInt(age);
}
catch(Exception e)
{
//JOptionPane.showMessageDialog(null,"年龄必须为数字!");
dispatch.forward(request,response);
return;//如果是错误页面就到erroroutput.jsp中
}
}
chain.doFilter(request, response);//这里表示是正确的,也就是说,他回去找下一个链,但是旗下面已经没有了,所以就会去跳转页面了,此时要跳转的页面就是action="output.jsp"了。
}
}
==================
encodefilter.java
===================
package servletbean;
public class encodefilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(request.getCharacterEncoding()==null)
{
System.out.println(encoding);
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);//到下一个链
}
public void init(FilterConfig fConfig) throws ServletException {
this.config=fConfig;
encoding=fConfig.getInitParameter("encoding");
}
}
==============
input.jsp
==============
<form action="output.jsp" name="form" method="post">
<table>
<tr>
<td>name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>age</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>
<input type="submit" name="ok" value="ok"/>
</td>
</tr>
</table>
</form>
================
web.xml
===============
注意:filter是有顺序的
<filter>
<description>
</description>
<display-name>
encodefilter</display-name>
<filter-name>encodefilter</filter-name>
<filter-class>servletbean.encodefilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodefilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<description>
</description>
<display-name>
myfilter</display-name>
<filter-name>myfilter</filter-name>
<filter-class>servletbean.myfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/output.jsp</url-pattern>
</filter-mapping>
发表评论
-
iPhone5s插件推荐 越狱后必装的50个插件
2014-12-31 16:58 15361. Activator (没有之一的强大手势操作软件)2. ... -
JS automation 环境迁移备注
2014-11-15 11:17 0此次迁移涉及的问题记录如下: 1) Jenkins 直接把 ... -
MacOS 怎么写NFS移动硬盘
2014-11-14 19:13 26701、打开命令行终端。 2、插上移动硬盘,这时候你在Find ... -
How can I resize a partition with Disk Utility (Bottom-up)
2014-10-10 14:30 448Create a new volume in the bla ... -
Outlook 邮件提醒长期显示
2012-08-30 10:38 1313I thought it was one of th ... -
chrome 背景色的设置
2012-07-17 09:56 2993最近换了浏览器,开始喜欢上chrome。 为了保护眼睛 ... -
日常生活中练习右脑
2012-07-12 10:16 1231成年人在日常琐细的生活中,同样可以采取各种方法锻炼右脑。 ... -
大公司面试集锦
2012-06-18 10:23 1324微软十五道面试题 1、有一个整数数组,请求出两两之差绝对值最 ... -
商务英语中的委婉表达
2012-06-18 10:19 12171. 委婉: 1) 动词:think、hope、re ... -
放松颈椎的几个动作
2012-05-29 12:41 1157随时放松颈部肌肉 ... -
drupal简单的体验
2012-05-24 15:41 839第一步: 到 drupal.org下下载最新的Drupal ... -
HtmlParser进行解析原理
2012-05-23 13:48 1144这两天准备做一些网站编程的工作,于是对HtmlParse小研究 ... -
使用HtmlParser提取HTML文本块
2012-05-23 13:46 1436听人介绍说HtmlParser(Java版本)在网页预处理 ... -
[zz] Opencms vs Magnolia
2012-05-22 10:35 1987最近一直在挑选CMS,Opencms和Magnolia是考察的 ... -
面向对象的三个基本特征
2012-04-25 14:56 784面向对象的三个基本特征是:封装、继承、多态。 ... -
Maven vs Ant
2012-04-23 14:35 1172Ant 将提供了很多可以重用的task,例如 copy, mo ... -
zz智力题
2012-03-26 22:50 12261、有两根不均匀分布 ... -
几种开源Portal的简单介绍分析
2012-02-21 22:42 2407主要包括:Pluto,Liferay,eXo,Jetsp ... -
Portal top ten
2012-02-21 22:37 973TOP1 独立网店系统 Sh ... -
英文面试须知
2012-02-16 15:11 954英语面试需要好好准备 ...
相关推荐
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (encoding != null) { request.setCharacterEncoding(encoding); ...
response.sendRedirect(request.getContextPath()); }else{ chain.doFilter(request, response); } } web.xml配置: <filter-name>accessFilter <filter-class>com.loginFilter</filter-class> ...
response.setCharacterEncoding("gb18030"); chain.doFilter(req, res); } 2、Servlet代码:[只要doPost()或doGet()方法] (1)add.java: response.setContentType("text/html"); request.setCharacter...
public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) return; Filter filter = filters.get(index); index++; filter.doFilter(request, response...
public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) return; Filter filter = filters.get(index); index++; filter.doFilter(request, response...
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { MyRequest myRequest = null; HttpServletRequest request2 = ...
经常会用到在网页上交替显示图片。很多推荐的作法是用new Image() 预先装载图片。但是用new Image()真的有用吗?试验: 在后台放一个过滤器捕捉所有 /* 的... chain.doFilter(request, response); html代码:
chain.doFilter((ServletRequest) Proxy.newProxyInstance(CharFilter.class.getClassLoader(),request.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method ...
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest reque=(HttpServletRequest) request; System.out...
Adds the specified cookie to the response. addDateHeader(String, long) - Method in class javax.servlet.http.HttpServletResponseWrapper The default behavior of this method is to call addDateHeader...
chain.doFilter(request,response); return; } ②方式2 单独放置登录页面 login.html login.jsp 4.项目周期 1周时间 项目答辩时间: 5.项目分析: (1)登录功能: ①根据用户名查询数据库, ...
public void doFilter ServletRequest sRequest ServletResponse sResponse FilterChain chain throws IOException ServletException { HttpServletRequest request HttpServletRequest sRequest; String ...
jsp代码: import javax.servlet.*;... public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { HttpServletRequest httpRequest = (HttpServle
在过滤器中写入如下方法: 代码如下: public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest = ...
在 Java Web 开发中,当... public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest servletRequest=(HttpServle