[英]ServletException in retrieving data from db in jsp mvc
我想知道在我的JSP項目中使用MVC的網絡,並在這里找到了很好的討論,現在我有以下內容:
public class Subject {
private String subjectId = null;
private String subjectName = null;
public String getSubjectId() {
return subjectId;
}
public void setSubjectId(String subjectId) {
this.subjectId = subjectId;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
}
我的SubjectDAO
類是:
public class SubjectDAO {
public List<Subject> subjectList() throws SQLException{
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Subject> subjects = new ArrayList<>();
try {
connection = DatabaseManager.initConnection();
statement = connection.prepareStatement(Constant.SUBJECT_QUERY);
resultSet = statement.executeQuery();
while (resultSet.next()) {
Subject subject = new Subject();
subject.setSubjectId(resultSet.getString("_id"));
subject.setSubjectName(resultSet.getString("subj_name"));
subjects.add(subject);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
return subjects;
}
}
在我的ControllerServlet
我有:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
List<Subject> subjects = subjectDAO.subjectList();
request.setAttribute("subjects", subjects);
getServletContext().getRequestDispatcher("/content.jsp").forward(
request, response);
} catch (Exception e) {
throw new ServletException("Cannot obtain subjects from DB", e.getCause());
}
}
我的JSP作為View是:
<c:forEach items="${subjects}" var="subject">
<li
<c:catch>
<c:choose>
<c:when test="${subject.subjectId == param.subj_id}">
<c:out value="class=\"selected\""/>
</c:when>
</c:choose>
</c:catch>
><a href='/programming-iqs/admin-controller?action=manage-content&subj_id=
<c:out value="${subject.subjectId}"/>'>
<c:out value="${subject.subjectId}"/>
</a>
</li>
</c:forEach>
使用上面的代碼,我得到:
javax.servlet.ServletException: Cannot obtain subjects from DB
我無法解決此問題,我們將不勝感激。
在使用/實現MVC之前,我的JSP代碼是:
<%
ResultSet sujectResult = DatabaseManager.getSubjects();
while (sujectResult.next()) { %>
<li <%
String selectedSubject = request.getParameter("subj_id");
try {
if (selectedSubject.equals(sujectResult.getString("_id"))) {%>
class="selected" <%}%>><a
href='/programming-iqs/admin-controller?action=manage-content&subj_id=
<%= sujectResult.getString("_id")%>'>
<%= sujectResult.getString("subj_name")%>
</a> <%
} catch (NullPointerException e) {
}
} %></li>
並且當然該代碼運行得很好,現在請幫助我實現MVC。 謝謝
java.lang.IllegalStateException: Cannot forward after response has been committed
一般原因java.lang.IllegalStateException: Cannot forward after response has been committed
異常是以下任何一種:
out.write("something")
編寫一些響應的內容 例如:這是正確的-
if(condition1)
redirect to resource 1
else if(condition 2)
redirect to resource 2
else
redirect to resource 3
But this is wrong --
if(condition 1)
redirect to resource 1
again
redirect to resource 2
您可以添加e.printStackTrace();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
List<Subject> subjects = subjectDAO.subjectList();
request.setAttribute("subjects", subjects);
getServletContext().getRequestDispatcher("/content.jsp").forward(
request, response);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException("Cannot obtain subjects from DB", e.getCause());
}
}
然后,只需檢查您的應用服務器控制台即可。 它應該告訴你什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.