3
我寫了一個簡單的servlet,它接受HTTP POST請求併發送一個簡短的響應。下面是該servlet代碼:檢查HTTP POST請求的內容類型到Java servlet
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.*;
/**
* Servlet implementation class MapleTAServlet
*/
@WebServlet(description = "Receives XML request text containing grade data and returns response in XML", urlPatterns = { "/MapleTAServlet" })
public class MapleTAServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Log log = LogFactory.getLog(MapleTAServlet.class);
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String strXMLResponse = "<Response><code>";
String strMessage = "";
int intCode = 0;
ServletOutputStream stream = null;
BufferedInputStream buffer = null;
try
{
String strContentType = request.getContentType();
// Make sure that the incoming request is XML data, otherwise throw up a red flag
if (strContentType != "text/xml")
{
strMessage = "Incorrect MIME type";
}
else
{
intCode = 1;
} // end if
strXMLResponse += intCode + "</code><message>" + strMessage + "</message></Response>";
response.setContentType("text/xml");
response.setContentLength(strXMLResponse.length());
int intReadBytes = 0;
stream = response.getOutputStream();
// Converts the XML string to an input stream of a byte array
ByteArrayInputStream bs = new ByteArrayInputStream(strXMLResponse.getBytes());
buffer = new BufferedInputStream(bs);
while ((intReadBytes = buffer.read()) != -1)
{
stream.write(intReadBytes);
} // end while
}
catch (IOException e)
{
log.error(e.getMessage());
}
catch (Exception e)
{
log.error(e.getMessage());
}
finally
{
stream.close();
buffer.close();
} // end try-catch
}
}
而這裏的,我使用發送請求的客戶端:
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.*;
public class TestClient
{
/**
* @param args
*/
public static void main(String[] args)
{
BufferedReader inStream = null;
try
{
// Connect to servlet
URL url = new URL("http://localhost/mapleta/mtaservlet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Initialize the connection
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "text/xml");
//conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
OutputStream out = conn.getOutputStream();
inStream = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String strXMLRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Request></Request>";
out.write(strXMLRequest.getBytes());
out.flush();
out.close();
String strServerResponse = "";
System.out.println("Server says: ");
while ((strServerResponse = inStream.readLine()) != null)
{
System.out.println(strServerResponse);
} // end while
inStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
} // end try catch
}
}
我遇到的問題是,當我運行客戶端程序,我得到以下輸出:
Server says:
<Response><code>0</code><message>Incorrect MIME type</message></Response>
我已經打過電話request.getContentType(),並已經得到了「文本/ XML」作爲輸出。試圖找出爲什麼字符串不匹配。
是是這個正確的方式來確定HttpServletRequest的內容類型?我不滿意它,因爲當你查找確切的字符串匹配時,「text/xml」不同於「text/xml; charset = UTF-8」,這與「text/xml; charset = UTF-8」不同。這與「application/xml」不同。那麼有沒有一種方法可以乾淨地區別於來自表單請求的xml請求?因此不會手動覆蓋可以表示不同請求類型的所有字符串 – tObi