我的jsp中的请求属性有问题,我正在使用mvc模式并设置请求属性并从servlet转发到jsp。该应用程序是一个银行应用程序,目标是使用客户ID通过客户ID获取客户将其传递给Account类以收集用户的所有帐户。我解决了这个问题我可以获得帐户信息并处理它没有任何问题。我遇到的问题是当我关闭页面并从头开始运行时,我发现当我到达表时,它仍然会输入上一个请求的信息以及我请求的新信息。像所以:
()
我的代码如下所示,指向我的servlet指向我使用信息的jsp和jsp。
AccountServlet.java:
package com.ChattBank.controller;
import com.ChattBank.business.Account;
import com.ChattBank.business.Accounts;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author AARONS
*/
public class AccountServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AccountServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AccountServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//HttpSession session = request.getSession();
String action = request.getParameter("action");
if (action == null) {
request.getRequestDispatcher("/Welcome.jsp").forward(request, response);
} else if (action.equals("view")) {
Accounts acct = new Accounts();
ArrayList<Account> list = new ArrayList();
try {
acct.setCustAccounts(request.getParameter("id"));
list.addAll(acct.getCustAccounts());
System.out.println(request.getParameter("id"));
} catch (SQLException ex) {
Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);
}
request.setAttribute("acctList", list);
request.getServletContext().getRequestDispatcher("/accounts.jsp").forward(request, response);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String custId = request.getParameter("custID");
if (action == null) {
request.getRequestDispatcher("/Welcome.jsp").forward(request, response);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
accounts.jsp:
<%--
Document : accounts
Created on : Jun 25, 2014, 12:24:38 AM
Author : Richard Davy
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ChattBank.business.Account"%>
<%@page import="com.ChattBank.business.Accounts"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Your Accounts</title>
</head>
<body>
<%
ArrayList<Account> list = new ArrayList();
list.addAll((ArrayList)request.getAttribute("acctList"));
for (Account custAccount : list) {
System.out.println("From JSP: " + custAccount.getCustId() + " " + custAccount.getAcctNo() + " " + custAccount.getAcctType() + " " + custAccount.getBalance());
System.out.println("Getting Object From Section");
}
%>
<h1>You Made It Here!</h1>
<table border="1" width="2" cellspacing="5" cellpadding="2">
<thead>
<tr>
<th colspan="10">Your Chatt Accounts</th>
</tr>
</thead>
<tbody>
<% for (int i = 0; i < list.size(); i++){ %>
<tr>
<td colspan="2">Account: </td>
<td colspan="2"><%= list.get(i).getCustId() %></td>
<td colspan="2"><%= list.get(i).getAcctNo() %></td>
<td colspan="2"><%= list.get(i).getAcctType() %></td>
<td colspan="2"><%= list.get(i).getBalance() %></td>
</tr>
<% } %>
<% list.clear(); %>
</tbody>
</table>
<p>Thank you for your business!</p>
</body>
</html>
我不确定发生了什么,我开始使用会话属性,并认为这是我的问题,所以我转向使用请求属性。但我仍然面临同样的问题。我的印象是,请求属性仅在该请求中持续,但似乎信息仍在进行中。有什么建议?
答案 0 :(得分:0)
关于这个问题的有趣之处在于它与请求属性变量没有任何关系。因为我在类中实例化对象列表,所以对象列表是持久的并且只是被添加到。我认为这主要是因为我使用的是能够附加到的ArrayList。我没有重写整个课程以找出一些工作,而是包含了一个try {} finally {}语句,如下所示:
package com.ChattBank.controller;
import com.ChattBank.business.Account;
import com.ChattBank.business.Accounts;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author AARONS
*/
public class AccountServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AccountServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AccountServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//HttpSession session = request.getSession();
String action = request.getParameter("action");
if (action == null) {
request.getRequestDispatcher("/Welcome.jsp").forward(request, response);
} else if (action.equals("view")) {
Accounts acct = new Accounts();
ArrayList<Account> list = new ArrayList();
try {
acct.setCustAccounts(request.getParameter("id"));
list.addAll(acct.getCustAccounts());
System.out.println(request.getParameter("id"));
//This was moved into the try block itself
request.setAttribute("acctList", list);
request.getServletContext().getRequestDispatcher("/accounts.jsp").forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);
//This is the added finally statement with the clearAccounts method
} finally {
acct.clearAccounts();
}
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String custId = request.getParameter("custID");
if (action == null) {
request.getRequestDispatcher("/Welcome.jsp").forward(request, response);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Accounts类中的clear accounts方法非常简单:
/**
* Clears the current array list of accounts
*/
public void clearAccounts(){
this.custAccounts.clear();
}