如何使用 servlet 从上一页 html 页面获取值到下一页?
How to take value from previous html page to next page using servlet?
这是我的 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 页面中,我希望 "Edit Collection" 文本框与第一个 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>
最简单的答案是将第二个 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 中,您需要查看 this posting 以获得有关如何从会话中获取数据的指导。
关于您的代码的注释 - 传统鼓励您使用小写字母(用于单个单词)或驼峰式命名用于多单词变量名称。 Java 不强制执行,但鼓励这样做。
编辑
您在评论中提到您只想使用 servlet。但是您如何将 HTML 文件提供给浏览器?猜测您正在使用 Tomcat 或者 Jetty。这些服务器也有能力处理 JSP 文件。该文件将位于与您现在拥有的目录相同的目录中,但名称会有所不同。如果你不能使用 JSP 你有一些其他的选择,比 JSP:
更难
- 在 servlet 中嵌入 index3.html 的内容。基本上你会有一堆看起来像 this post 中的答案的代码,你会将你的字符串放入输出中。 问题:对页面的任何更改layout/color/wording都需要重新编译和重新部署。
- 在 index3.html 中创建一个 "sentinel" 值,例如“----myvalue----”。读取 index3.html 中的每一行并将标记值替换为您想要的值,然后重定向到您在替换期间创建的新文件。 问题:如果你想要两个值怎么办?现在你需要两个哨兵值。如果您对保存您提供的文件的目录只有读取权限怎么办?
- 使用Java脚本并向第二页传递一个URL参数。渲染时,从 URL 中提取值并渲染它。 问题:现在您需要处理两种语言。
老实说,所有这些(可能还有其他方法)都比 JSP 方法难。
假设
您在表单操作标记中指定的路径是正确的。
以下是有关如何执行此操作的简要指南:
第 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-->
希望对您有所帮助。
这是我的 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 页面中,我希望 "Edit Collection" 文本框与第一个 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>
最简单的答案是将第二个 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 中,您需要查看 this posting 以获得有关如何从会话中获取数据的指导。
关于您的代码的注释 - 传统鼓励您使用小写字母(用于单个单词)或驼峰式命名用于多单词变量名称。 Java 不强制执行,但鼓励这样做。
编辑
您在评论中提到您只想使用 servlet。但是您如何将 HTML 文件提供给浏览器?猜测您正在使用 Tomcat 或者 Jetty。这些服务器也有能力处理 JSP 文件。该文件将位于与您现在拥有的目录相同的目录中,但名称会有所不同。如果你不能使用 JSP 你有一些其他的选择,比 JSP:
更难- 在 servlet 中嵌入 index3.html 的内容。基本上你会有一堆看起来像 this post 中的答案的代码,你会将你的字符串放入输出中。 问题:对页面的任何更改layout/color/wording都需要重新编译和重新部署。
- 在 index3.html 中创建一个 "sentinel" 值,例如“----myvalue----”。读取 index3.html 中的每一行并将标记值替换为您想要的值,然后重定向到您在替换期间创建的新文件。 问题:如果你想要两个值怎么办?现在你需要两个哨兵值。如果您对保存您提供的文件的目录只有读取权限怎么办?
- 使用Java脚本并向第二页传递一个URL参数。渲染时,从 URL 中提取值并渲染它。 问题:现在您需要处理两种语言。
老实说,所有这些(可能还有其他方法)都比 JSP 方法难。
假设
您在表单操作标记中指定的路径是正确的。
以下是有关如何执行此操作的简要指南:
第 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-->
希望对您有所帮助。