CartServlet无效
<?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>CartDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>CharseEncodingFilter</filter-name> <filter-class>com.imooc.cart.Filter.CharseEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharseEncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <servlet> <servlet-name>ProductServlet</servlet-name> <servlet-class>com.imooc.cart.servlet.ProductServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProductServlet</servlet-name> <url-pattern>/product/list.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>CartServlet</servlet-name> <servlet-class>com.imooc.cart.servlet.CartServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CartServlet</servlet-name> <url-pattern>/cart/list.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CartServlet</servlet-name> <url-pattern>/cart/cart.do</url-pattern> </servlet-mapping> <error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location> </error-page> </web-app>
正在回答
ieda在发布项目的时候,默认是/(当然,此处也也可以自己配置,如果默认是/ 的情况下是不用写项目名的)
而eclipse是要写上项目名的。
但是无论是eclipse还是idea,都建议同学可以加上<%=basePath%>,可以获取到我们的发布路径,这样无论你配置的访问路径是否有项目名,它就都可以自动获取了。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
此时req.getServlet()获取到的是/cart/list.do.
为什么else if (Objects.equals("/cart.list.do", req.getServletPath())){}没有执行呢
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getContextPath();
String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + path;
if (Objects.equals("/cart/cart.do", req.getServletPath())) {
System.out.println("获取到/care/cart.do请求");
System.out.println(req.getServletPath());
String productId = req.getParameter("productId");
if (null != productId) {
System.out.println("添加商品到购物车");
Product product = LocalCache.getProduct(Long.valueOf(productId));
LocalCache.addCart(product);
}
resp.sendRedirect("list.do");
System.out.println(req.getServletPath());
System.out.println("地址重定向完成");
} else if (Objects.equals("/cart.list.do", req.getServletPath())) {
req.setAttribute("carts", LocalCache.getCarts());
req.getRequestDispatcher("/WEB-INF/views/biz/cart.jsp").forward(req, resp);
System.out.println("跳转页面成功");
} else {
System.out.println("没有方法被执行");
System.out.println(req.getServletPath());
}
}
}
package com.imooc.cart.servlet; import java.io.IOException; import java.util.Objects; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.cart.data.LocalCache; import com.imooc.cart.data.Product; @SuppressWarnings("serial") public class CartServlet extends HttpServlet { @Override public void destroy() { super.destroy(); } @Override public void init() throws ServletException { System.out.println("123"); super.init(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if(Objects.equals("/cart/cart.do",req.getServletPath())) { String productId=req.getParameter("productId"); if(null!=productId) { Product product=LocalCache.getProduct(Long.valueOf(productId)); LocalCache.addCart(product); } resp.sendRedirect("/cart/list.do"); }else if (Objects.equals("cart/list.do", req.getServletPath())){ req.setAttribute("carts", LocalCache.getCarts()); req.getRequestDispatcher("/WEB-INF/views/biz/cart.jsp").forward(req, resp); } } }
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星