import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/ShowPersonServlet"})
public class ShowPersonServlet extends HttpServlet {
String key = "";
String lastName = "";
String firstName="";
HttpSession session;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
key = request.getParameter("txt_Key");
firstName = request.getParameter("txt_firstName");
lastName = request.getParameter("txt_lastName");
session = request.getSession();
session.setAttribute("key", key);
session.setAttribute("firstName", firstName);
session.setAttribute("lastName", lastName);
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 ShowPersonServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border=\"1\" width =\"400\" height=\"30\" >");
out.println(" <tbody>");
Enumeration e = session.getAttributeNames();
while ( e.hasMoreElements() )
{
String k = (String)e.nextElement();
Object value = session.getAttribute(k);
String k2 = (String)e.nextElement();
Object value2 = session.getAttribute(k2);
String k3 = (String)e.nextElement();
Object value3 = session.getAttribute(k3);
out.println("<tr>");
out.println("<td >"+ value +"</td>");
out.println("<td >"+ value2 +"</td>");
out.println("<td >"+ value3 +"</td>");
out.println("</tr>");
}
out.println(" </tbody>");
out.println("</table>");
out.println("<br>");
out.println("<form method=\"get\" action=\"ShowPersonServlet\">");
out.println("<table width=\"1200\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">");
out.println("<tbody>\n" +
" <tr>\n" +
" <td><label>Key=</label><br>\n" +
" </td>\n" +
" <td><input name=\"txt_Key\"><br>\n" +
" </td>\n" +
" <td>First Name=<br>\n" +
" </td>\n" +
" <td><input name=\"txt_firstName\"><br>\n" +
" </td>\n" +
" \n" +
" <td >Last Name=</td>\n" +
" <td><input name=\"txt_lastName\"><br>\n" +
" </td>\n" +
" <td><button type=\"submit\">Add</button><br>\n" +
" </td>\n" +
" </tr>\n" +
" </tbody>\n" +
"</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
}
我想在每次填充文本字段时将(键名和姓)添加到上表中,然后通过将它们存储在会话中来按添加。但问题是每次我添加新数据都会替换旧数据(没有创建新行!!)。请帮忙。!
答案 0 :(得分:0)
创建一个具有所需属性的类,然后将该实例存储在会话中,并将键作为属性键存储,或者您也可以创建一个List并将该List存储在会话中。如下所示。
class Person {
String key = "";
String lastName = "";
String firstName="";
//create setter/getter for attributes
}
然后在servlet中
Person person = new Person();
person.setKey(key)
person.setFirstName(firstName);
person.setLastName(lastName);
我建议维护List中的所有数据并将List添加到会话中。
List list = new ArrayList();
list.add(person);
session.setAttribute("list", list);
然后遍历列表以获取所有数据。您需要相应地修改servlet代码。
答案 1 :(得分:0)
您必须将用户详细信息存储在应用程序上下文变量中,而不是存储在会话变量中。会话适用于每个用户。因此,所有用户的列表必须是应用程序作用域而不是会话作用域。有关详细信息,请参阅ServletContext api