AJAX无刷新页面和监听器文档格式.docx
《AJAX无刷新页面和监听器文档格式.docx》由会员分享,可在线阅读,更多相关《AJAX无刷新页面和监听器文档格式.docx(31页珍藏版)》请在冰豆网上搜索。
//后去配置文件中的初始化参数
publicEnumeractiongetInitParameterNames();
//用于获取全部的初始化参数
Enumeraction:
列举,不是一种数据结构,是java.util中的一个接口类
接口中定义了一个数据结构得到连续数据的手段
FilterChain
doFilter()//继续走下一个过滤器或请求资源
过滤器的加载
过滤器要配置web.xml
过滤只能通过登录界面进入主界面
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
web-appxmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="
//xmlns.jcp.org/xml/ns/javaee"
xsi:
schemaLocation="
//xmlns.jcp.org/xml/ns/javaeehttp:
//xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="
WebApp_ID"
version="
3.1"
<
display-name>
day0601<
/display-name>
<
welcome-file-list>
welcome-file>
index.html<
/welcome-file>
index.htm<
index.jsp<
default.html<
default.htm<
default.jsp<
/welcome-file-list>
filter>
filter-name>
myfilter<
/filter-name>
filter-class>
com.chinasofti.eec.filter.MyFilter<
/filter-class>
//路径
init-param>
//初始化参数来获取初始化参数的值
<
param-name>
jdbc<
/param-name>
param-value>
jdbc:
mysql:
//localhost:
3306/database<
/param-value>
/init-param>
jdbc1111<
3306/database1111<
/filter>
filter-mapping>
url-pattern>
/login<
/url-pattern>
!
--配置我们需要过滤的页面-->
/filter-mapping>
/web-app>
MyFilter.java
packagecom.chinasofti.eec.filter;
importjava.io.IOException;
importjava.util.Enumeration;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassMyFilterimplementsFilter{
@Override
publicvoiddestroy(){
publicvoiddoFilter(ServletRequestarg0,ServletResponsearg1,FilterChainarg2)
throwsIOException,ServletException{
HttpServletRequestreq=(HttpServletRequest)arg0;
//用req对象来替换arg0对象
HttpServletResponseresp=(HttpServletResponse)arg1;
req.setCharacterEncoding("
utf-8"
);
resp.setCharacterEncoding("
resp.setContentType("
text/html;
charset=utf-8"
//在前面对编码格式进行顾虑
arg2.doFilter(req,resp);
publicvoidinit(FilterConfigconfig)throwsServletException{
//获得初始化参数System.out.println(config.getInitParameter("
jdbc"
));
//输出jdbc:
3306/database
System.out.println(config.getFilterName());
//输出myfilter
System.out.println(config.getServletContext().getContextPath());
//输出/day0602
jdbc
jdbc1111
Enumeration<
String>
e=config.getInitParameterNames();
while(e.hasMoreElements()){
Stringstr=e.nextElement();
System.out.println(str);
}
LoginServlet.java
packagecom.chinasofti.eec.servlet;
importjava.io.PrintWriter;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
@WebServlet("
/login"
)//servlet与数据库连接的路径与form表单中的login跳转对应
publicclassLoginServletextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
this.doPost(req,resp);
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
Stringname=req.getParameter("
user"
//从页面获取内容name来装如页面的内容
System.out.println(name);
PrintWriterout=resp.getWriter();
out.print("
姓名是"
+name);
Index.jsp
%@pagelanguage="
java"
contentType="
charset=UTF-8"
pageEncoding="
%>
DOCTYPEhtmlPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
"
//www.w3.org/TR/html4/loose.dtd"
html>
head>
metahttp-equiv="
Content-Type"
content="
title>
Inserttitlehere<
/title>
/head>
body>
formaction="
login"
method="
post"
//action就是路径
user:
inputtype="
text"
name="
br/>
submit"
>
/form>
/body>
/html>
Web.xml
loginfilter<
com.chinasofti.eec.filter.LoginFilter<
/sec/*<
vipfilter<
com.chinasofti.eec.filter.VipFilter<
/vip/*<
uname"
radio"
name="
value="
2"
普通用户<
1"
VIP用户<
验证码:
imgsrc="
img"
)//servlet中的路径
Stringrole=req.getParameter("
//让role存储输如到点击时间的value值
if("
tom"
.equals(name)){
req.getSession().setAttribute("
name);
//uname是input中的而name则是容器
role"
role);
resp.sendRedirect("
sec/sec.jsp"
}else{
index.jsp"
LoginFilter.java
只能通过输入用户名来进入其他界面否则会一致在登录界面
importjavax.servlet.annotation.WebFilter;
publicclassLoginFilterimplementsFilter{
/**
*@seeFilter#doFilter(ServletRequest,ServletResponse,FilterChain)
*/
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{
HttpServletRequestreq=(HttpServletRequest)request;
//只要上传到服务器上拿和取都要用到req和resp对象
HttpServletResponseresp=(HttpServletResponse)response;
Stringname=(String)req.getSession().getAttribute("
//从session中获取值因为有HttpServletRequest。
过滤器不能直接从页面中拿要从session中拿
if(name==null){
resp.sendRedirect(req.getContextPath()+"
/index.jsp"
chain.doFilter(request,response);
*@seeFilter#init(FilterConfig)
publicvoidinit(FilterConfigfConfig)throwsServletException{
VipFilter.java
不是vip用户不能进入vip界面
publicclassVipFilterimplementsFilter{
//TODOAuto-generatedmethods