1、http:/www.w3.org/2001/XMLSchema-instance xmlns=/xmlns.jcp.org/xml/ns/javaee xsi:schemaLocation=/xmlns.jcp.org/xml/ns/javaee http:/xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd id=WebApp_ID version=3.1 libraryprojectwelcome-file-listwelcome-fileindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault
2、.jspfilter userFilterfilter-classcom.chinasofti.eec.filter.UserFilter/filterfilter-mappingurl-pattern/* /filter-mapping/web-appUserFilter.javapackage com.chinasofti.eec.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;im
3、port javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UserFilter implements Filter Override public void destroy() / TODO Auto-generated metho
4、d stub public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException HttpServletRequest req = (HttpServletRequest)arg0; HttpServletResponse resp = (HttpServletResponse)arg1; req.setCharacterEncoding(utf-8); resp.setCharacterEncoding( arg2.doFi
5、lter(arg0, arg1); public void init(FilterConfig arg0) throws ServletException 2 通过cookie来存储登录的次数Login.jsp% page language=java contentType=text/html; charset=utf-8 pageEncoding=%!DOCTYPE html PUBLIC -/W3C/DTD HTML 4.01 Transitional/EN /www.w3.org/TR/html4/loose.dtdhtmlheadmeta http-equiv=Content-Type
6、 content=titleInsert title here/headbodyform action=login method=postdiv align=center 用户名:input type=text name=userbr / 密 &码:passwordpwd radiodate value=0 checked=checked 每次登陆都需要1010天内3030天内/form/body/htmlLoginServlet.javapackage servlet;import javax.servlet.ServletContext;import javax.servlet.
7、annotation.WebServlet;import javax.servlet.http.HttpServlet;WebServlet(/login)public class LoginServlet extends HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException this.doPost(req, resp); protected void doPost(HttpServletRequest req
8、, HttpServletResponse resp) throws ServletException, IOException String btn = req.getParameter( String name = req.getParameter( String pwd = req.getParameter( if(.equals(btn) if(admin.equals(name)&.equals(pwd) ServletContext ctxt = getServletContext(); int count = 0; if(ctxt.getAttribute(count)=null
9、) count= 0; else count = Integer.parseInt(ctxt.getAttribute().toString(); count+; ctxt.setAttribute(, count); resp.sendRedirect(index.jsp Index.jsp charset=UTF-8leftfont color=blue访问次数 /font3 通过cookie来存储对用户密码进行存储内置对象来进行变量参数的赋值,通过req.getParament 与req.setAttribute(”key”,vlaue)righth1 red欢迎 % String us
10、ername = null; String pwd = null; Cookie c = request.getCookies(); if(c!=null& c.length!=0) for(Cookie co: c) if(co.getName().equals(username) /* 不同页面中传值的变量是的双引号是键值 */ username = co.getValue(); /* 同一个页面中传值的变量是自己本页面定义的变量不能直接拿 ,servlet中要通过req,resp来进行值的传递*/ else if(co.getName().equals(userpwd) pwd = co
11、.getValue(); if(username!=null & pwd !=null)% request.getRequestDispatcher(login?user=+username+pwd=+pwd+btn=登录).forward(request, response);import javax.servlet.http.Cookie; String timeLength = req.getParameter( /*name属性选择器中是页面上可以看见的键值变量*/ int days = 0; if(timeLength!=null) days = Integer.parseInt(timeLength); if(days! Cookie usernamecookie = new Cookie(,name); Cookie userpwd = new Cookie(,pwd); usernamecookie.setMaxAge(days*24*3600); userpwd.setMaxAge(days*24*36000); resp.addCookie(usernamecookie); resp.addCookie(userpwd); req.getSession().setAttribute(, name);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1