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

Servlets 如何使用servlet从上一个html页面到下一个页面获取值?,servlets,navigation,parameter-passing,Servlets,Navigation,Parameter Passing,这是我的Servlet: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String collectionName = request.getParameter("myColle

这是我的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页面的外观:

用户单击“创建”后,“我的web应用”会将用户重定向到下一页,如下所示:

我想使用我的第一个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)


在此处插入标题
编辑收藏:


包括与以下模式匹配的内容:

例子:http://www.mycompany.com/engineering/

不要包含与以下模式匹配的内容:

例子:http://www.mycompany.com/engineering/

最简单的答案是将第二个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更难的选择:

  • 将index3.html的内容嵌入servlet中。基本上,您将拥有一组看起来像中答案的代码,并将字符串放入输出中。问题:页面布局/颜色/措辞的任何更改都需要重新编译和重新部署
  • 在index3.html中创建一个“sentinel”值,例如,“----myvalue----”。读取index3.html中的每一行,并用所需的值替换sentinel值,然后重定向到替换过程中创建的新文件。问题:如果您想要两个值怎么办?现在需要两个sentinel值。如果您仅对保存您所服务的文件的目录具有读取权限,该怎么办
  • 使用JavaScript并将URL参数传递到第二个页面。呈现时,从URL中提取值并呈现它。问题:现在您需要处理两种语言
  • 老实说,所有这些(可能还有其他方法)都比JSP方法更难 表单操作标记中指定的路径正确

    这里有一个关于如何做到这一点的简短指南:

    第一步 将两个页面的扩展名更改为jsp。e、 g.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 在第二页: 将yout textarea code更改为以下内容:

    <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-->
    
    ${collectionName}
    

    希望有帮助。

    谢谢您抽出时间。但是有没有一种方法可以在Servlet中实现呢?因为我以前没有使用过JSP。将JSP与servlet结合使用有很多好处,比如使用EL从servlet访问对象。您可以通过将html扩展名更改为html来开始使用它。就这样。谢谢@stdunbar我已经使用了jsp方法,它正在工作。谢谢Minjun。我要试试这个让你知道
    <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-->