问题:为什么跟随 Servlet 正在打印“发生了其他事情”。LoginServlet 和 index.jsp 的结构如下。我使用的是 Netbeans 8.1 和 ApacheTomcat 8.0.9.0,浏览器是 chrome。
LoginServlet 的结构
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String contentType=request.getContentType();
PrintWriter out=response.getWriter();
//Why the following condition is failed.
if((contentType!=null)&&(contentType.indexOf("multipart/form-data")>=0)){//<==see this
DataInputStream in =new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
out.print("contentType is not null");
byte [] dataBytes=new byte[formDataLength];
}
if(contentType==null)
out.print("contentType is null");
else
out.print("other thing happended");//<== Why this is printing
}
index.jsp的结构
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
Name: <input type="text" name="name"> <br>
Password: <input type="password" name="password"> <br>
<input type="submit" value="login">
</form>
</body>
浏览器的输出是“发生了其他事情”。