作者noapaov (單身漢)
看板java
標題[JSP ] 關於servlet Exception問題
時間Sun May 10 11:43:45 2015
請問一下一個問題,
這麼功能主要是傳訂單資訊到servlet, 我這邊有判斷p_id(訂單如果為0)則顯示參數錯誤
但我發現如果我的URL為 :
http://localhost:8080/testDemo/servlet
也就士都不帶任何參數, 這時候在使用p_id.length()就會發生異常,
也就是我的code會停在呼叫pid_length的那一行, 但我這時候也捕捉不到錯誤
(catch也沒印出錯誤) ~ 麻煩各位 謝謝
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String pid = request.getParameter("p_id");
String uid = request.getParameter("uid");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
if(pid.length() != 0) {
out.println("<div>訂單資訊 pid : " + pid + " uid : "+ uid + " length : " + pid.length() +"</div>");
} else {
out.println("測試");
}
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
response.getWriter().write(e.getMessage());
StackTraceElement[] trace = e.getStackTrace();
for(int i = 0 ; i < trace.length ; i++){
response.getWriter().write(trace[i].toString());
response.getWriter().write("<br/>");
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.181.227
※ 文章網址: https://www.ptt.cc/bbs/java/M.1431229428.A.B3D.html
推 pttworld: keyworld:GET/POST getParameter usage 05/10 11:54
→ noapaov: 主要是String obj = null的關係, 但也沒有跳HTTP 500 05/10 13:17
→ noapaov: 但是 exception 有抓到錯誤, 只是getMessage 是 null 05/10 13:18
※ 編輯: noapaov (114.34.181.227), 05/10/2015 13:18:50
→ ssccg: 都知道原因是NPE,那抓完參數先檢查啊... 05/10 14:10
推 pttworld: 嗯,追問為什麼會是null 05/10 14:23
推 pttworld: 我會持續追問,如你能回答到最後請再回頭看我第一則推 05/10 14:24
→ pttworld: 文。 05/10 14:24
推 pttworld: 有神快拜!哈哈,趕流行。 05/10 14:40
推 yryangs: 良葛格!!! 05/11 13:50