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

zengkefu 阅读:17 2025-01-19 22:14:33 评论:0

这是我的 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 页面的样子:http://i.stack.imgur.com/oFlQD.png

在用户点击创建后,我的网络应用程序将用户重定向到下一页,如下所示:http://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>  

请您参考如下方法:

假设

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

以下是有关如何执行此操作的简要指南:

第一步

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

第二步

在你的 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"); 
} 

第三步

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

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

希望对您有所帮助。


标签:Servlet
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号