As my piece web.xml file access interface is not configured by default, tomcat will automatically find index.html, but found no index.html, index.jsp interface will visit the
1. Write index.jsp interface, so that it automatically accesses the login screen:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: Joker Date: 2019/8/11 0011 Time: 20:55 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <c:redirect url="login?method=getjsp"/> <%-- <html> <head> <title>Title</title> </head> <body> <h1>Hello world!</h1> </body> </html> --%>
2. Create a new class LoginServlet at com.fitsoft.shop.action packet, receives a processing request
@WebServlet ( "/ Login" ) public class the LoginServlet the extends the HttpServlet { Private the HttpServletRequest Request; Private the HttpServletResponse Response; // define business layer objects @Resource Private ShopService shopService; @Override public void the init () throws ServletException { Super .init (); // Get Spring container, then obtain the business layer objects from the container the ServletContext where servletContext = the this .getServletContext (); the WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); shopService = (ShopService) context.getBean("shopService"); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //跳转到登录界面 String method = req.getParameter("method"); if(method.equals("getjsp")){ req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp); } } }
@Resource use annotations to acquire the business layer objects, but because Servlet is not handed over to the management of Spring, Spring is not the object is to inject it, so @Resource comment and did not take effect, so we initialize method in accordance with the Servlet Spring context object Get context object, and then acquires the business layer in accordance with the object name ( "shopService"), and at this time should be added to the business layer objects Note:
To ensure that the object is scanned, then we can not use this shopService, and then deal with our request, to determine the value of method is not index.jsp pass over the interface, if it is, it will forward the request to the login screen: