gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:25:59 24 4
gpt4 key购买 nike

我在获取 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/

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