【发布时间】:2015-05-07 06:37:16
【问题描述】:
图片如下:
我有一个带有表单的 html/jsp 页面。
<div id = "divAttributes">
<form id = 'fid' method = "post" action = "RunQuery">
Id Number: <input type= "text" name = "foo" id = "txtFoo"/><br/>
<input type = "checkbox" id = "chboxZap" value = "zap"/>Foo<br/>
<input type = "checkbox" id = "chboxBar" value = "bar"/>Bar<br/>
<input type= "submit" id = "btnSubmit" value = "submit" onclick = "setDisabled('divAttributes', true)"/><br/>
</form>
</div>
当用户按下提交按钮时,我想将表单中包含的信息发送到一个servlet,然后它会做一些处理并返回一个字符串。
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
ReturnCode rc = world.hello.MyMainClass.wait(request.getParameter("foo"));
/*I want to return the RC, which is a bool, a string, and another object, which in this case is a string*/
}
然后该字符串应该被发送到不同的 servlet,然后保存一个文件。
我已经将 servlet 保存到文件中:
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.setHeader("Content-Disposition",
"attachment;filename=downloadname.txt");
ServletContext ctx = getServletContext();
String s = new String(request.getParameter("data"));
InputStream is = new ByteArrayInputStream(s.getBytes());
int read = 0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
os.flush();
os.close();
}
我有两个问题:
调用第二个 servlet 时,不会重定向到新页面。它只是立即提供下载对话框。但是,当我调用第一个 servlet 时,它提供了一个空白的 html 页面。为什么?
如何将值从 servlet 返回到调用它的 HTML 页面,并从那里访问它们?
【问题讨论】: