css样式失效
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/css/login.css"> <script type="text/javascript"> function data_Judge(){ // 获得用户名的值 var username = document.getElementById("name").value; if(username == null || username==""){ alert("用户名不能为空"); return false; } // 获得密码的值 var password = document.getElementById("pwd").value; if(password == null || password==""){ alert("密码不能为空"); return false; } //获得验证码的值 var code = document.getElementById("code").value; var length = code.length; if(length != "5"){ alert("验证码格式有误"); return false; } } //点击更换图片函数 function changeImg(){ var codeImg = document.getElementById("codeImg"); /* 使用第三方直接获取验证码 */ codeImg.src="${pageContext.request.contextPath}/KaptchaServlet?time=" + new Date().getTime(); } </script> </head> <body> <div class="login"> <div class="header"> <h1> <a href="/login.do">登录</a> <!-- 如果登入失败 msg就会被赋值并提示错误信息 --> <p style="color:red"> ${ msg }</p> </h1> <button></button> </div> <form action="${pageContext.request.contextPath}/LonginServlet" method="post" onsubmit="return data_Judge()"> <div class="name"> <input type="text" id="name" name="username"> <p></p> </div> <div class="pwd"> <input type="password" id="pwd" name="password"> <p></p> </div> <div class="code"> <input type="text" id="code" name="verifyCode" style="width: 150px"> <img id="codeImg" onclick="changeImg()" src="${pageContext.request.contextPath}/KaptchaServlet" style="width: 150px; height: 42px; vertical-align: middle;"> <p></p> </div> <div class="btn-red"> <input type="submit" value="登录" id="login-btn"> </div> </form> </div> </body> </html>
老师 刚才打开Ec'licpse的是时候打开浏览器输入地址样式就全部失效了 地址好像也没问题
61
收起
正在回答
11回答
同学你好,1、< url-pattern >/* < url-pattern > 中/* 能匹配所有请求URL,会匹配到servlet(/login)、*.jsp、*.js、*.css和*.html,所以直接设置/*也有可能会拦截到css文件的请求响应。
2、比如页面中<script src="http://地址:端口/项目名/文件名.js"></script>,这就是一个html请求访问这个js文件的一个请求。
3、jsp引入css有请求和响应嘛
jsp页面引入一个文件,当项目启动后,在页面中访问时,浏览器会向页面发送请求访问这个css文件,服务器通过请求,将这个css文件的内容响应给浏览器的html页面。这就是引入css内容在浏览器执行的一次过程。
4、如果要绕开css我应该怎样编写这个<url-pattern>拦截路径
同学可以尝试如下方法过滤对应的页面,比如:
修改后重启,再试一下。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
jia_蛙
2019-12-10 21:40:51
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>book_system</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>login.jsp</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置初始化用户账户参数 --> <context-param> <param-name>account</param-name> <param-value>admin</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>huangzhou</param-value> </context-param> <!-- 对kaptcha第三方验证码生成包进行配置 --> <servlet> <servlet-name>KaptchaServlet</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!--去掉干扰线 --> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise</param-value> </init-param> <!--更换干扰样式 这里可以使用阴影效果 ShadowGimpy --> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>KaptchaServlet</servlet-name> <url-pattern>/KaptchaServlet</url-pattern> </servlet-mapping> <!-- 字符集的过滤器 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.zhou.web.filter.CharacterEncodingFilter</filter-class> <!-- 过来参数化设置 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <!-- 设置多个拦截路径 --> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/login.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/AddCategoryServlet</url-pattern> </filter-mapping> <!--登录过滤器 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.zhou.web.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> </web-app>
老师我刚才直接对添加分类的servlet进行了拦截, 现在有用了,回想起来,一开始,我先是因为 在字符解过滤器中设置了拦截全部,然后是css样式失效,然后接着直接根据老师的指导,直接拦截 对应的sevlet,现在有用了,我想问一下,<url-pattern>/page/*</url-pattern>和 <url-pattern>/login.jsp</url-pattern> 是拦截对应的页面里面传入的数据是吧! 我在添加 分类的页面上已经可以拦截到这个页面,案例说传入到添加分类的servlet中的数据已经是utf-8 的了,为什么我们还需要到添加分类的sevlet进行拦截才能生效?这个我有点不理解
jia_蛙
2019-12-10 19:52:21
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>book_system</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> <!-- 配置初始化用户账户参数 --> <context-param> <param-name>account</param-name> <param-value>admin</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>huangzhou</param-value> </context-param> <!-- 对kaptcha第三方验证码生成包进行配置 --> <servlet> <servlet-name>KaptchaServlet</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!--去掉干扰线 --> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise</param-value> </init-param> <!--更换干扰样式 这里可以使用阴影效果 ShadowGimpy --> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>KaptchaServlet</servlet-name> <url-pattern>/KaptchaServlet</url-pattern> </servlet-mapping> <!--登录过滤器 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.zhou.web.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> <!-- 字符集的过滤器 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.zhou.web.filter.CharacterEncodingFilter</filter-class> <!-- 过来参数化设置 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <!-- 设置多个拦截路径 --> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/login.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/page/addCategory.jsp</url-pattern> </filter-mapping> </web-app>
把<url-pattern>改为<url-pattern>/<url-pattern> 是什么意思 ?我跟老师你说的对添加新分类 的servlet的过滤 还是乱码
jia_蛙
2019-12-10 16:09:51
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>s_book_system</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> <!-- 配置初始化用户账户参数 --> <context-param> <param-name>account</param-name> <param-value>admin</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>huangzhou</param-value> </context-param> <!-- 对kaptcha第三方验证码生成包进行配置 --> <servlet> <servlet-name>KaptchaServlet</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!--去掉干扰线 --> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise</param-value> </init-param> <!--更换干扰样式 这里可以使用阴影效果 ShadowGimpy --> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>KaptchaServlet</servlet-name> <url-pattern>/KaptchaServlet</url-pattern> </servlet-mapping> <!--登录过滤器 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.zhou.web.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> <!-- 字符集的过滤器 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.zhou.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package com.zhou.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * CharacterEncodingFilter 字符集过滤器 * @author 25677 * */ public class CharacterEncodingFilter implements Filter { //定义一个类中私有变量用来保存我们从xml中获取的过滤器的值 private String encoding = ""; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //过滤请求字符集 //因为我们的操作涉及的是Http操作 所以这里要进行强转操作 HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; //对拦截到的所有请求进行字符集设置 req.setCharacterEncoding(encoding); //对拦截到的所有响应进行字符集设置 res.setContentType("text/html;charset=" + encoding); //最后将请求和 响应继续向后传递 chain.doFilter(req, res); } @Override public void init(FilterConfig filterConfig) throws ServletException { //通过filterConfig对象中的getInitParameter方法得到我们在xml中的初始化参数 encoding = filterConfig.getInitParameter("encoding"); } }
老师 我找到了css样式失效的关键步骤了,就是web.xml中的字符集配置,我发现我设置字符集配置 之后css样式就会失效,如果屏蔽它,css样式就能够正常的显示图1图2就是分别设置和频闭的样图, web.xml的代码我贴在上面了,字符过滤器代码也贴在上面了,麻烦老师帮我分析一下
jia_蛙
2019-12-10 10:26:22
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/css/login.css"> <script type="text/javascript"> function data_Judge(){ // 获得用户名的值 var username = document.getElementById("name").value; if(username == null || username==""){ alert("用户名不能为空"); return false; } // 获得密码的值 var password = document.getElementById("pwd").value; if(password == null || password==""){ alert("密码不能为空"); return false; } //获得验证码的值 var code = document.getElementById("code").value; var length = code.length; if(length != "5"){ alert("验证码格式有误"); return false; } } //点击更换图片函数 function changeImg(){ var codeImg = document.getElementById("codeImg"); /* 使用第三方直接获取验证码 */ codeImg.src="${pageContext.request.contextPath}/KaptchaServlet?time=" + new Date().getTime(); } </script> </head> <body> <div class="login"> <div class="header"> <h1> <a href="/login.do">登录</a> <!-- 如果登入失败 msg就会被赋值并提示错误信息 --> <p style="color:red"> ${ msg }</p> </h1> <button></button> </div> <form action="${pageContext.request.contextPath}/LonginServlet" method="post" onsubmit="return data_Judge()"> <div class="name"> <input type="text" id="name" name="username"> <p></p> </div> <div class="pwd"> <input type="password" id="pwd" name="password"> <p></p> </div> <div class="code"> <input type="text" id="code" name="verifyCode" style="width: 150px"> <img id="codeImg" onclick="changeImg()" src="${pageContext.request.contextPath}/KaptchaServlet" style="width: 150px; height: 42px; vertical-align: middle;"> <p></p> </div> <div class="btn-red"> <input type="submit" value="登录" id="login-btn"> </div> </form> </div> </body> </html> ------------- 老师这上面就是我的全部代码 怕字符超出所以页面直复制了登录的login.jsp css我都是直接复制 过来的没有该过,我昨天下午发现过滤器和监视器写在同一个包下,然后就创建了一个过滤器,把 这些类分开来,然后修改了一下web.xml文件,后面晚上,我登入的时候,清理了一下浏览器缓存, 就发现没有样式了,不知道是什么原因,找了很久
jia_蛙
2019-12-10 10:24:01
DeleteCategoryServlet.java package com.zhou.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhou.web.domain.Category; import com.zhou.web.service.UserService; import com.zhou.web.service.impl.UserServiceImpl; /** * DeleteCategoryServlet 删除图书分类的控制 */ @WebServlet("/DeleteCategoryServlet") public class DeleteCategoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据 String categoryId = request.getParameter("categoryId"); //处理数据 UserService userService = new UserServiceImpl(); //获取图书分类列表 List<Category> categoryList = (List<Category>) request.getServletContext().getAttribute("categoryList"); //删除图书 userService.DeleteCategory(categoryList,categoryId); //显示结果 //重定向到图书分类管理页面 response.sendRedirect(request.getContextPath() + "/page/categoryList.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } -------------------- LonginServlet.java package com.zhou.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhou.web.domain.User; import com.zhou.web.service.UserService; import com.zhou.web.service.impl.UserServiceImpl; /** * LonginServlet 登入注册的判断的控制器 */ @WebServlet("/LonginServlet") public class LonginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收验证码并进行校验 String verifyCode = request.getParameter("verifyCode"); //先获取session中保存的验证码的信息 String code =(String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); //equalsIgnoreCase 忽略大小写比较 if(verifyCode==null || !verifyCode.equalsIgnoreCase(code)) { request.setAttribute("msg", "验证码输入不正确!"); request.getRequestDispatcher("/login.jsp").forward(request, response); //response.sendRedirect(request.getContextPath()+"/login.jsp"); return;//结束下面的代码 } //接收用户名与密码并判断 String username = request.getParameter("username"); String password = request.getParameter("password"); //封装数据 User user = new User(username,password); //处理数据 UserService userService = new UserServiceImpl(); //.获取全局域ServletContext中用户集合 并调用业务逻辑处理接口中的方法进行判断 List<User> listUser =(List<User>) request.getServletContext().getAttribute("userList"); User existUser = userService.login(listUser, user); if(existUser == null) { request.setAttribute("msg", "用户名或密码错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); }else { //登录成功 再session设置一个标志表明用户已经登录 request.getSession().setAttribute("loginName", existUser); //重定向到首页 response.sendRedirect(request.getContextPath()+"/page/categoryList.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ---------------------- web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>book_system</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> <!-- 配置管理员账户和密码 --> <context-param> <param-name>account</param-name> <param-value>admin</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>huangzhou</param-value> </context-param> <!--配置验证码 --> <servlet> <servlet-name>KaptchaServlet</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!--去掉干扰线 --> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise</param-value> </init-param> <!--更换干扰样式 这里可以使用阴影效果 ShadowGimpy --> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>KaptchaServlet</servlet-name> <url-pattern>/KaptchaServlet</url-pattern> </servlet-mapping> <!-- 登录过滤器配置 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.zhou.web.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/page/*</url-pattern> </filter-mapping> <!-- 字符集过滤器 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.zhou.web.filter.CharacterEncodingFilter</filter-class> <!-- 过滤参数化设置 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
jia_蛙
2019-12-10 10:22:42
CharacterEncodingFilter.java package com.zhou.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * CharacterEncodingFilter 字符集过滤器 * @author 25677 * */ public class CharacterEncodingFilter implements Filter { //定义一个类中私有变量用来保存我们从xml中获取的过滤器的值 private String encoding = ""; @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //过滤请求字符集 //因为我们的操作涉及的是Http操作 所以这里要进行强转操作 HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; //对拦截到的所有请求进行字符集设置 req.setCharacterEncoding(encoding); //对拦截到的所有响应进行字符集设置 res.setContentType("text/html;charset=" + encoding); //最后将请求和 响应继续向后传递 chain.doFilter(req, res); } @Override public void init(FilterConfig filterConfig) throws ServletException { //通过filterConfig对象中的getInitParameter方法得到我们在xml中的初始化参数 encoding = filterConfig.getInitParameter("encoding"); } }
LoginFilter.java package com.zhou.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhou.web.domain.User; /** * LoginListener 登录过滤器 * @author 25677 * */ public class LoginFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { //首先将ServletRequest、ServletResponse进行强转 HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; //判断session域中的有没有存在用户名,没有则需要重新登录 有则表示登入 User user = (User) request.getSession().getAttribute("loginName"); if(user == null) { response.sendRedirect(request.getContextPath() + "/login.jsp"); }else { arg2.doFilter(request, response); } } @Override public void init(FilterConfig filterConfig) throws ServletException { } }
InitServletContextListener.java package com.zhou.web.listener; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import com.zhou.web.domain.Category; import com.zhou.web.domain.User; /** * InitServletContextListener 启动程序时,初始化数据 * @author 25677 * */ //使用注解的方式对监听器进行配置 @WebListener public class InitServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent event) { //1.初始化账户列表参数 //创建一个list集合用来保存用户 List<User> userList = new ArrayList<User>(); /* * 假设管理员账号比较隐私 不允许别人创建等操作 我们现在web.xml中配置好管理员账号 * 然后再程序初始化的时候获取到保存到list集合中,根据用户登录时输入的数据进行比较 */ String account = event.getServletContext().getInitParameter("account"); String password = event.getServletContext().getInitParameter("password"); //创建User对象 并将获得的管理员输入填入其中 User user = new User(account,password); //将user对象填入userlist中 userList.add(user); //将userList存入ServletContext全局对象中 event.getServletContext().setAttribute("userList", userList); //2.初始化图书分类列表参数 //创建默认分类数据 Category category = new Category("ca0001","计算机类"); //创建集合 List<Category> categoryList = new ArrayList<Category>(); //往集合中添加数据分类 categoryList.add(category); //将集合对象categoryList存入ServletContext全局对象中 event.getServletContext().setAttribute("categoryList", categoryList); } }
UserService.java package com.zhou.web.service; import java.util.List; import com.zhou.web.domain.Category; import com.zhou.web.domain.User; /** * UserService 处理业务逻辑的接口类 * @author 25677 * */ public interface UserService { //处理登录的业务逻辑接口 public User login(List<User> listUser, User user); //处理添加分类逻辑的业务接口 public boolean JudgeCategory(List<Category> categoryList, Category category); //处理删除分类逻辑的业务接口 public void DeleteCategory(List<Category> categoryList, String categoryId); }
UserServiceImpl.java package com.zhou.web.service.impl; import java.util.Iterator; import java.util.List; import com.zhou.web.domain.Category; import com.zhou.web.domain.User; import com.zhou.web.service.UserService; /** * UserServiceImpl 处理业务逻辑接口的实现类 * @author 25677 * */ public class UserServiceImpl implements UserService { /** * 登入业务逻辑 */ @Override public User login(List<User> listUser, User user) { for(User users: listUser) { if(users.getUser().equals(user.getUser()) && users.getPassword().equals(user.getPassword())) { return users; } } return null; } /** * 处理添加分类的业务逻辑 * true 表示添加成功 */ @Override public boolean JudgeCategory(List<Category> categoryList, Category category) { //如果分类名或者分类id存在一样相同我们都认为添加失败 for(Category c: categoryList) { if(c.getId().equals(category.getId()) || c.getCategoryName().equals(category.getCategoryName())) { return false; } } //不存在相同的 则将新的分类添加到集合 categoryList.add(category); return true; } /** * 处理删除图书分类的业务逻辑 */ @Override public void DeleteCategory(List<Category> categoryList, String categoryId) { /* * for(Category category: categoryList) { * if(category.getId().equals(categoryId)) { categoryList.remove(category); } } */ Iterator<Category> iterator = categoryList.iterator(); while(iterator.hasNext()) { Category c = iterator.next(); if(c.getId().equals(categoryId)) { iterator.remove(); return; } } } }
AddCategoryServlet.java package com.zhou.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhou.web.domain.Category; import com.zhou.web.service.UserService; import com.zhou.web.service.impl.UserServiceImpl; /** * AddCategoryServlet 添加分类的控制器 */ @WebServlet("/AddCategoryServlet") public class AddCategoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据 String categoryId = request.getParameter("categoryId");//图书分类ID String categoryName = request.getParameter("categoryName");//分类名称 //封装数据 Category category = new Category(categoryId,categoryName); //处理结果 UserService userService = new UserServiceImpl(); //获取当前图书分类集合categoryList List<Category> categoryList =(List<Category>) request.getServletContext().getAttribute("categoryList"); //调用方法判断是否已经有相同的分类或者分类名称 boolean flagAdd = userService.JudgeCategory(categoryList,category); //显示数据 if(flagAdd) { //添加成功 重定向到图书分类管理页面 response.sendRedirect(request.getContextPath() + "/page/categoryList.jsp"); }else { //添加失败 //设置失败标识 request.setAttribute("flag", "0"); System.out.println("失败 重复"); request.getRequestDispatcher("/page/addCategory.jsp").forward(request, response); //response.sendRedirect(request.getContextPath() + "/page/addCategory.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星