0%

多个请求使用同一个Servlet

多个请求使用同一个Servlet可以采用以下两种方案
第一种方案:在url加上入参method,根据method进行分发

第二种方案:web.xml使用*.do来匹配Servlet,根据 request.getServletPath()然后反射调用方法

表单的重复提交

重复提交的情况

  • 在表单提交到一个Servlet,Servlet又通过请求转发的方式响应了一个页面,此时地址栏还保留着Servlet的那个路径,在响应页面点击刷新
  • 在响应没有返回时重复点击提交按钮
  • 点击返回,再点击提交

不是重复提交的情况

点击返回之后,刷新页面,再点击提交

如何避免表单的重复提交

使用session,生成属性,移除属性

post请求体内容无法重复获取

为什么会无法重复读取呢?

以tomcat为例,在进行请求体读取时实际底层调用的是org.apache.catalina.connector.Request的getInputStream()方法,而该方法返回的是CoyoteInputStream输入流

1
2
3
4
5
6
7
8
9
10
11
12
13
public ServletInputStream getInputStream() throws IOException {

if (usingReader) {
throw new IllegalStateException(sm.getString("coyoteRequest.getInputStream.ise"));
}

usingInputStream = true;
if (inputStream == null) {
inputStream = new CoyoteInputStream(inputBuffer);
}
return inputStream;

}

在使用CoyoteInputStream进行读取时

1
2
3
4
5
6
7
8
9
10
11
12
13
public int read(byte[] b, int off, int len) throws IOException {
// 如果流关闭,则抛出异常
if (closed) {
throw new IOException(sm.getString("inputBuffer.streamClosed"));
}
// 如果已经读完了,则返回-1
if (checkByteBufferEof()) {
return -1;
}
int n = Math.min(len, bb.remaining());
bb.get(b, off, n);
return n;
}

而流读取完毕都会进行close,这个流close之后,close状态就置为了true,所以导致流无法进行二次读取

阅读全文 »

解决方案:

应该是tomcat以前部署的项目有残留,删除掉webapps里面的其他项目,删除掉work文件夹下Catalina下localhost文件夹里其他的项目,删除掉conf文件夹下Catalina下localhost文件夹下的其他项目配置,最后如果还是不行,查看一下conf下的server.xml里是不是有其他项目残留的配置

测试页面,无需关心

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

阅读全文 »

错误的文字内容:Diagnostic:Exception in windowonload: Error An error has occurrediSPlugin.3005StackTrace:Error An error has occurredJSPlugin.3005 at getString

阅读全文 »