java - 全局变量未被覆盖函数更新

标签 java

我在获取 doGet 的覆盖函数来更新我的全局 arrayList 变量“list”时遇到问题,以便我可以在 doPost 函数内部对其进行迭代。我对java有点生疏,因为我有一段时间没有使用它了,所以除了怀疑它与我使用覆盖函数有关之外,我似乎找不到问题。感谢任何帮助,代码如下。

public class MembershipControllerServlet extends HttpServlet {

    public static ArrayList <String> list = new ArrayList();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String action=request.getParameter("action");
        String url="/index.html";

        if (action == null) {
            action="";
        }
        else if (action.equals("signup")) {
            url="/signup.jsp";
        }
        else{
            String m = "";
            String name = request.getParameter("name");
            String user = request.getParameter("userName");
            String password = request.getParameter("password");
            String address = request.getParameter("address");
            String countries[] = request.getParameterValues("countries");
            String zip = request.getParameter("zip");
            String email = request.getParameter("email");
            String male = request.getParameter("male");
            String female = request.getParameter("female");
            String langs[] = request.getParameterValues("lang");


         if (name.isEmpty() || user.isEmpty() || password.isEmpty() || address.isEmpty() || countries.length ==0 || zip.isEmpty() ||
                 email.isEmpty() || male == null || female == null || langs.length ==0) 
         {
            m = "Please enter all data";  
            list.add(m);
         }
         else
         {
           m = "name: " + name;
           list.add(m);
         }
        }


         processRequest(request, response);
        getServletContext().getRequestDispatcher(url).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 {

     try (PrintWriter out = response.getWriter()) {
         /* TODO output your page here. You may use following sample code. */

         String newmsg= "";
         for (Object li : list) {
             newmsg += li;
         }
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MembershipControllerServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>" + newmsg + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }

和 xml 文件:

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>MembershipControllerServlet</servlet-name>
        <servlet-class>edu.uncc.nbad.MembershipControllerServlet</servlet-class>
    </servlet>


     <servlet-mapping>
        <servlet-name>MembershipControllerServlet</servlet-name>
        <url-pattern>/membership</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
       <welcome-file>signup.jsp</welcome-file>
    </welcome-file-list>

</web-app>

最后signup.jsp:

<!DOCTYPE html>
<head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <h1>Registration Form</h1>

        <form method = "post" action = "/Lab3-Hitz-Herron/membership?action=signup">
            <label>Name</label><input class = 'box' type = 'text' name = "name"><br />
            <label>UserName:</label><input class = 'box' type = 'text' name =
            "userName"><br />
            <label>Password: </label><input class = 'box' type = 'text' name =
            "password"><br />
            <label>Address:</label><input class = 'box' type = 'text' name =
            "address"><br />
            <label>Country:</label>
            <select class= "box" name = 'countries'>
                <option value="USA">USA</option>
                <option value="OTHER">Other</option> </select
            ><br />
            <label>Zip Code:</label> <input class="box" type="text" name ="zip" /><br />
            <label>Email: </label> <input class="box" type="email" email namd ="email" /><br />
            <label>Sex: </label> <input type="radio" name="gender" value="male" name ="male" />Male
            <input type="radio" name="gender" value="female" name = "female"/>Female<br />
            <label>Language:</label>
            <input type="checkbox" name="language" value="English" name ="lang" />English
            <input type="checkbox" name="language" value="French" name ="lang"/>French
            <input type="checkbox" name="language" value="German" name ="lang" />German <br />
            About:
            <textarea name = "about"></textarea><br />
                        <button type="submit" >Submit</button>
        </form>
    </body>
</html>

最佳答案

TL;博士

永远不要这样做!

此实现存在并发问题,称为竞争条件,因为列表将在线程之间共享。

关于java - 全局变量未被覆盖函数更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050612/

相关文章:

java - 在 Gradle 应用程序插件中部署其他文件

Java GUI - 读取文本文件,其中文本文件的每一行都是它自己的字符串

java - 查询对象的数组列表

java - Terracotta Ehcache : server disconnects during debug

java - 连接重置错误 - REST 服务客户端

java - 编写 Hibernate HQL 时出错

java - bean 反射

带有通配符的 Java 抽象泛型方法用具体类型实现

java - % 2147483647L 在纪元时间是什么意思?

java - 创建一个全局对象或多个局部对象