老师,为什么我的userList集合里面没有数据呢
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path=request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>注册界面</title> </head> <body> <center> <h1>用户注册</h1> <form action="<%=basePath%>/RegistServlet" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>用户名</td> <td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" pattern="[\w]{3,12}"></td> </tr> <tr> <td>密 码</td> <td><input type="password" name="password" placeholder="密码长度为6-12位的纯数字" id="password" pattern="[\d]{6,12}"></td> </tr> <tr> <td>确认密码</td> <td><input type="password" name="checkPWD" placeholder="密码长度为6-12位的纯数字" pattern="[\d]{6,12}"></td> </tr> <tr> <td>手机号码</td> <td><input type="text" name="phone" placeholder="请输入正确的手机号码格式" pattern="1[3578]\d{9}"></td> </tr> <tr> <td>邮箱</td> <td><input type="email" name="email" placeholder="请输入正确邮箱格式" required="required"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="注册"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
package com.imooc.servlet;
import java.io.IOException;
import java.util.List;
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 com.imooc.model.User;
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
String checkPWD=request.getParameter("checkPWD");
String phoneNum=request.getParameter("phone");
String email=request.getParameter("email");
String usernameRegex="[\\w]{3,12}";
String passwordRegex="[\\d]{6,12}";
String phoneRegex="1[3578]\\d{9}";
String emailRegex="[\\w] {3,}@([a-aA-Z]+|\\d+)(\\.[a-zA-Z]+)+";
boolean flag1=username.matches(usernameRegex);
boolean flag2=password.matches(passwordRegex);
boolean flag3=checkPWD.matches(passwordRegex);
boolean flag4=phoneNum.matches(phoneNum);
boolean flag5=email.matches(emailRegex);
List<User> userList=(List<User>)this.getServletContext().getAttribute("UserList");
if(flag1&&flag2&&flag3&&flag4&&flag5) {
User user=new User();
user.setUsername(username);
user.setPassword(password);
user.setPhoneNum(phoneNum);
user.setEmail(email);
userList.add(user);
}
System.out.println(userList);
this.getServletContext().setAttribute("UserList", userList);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星