Hello. Trying to make Servlet redirect request to JSP. I use RequestDispatcher.
Here is the servlet code:
public class ServletDispatcher extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); dispatcher.forward(request,response); } } Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>ServletDispatcher</servlet-name> <servlet-class>com.geraltsx.library.web.controlers.ServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDispatcher</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> </web-app> index.jsp is located in the web folder of the project, next to WEB-INF:
<%@page contentType="text/html" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Документ</title> </head> <body> <h1>пРивет</h1> </body> </html> I use the server GlassFish. When entering the browser url http: // localhost: 8080 / app / servlet gives 404 error ((