【问题标题】:Issue in the servlet container, getting the servlet location twiceservlet 容器中的问题,两次获取 servlet 位置
【发布时间】:2020-05-12 19:04:38
【问题描述】:

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

这是我的项目结构:


test是首页,用户可以输入自己的爱好。确认页面,将向用户确认详细信息。它可以选择返回并编辑信息或将信息提交到 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>

确认.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 不再重复,但无法返回。

请找到confirm.jsp的HTML代码:

<!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>

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    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">
    

    【讨论】:

    • 感谢 Areus 的反馈。我已将表单操作更新为
      但我仍然收到错误消息。
    • 生成的 HTML 是什么?我
    • 嗨,Areus,我在问题中添加了 confirm.jsp 的 HTML 代码。
    • 我的错。我在想JSF。在 JSP 中没有 request 隐式对象。您需要使用${pageContext.request.contextPath}。我会更新答案
    • 您的 servlet 上也可能存在问题,您使用的 address var 用于获取 RequestDispatcher。您应该像使用 /Confirm.jsp 一样对 test.jspProcess.jsp 使用前导 /
    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多