servlet 容器中的问题,两次获取 servlet 位置

Issue in the servlet container, getting the servlet location twice

我正在学习JSP/Servet并尝试做一个简单的Servlet控制器。

这是我的项目结构:


测试是主页,用户可以输入his/her爱好。确认页面,将向用户确认详细信息。它可以选择返回并编辑信息或将信息提交到 process.jsp 页面。

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_4_0.xsd" 
id="WebApp_ID" version="4.0">
  <servlet>
    <servlet-name>FirstController</servlet-name>
    <servlet-class>ch2.servletController.Controller</servlet-class>         
  </servlet>
  <servlet-mapping>
    <servlet-name>FirstController</servlet-name>
    <url-pattern>/ch2/servletController/Controller</url-pattern>
  </servlet-mapping>
</web-app>

Test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="eu">
    <head>
        <meta charset="UTF-8">
        <title>Simple web page</title>
    </head>
    <body>
    <p>
    This is a simple HTML page that has a form in it.
        <form action="ch2/servletController/Controller">
        <p>
            Hobby:<input type="text" name="hobby" value="${param.hobby}">
            <input type="submit" name ="confirmButton" value="confirm">
        </form>

    </body>
</html>

Confirm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Confirm Page</title>
</head>
<body>
    <p>
        The value of the hobby that was sent to this page is <strong>${param.hobby} </strong>.
    <p>
    <form action="ch2/servletController/Controller">
    <p>
        If there is any error, please click the edit button and 
            to process press the Submit button <br>
        <input type="hidden" name="hobby" value="${param.hobby}">
        <input type="submit" name="editButton" value="Edit">
        <input type="submit" name="submit" value="submit">
    </form>
</body>
</html>

Process.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Process Page</title>
    </head>
    <body>
    Thank you for your information, Your hobby of <strong>${param.hobby} </strong> will
    be added to our records, eventually.
    </body>
    </html>

Controller.java

package ch2.servletController;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Controller")
public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        String address;
        if(request.getParameter("submit") != null)
        {
            address = "Process.jsp";
        }
        else if ( request.getParameter("editButton") != null)
        {
            address = "test.jsp";
        }
        else
        {
            address = "/Confirm.jsp";
        }

        RequestDispatcher dispatcher = 
                request.getRequestDispatcher(address);
        dispatcher.forward ( request, response);
    }
}

test.jsp 输出:

点击确认:

但是我无法返回 test.jsp 页面和 Process.jsp 页面:

这是我在单击 confirm.jsp 页面的编辑按钮时遇到的错误。

据我所知,我的路径不正确,甚至 servlet 位置也在重复。是否可以解释为什么 servlet 位置重复以及我如何通过控制器成功导航到 test.jsp?

非常感谢您的提前反馈。

编辑 1:

感谢 areus 的反馈。修改 confirm.jsp 上的操作后,出现以下错误。 URL 不再重复,但无法返回。

请查找 HTML 代码 confirm.jsp:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Confirm Page</title>
</head>
<body>
    <p>
        The value of the hobby that was sent to this page is <strong>web programming </strong>.
    <p>
    <form action="/ch2/servletController/Controller">
    <p>
        If there is any error, please click the edit button and 
            to process press the Submit button <br>
        <input type="hidden" name="hobby" value="web programming">
        <input type="submit" name="Edit" value="Edit">
        <input type="submit" name="submit" value="submit">
    </form>
</body>
</html>

Confirm.jsp 中的表格中,您使用的是亲戚 URL:ch2/servletController/Controller。 URL 是相对于为请求提供服务的资源的位置(浏览器地址栏中的位置),http://localhost:9191/SampleProject/ch/servletController/Controller.

你应该对你 Confirm.jsp 使用绝对 URL,使用:

<form action="${pageContext.request.contextPath}/ch2/servletController/Controller">