gpt4 book ai didi

java - 请求结束后请求属性未清除

转载 作者:太空宇宙 更新时间:2023-11-04 14:45:24 25 4
gpt4 key购买 nike

我的 jsp 中的请求属性有问题,我正在使用 mvc 模式并设置请求属性并从 servlet 转发到 jsp。该应用程序是一个银行应用程序,目标是使用客户 id 来获取客户,通过客户 id 将其传递给 Account 类来收集用户的所有帐户。我已经解决了这个问题,我可以毫无问题地获取帐户信息并进行处理。我遇到的问题是,当我关闭页面并从头开始再次运行时,我发现当我到达表时,它仍然放入前一个请求中的信息以及我请求的新信息。像这样:

(First Run)

second Run

我的代码如下,用于将我定向到 jsp 的 servlet 以及我在其中使用信息的 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>

我不太确定发生了什么,我从 session 属性开始,并认为这是我的问题,所以我转向使用请求属性。但我仍然面临同样的问题。我的印象是请求属性仅根据该请求持续,但似乎该信息仍在传递。有什么建议么?

最佳答案

这个问题的有趣之处在于它与请求属性变量无关。由于我正在实例化类中的对象列表,因此该对象列表将持续存在并且只是被添加到其中。我认为这主要是因为我使用的是可以附加的 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 类中的清除帐户方法非常非常简单:

  /**
* Clears the current array list of accounts
*/
public void clearAccounts(){
this.custAccounts.clear();
}

关于java - 请求结束后请求属性未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443351/

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