正常的java文件只執行main中調用的那些方法。但是,servlet做了什麼?我想也許它執行文件,但我試圖分裂一個HTTP servlets請求和響應,但它不起作用。使用servlet來讀取輸入流,然後執行一些可以調用響應寫入器方法的函數是一種很好的方式?使用Servlets?
這裏是我的試用代碼:
public void requestReader(HttpServletRequest req) {
try {
int len = req.getContentLength();
byte[] input = new byte[len];
ServletInputStream sin = req.getInputStream();
int c, count = 0 ;
while ((c = sin.read(input, count, input.length-count)) != -1) {
count +=c;
}
sin.close();
String inString = new String(input);
int index = inString.indexOf("=");
String value = inString.substring(index + 1);
inputStream = URLDecoder.decode(value, "UTF-8");
} catch (IOException e) {
}
}//end of requestReader
public void responseWriter(HttpServletResponse resp) {
try{
resp.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(resp.getOutputStream());
writer.write("Working");
writer.flush();
writer.close();
} catch (IOException e) {
}
}//end of responseWriter
不再擔心servlet和輸入流一分鐘,你究竟想要做什麼?你只是想顯示一個頁面或什麼? – aishwarya