gpt4 book ai didi

java - servlet 中似乎没有正确捕获 id 值

转载 作者:行者123 更新时间:2023-12-01 18:57:19 25 4
gpt4 key购买 nike

如果我在实现 servlet 中任意分配一个“id”值,它就可以工作;但是,如果我不这样做,它会正确地从抽象类继承 id 值,并跳过“if”语句以转发到“then”语句中提供的 url。是什么赋予了?可以判断“id”变量出了什么问题:

抽象 servlet 片段:

 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 片段:

 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);
}
}

当上面的 servlet 'else' 代码不在条件语句中时,下面的 jsp 代码将起作用:

 <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 是类字段,否则您不会向我们展示所有代码。也不清楚你所说的“它不起作用”是什么意思。预期内容为空?你有异常(exception)吗?

关于java - servlet 中似乎没有正确捕获 id 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com