如何使用servlet将值从上一个html页面获取到下一个页面



这是我的Servlet:

    protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
    String collectionName = request.getParameter("myCollectionName");
    response.sendRedirect("index3.html");
    String pattern = request.getParameter("Pattern");
    String notPattern = request.getParameter("NotPattern");
    }
}

这是我的第一个html页面的样子:https://i.stack.imgur.com/oFlQD.png

用户单击创建后,我的web应用程序会将用户重定向到下一个页面,如下所示:https://i.stack.imgur.com/UkfGd.png

我想使用我的第一个html网页中的集合名称的值。在我的第二个html页面中,我希望"编辑集合"文本框的值与第一个html页面的集合名称的值相同。我怎样才能做到这一点?

这是我的第一个html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create New Collection</title>
<h><b>Create New Collection</b></h>
</head>
<body>
    <form method="post" action='CollectionPath' >
    <br>
    Collection Name:<textarea name="myCollectionName" cols="10" rows="1"></textarea>
    <br>
    <br>
    <input type="submit"
            value="Create" style="color:white;background: blue" />
    </form>
</body>
</html>

这是我的第二个html文件(index3.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action='CollectionPath' >
    Edit Collection: 
    <textarea name="CollectionNameValue" cols="10" rows="1"></textarea>
    <br>
    <br>
    <b>Include Content Matching the Following Patterns:</b>
    <br>
    <textarea name="pattern" cols="50" rows="10"></textarea>
    <br>
    example:http://www.mycompany.com/engineering/ 
    <br>
    <br>
    <b>Do Not Include Content Matching the Following Patterns</b>:
    <br>
    <textarea name="notPattern" cols="50" rows="10"></textarea>
    <br>
    example:http://www.mycompany.com/engineering/ 
    <br>
    <input type="submit"
            value="Save"  style="color:white;background: blue"/>

    </form>
</body>
</html> 

假设

您在表单操作标记中指定的路径是正确的。

以下是如何做到这一点的简要指南:

步骤1

将两个页面的扩展名更改为jsp。例如index.html->index.jsp。您将能够在jsp中使用EL(表达式语言)。

步骤2

在您的Servlet中:

protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
// Java's naming convention suggests that variable should be camel case
// e.g. String collectionName, please fix yourself.
String CollectionName = request.getParameter("myCollectionName");
request.setAttribute("collectionName", CollectionName);
//-- response.sendRedirect("index3.html");
// sendredirect will create a fresh request. As a result, the CollectionName you stored in the previous
// request does not exist anymore. You don't want that because your
// second page will get it from the request scope, see step 3.
// use forward instead
request.getRequestDispatcher("yourpage.jsp").forward(request,response);
// Not sure what these two lines are doing here because
// the previous html page do not have any input with name **Pattern**
// or "NotPattern", so you are not getting anything.
// please fix accordingly
String Pattern = request.getParameter("Pattern");
String NotPattern = request.getParameter("NotPattern");
}

步骤3

在您的第二页中:将您的文本区域代码更改为以下内容:

<textarea name="CollectionNameValue" cols="10" rows="1">${collectionName}</textarea>
<!-- you can do it because a String object with name **collectionName** is saved in request scope in the Servlet-->

希望有帮助

最简单的答案是使第二个HTML页面成为JSP。基本上,它看起来类似于现有的index3.html,但会被重命名为"index3.jsp"。然后,使用简单的jsp标记,您可以获得数据。不过,首先,您必须更新servlet:

protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    String CollectionName = request.getParameter("myCollectionName");
    // store this value in the session
    request.getSession().setAttribute("myCollectionName", CollectionName);
    String Pattern = request.getParameter("Pattern");
    String NotPattern = request.getParameter("NotPattern");
    response.sendRedirect("index3.jsp");
}

现在,在您的JSP中,您将希望看到这篇文章,以获得如何从会话中获取数据的指导。

关于你的代码传统的一条注释鼓励你使用小写(用于单个单词)或camelCase用于多单词变量名。Java没有强制执行这一点,但它受到鼓励。

编辑

您在评论中提到,您希望只使用servlet。但是,您是如何将HTML文件提供给浏览器的呢?猜测您使用的是Tomcat或Jetty。这些服务器也能够处理JSP文件。该文件将位于与现在相同的目录中,但名称会有所不同。如果您不能使用JSP,那么您还有其他一些选择,它们比JSP:更难

  1. 在servlet中嵌入index3.html的内容。基本上,你会有一堆代码,看起来像这篇文章中的答案,你会把你的字符串放在输出中问题:对页面布局/颜色/措辞的任何更改都需要重新编译和重新部署
  2. 在index3.html中创建一个"sentinel"值,例如"----myvalue----"。读取index3.html中的每一行,将sentinel值替换为所需值,然后重定向到替换过程中创建的新文件问题:如果想要两个值怎么办?现在您需要两个sentinel值。如果您只对保存所提供文件的目录具有读取权限,该怎么办
  3. 使用JavaScript并将URL参数传递到第二个页面。当它被渲染时,从URL中提取值并渲染它。问题:现在你有两种语言要处理

老实说,所有这些(可能还有其他方法)都比JSP方法更难。

最新更新