我是靠谱客的博主 漂亮荔枝,最近开发中收集的这篇文章主要介绍Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法)准备一个Servlet(Lesson2/lesson6_2.Sample7.java)Filter代码段:(Lesson2/lesson6_2.SampleFilter.java)web.xml配置(WEB-INF/web.xml)执行结果:说明:,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
准备一个Servlet(Lesson2/lesson6_2.Sample7.java)
package lesson6_2;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
//@WebServlet("/Sample7")←此处如果使用webservlet,可以不用在web.xml配置Servlet,但是Filter必须配置
public class Sample7 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
//コンテンツタイプの設定
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
pw.println(
"お選びください<br/>n"+
"<br/>n"+
"<a href="../car1.html">乗用車</a><br/>n"+
"<a href="../car2.html">トラック</a><br/>n"+
"<a href="../car3.html">オープンカー</a><br/>n"
);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Filter代码段:(Lesson2/lesson6_2.SampleFilter.java)
package lesson6_2;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
/**
* Servlet Filter implementation class SampleFilter
*/
//@WebFilter("/SampleFilter")
public class SampleFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
response.setContentType("text/html; charset=UTF-8");
PrintWriter pw=response.getWriter();
pw.println("<html>n"+
"<head><title>SampleFilter</title></head>n"+
"<body><center>n"+
"<h2>こんにちわ</h2>"+
"<hr/>n");
// pass the request along the filter chain
chain.doFilter(request, response);
pw.println("<hr/>ありがとうございました.n"+
"</center></body>n"+
"</html>"
);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) {
// TODO Auto-generated method stub
}
}
web.xml配置(WEB-INF/web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Lesson2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>lesson6_2.SampleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>/Sample7</url-pattern><!-- 这里的url是需要拦截的url,浏览器访问时首先调用Filter(过滤器)里的内容-->
</filter-mapping>
<servlet><!--如果在Sevlet里使用了//@WebServlet("/Sample7")配置url,这里可以不用配置Servlet-->
<servlet-name>Sample7</servlet-name>
<servlet-class>lesson6_2.Sample7</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sample7</servlet-name>
<url-pattern>/Sample7</url-pattern>
</servlet-mapping>
</web-app>
执行结果:
在浏览器中访问:http://localhost:8080/Lesson2/Sample7
说明:
里配置的是需要拦截的url,当访问该url的时候,首先调用Filter(过滤器)里的内容,然后根据过滤器的方法继续执行。
最后
以上就是漂亮荔枝为你收集整理的Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法)准备一个Servlet(Lesson2/lesson6_2.Sample7.java)Filter代码段:(Lesson2/lesson6_2.SampleFilter.java)web.xml配置(WEB-INF/web.xml)执行结果:说明:的全部内容,希望文章能够帮你解决Java学习笔记·Servlet&Filter过滤器配置实例(web.xml配置方法)准备一个Servlet(Lesson2/lesson6_2.Sample7.java)Filter代码段:(Lesson2/lesson6_2.SampleFilter.java)web.xml配置(WEB-INF/web.xml)执行结果:说明:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复