Servlets 如何使用servlet从上一个html页面到下一个页面获取值?
这是我的Servlet: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
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更难的选择:
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-->