正確捕獲如果我任意分配在執行servlet的工作原理的「id」值;但是,如果我沒有正確地從抽象類繼承id值並且跳過'if'語句轉發到'then'語句中提供的url。是什麼賦予了?能告訴什麼是錯的 '身份證' 變量:ID似乎並沒有被在servlet的
摘要的servlet snipet:
protected Integer id = null;
private void _doProcess(final HttpServletRequest request, final HttpServletResponse response)
throws IOException, ServletException {
try {
response.setContentType("text/html");
writer = response.getWriter();
final String idString = request.getParameter("id");
if(StringUtil.isNotEmptyStringTrimmed(idString)){
try {
id = Integer.parseInt(idString.trim());
} catch (NumberFormatException e) {
id = null;
}
}
doProcess(request,response);
} finally {
id = null;
}
try {
writer.close();
} catch (Exception e) {
// no-op
}
}
實施的servlet snipet:
public void doProcess(final HttpServletRequest request, final HttpServletResponse response)
throws IOException, ServletException {
// set page title
final HttpSession session = request.getSession();
session.setAttribute("pageTitle", "Training Project 5: Author");
if (id == null){
request.setAttribute("authorNamesList", printAuthorNames());
request.getRequestDispatcher("authorList.jsp").include(request,response);
}else{
final Author author = BEAN_FACTORY.getMember(id);
session.setAttribute("authorId",author.getId());
session.setAttribute("name", author.getName());
session.setAttribute("bio", author.getBio());
session.setAttribute("picture",author.getPicture());
session.setAttribute("bookTitles", printBookTitles(author.getId()));
request.getRequestDispatcher("authorPage.jsp").include(request,response);
}
}
下面的JSP代碼工作時進行如上的servlet '其他'代碼不在條件語句中:
<div id="right">
<table class="display" summary="Author Information">
<tr>
<td><span class="brown">Author Id: <c:out value="${authorId}"/></span></td>
</tr>
<tr>
<td><span class="brown">Name: <c:out value="${name}"/></span></td>
</tr>
<tr>
<td><span class="brown">Bio: <c:out value="${bio}"/></span></td>
</tr>
<tr>
<td>
<p>
<span class="brown"><img src="<c:out value="${picture}"/>" alt= ""/></span>
</p>
</td>
</tr>
什麼是'id'變量?它在哪裏定義?它在哪裏獲得價值? – Isaac
是否爲ID未定義?它是否初始化? – Rayshawn
id變量在此servlet擴展的抽象類中定義: – Mike