我正在为注册表单编写验证servlet。表单位于.jsp文件中,并具有以下布局:
<div class="content">
<h2 class="form">Register</h2>
<form action="Register">
<p>
User name: <br />
<input type="text" name="username" /><br /> First Name: <br />
<input type="text" name="fist_name" /><br /> Last Name: <br />
<input type="text" name="last_name" /><br /> Email Address: <br />
<input type="text" name="email" /><br /> Password: <br />
<input type="password" name="password" /><br /> Retype password:
<br />
<input type="password" name="password2" /><br /> <input
type="submit" value="Register" />
</p>
</form>
<!-- end .content -->
</div>
验证代码应检查每个字段是否满足一组约束,如果不符合,则写回jsp错误消息。
ValidationServlet.java
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// get request parameters for username and password
String user = request.getParameter("username");
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String email = request.getParameter("email");
String pwd = request.getParameter("password");
String pwd2 = request.getParameter("password2");
// Validate parameters and forward them to the ejb
Map<String, String> messages = new HashMap<String, String>();
if(user != null) {
// Validate user name
if(user.trim().isEmpty()) {
messages.put("error", "Please enter username");
} else if(!user.matches("^[a-zA-Z0-9]$")) {
messages.put("error", "Invalid username.");
} else if(user.length() > MAX_LEN){
messages.put("error", "Invalid username");
} else {
// Username is valid
messages.put("success", "good");
}
}
if(firstName != null) {
// Validate first name
if(firstName.trim().isEmpty()) {
messages.put("error", "Please enter your first name");
} else if(!firstName.matches("^[a-zA-Z]$")) {
messages.put("error", "Invalid name. Use alpha values only.");
} else if(firstName.length() > MAX_LEN){
messages.put("error", "Invalid name. Name should be at most " + MAX_LEN + " characters long");
} else {
// First name is valid
messages.put("success", "good");
}
}
if(lastName != null) {
// Validate last name
if(lastName.trim().isEmpty()) {
messages.put("error", "Please enter your last name");
} else if(!lastName.matches("^[a-zA-Z]$")) {
messages.put("error", "Invalid name. Use alpha values only.");
} else if(lastName.length() > MAX_LEN){
messages.put("error", "Invalid name. Name should be at most " + MAX_LEN + " characters long");
} else {
// Last name is valid
messages.put("success", "good");
}
}
if (email != null) {
// Email is specified as request parameter, do the business logic here.
if (email.trim().isEmpty()) {
messages.put("error", "Please enter email");
} else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) {
messages.put("error", "Invalid email, please try again.");
} else {
// Email is valid
messages.put("success", "good");
}
}
if(pwd != null) {
// Validate password
if(pwd.trim().isEmpty()) {
messages.put("error", "Please enter password");
} else if(pwd.length() > MAX_LEN){
messages.put("error", "Invalid password");
} else {
// Password is valid
messages.put("success", "good");
}
}
if(pwd2 != null) {
// Validate password
if(pwd2.trim().isEmpty()) {
messages.put("error", "Please enter password");
} else if(pwd2.length() > MAX_LEN){
messages.put("error", "Invalid password");
} else if(!pwd2.equals(pwd)) {
messages.put("error", "Passwords do not match");
} else {
// Password is valid
messages.put("success", "good");
}
}
// Put messages in request scope so that it's accessible in EL by
// ${messages}.
request.setAttribute("messages", messages);
// Forward request to JSP for display.
request.getRequestDispatcher("login.jsp").forward(request,
response);
}
我的问题是如何从哈希映射中访问每条消息并在jsp中的相应输入字段之前相应地显示它们?
答案 0 :(得分:5)
您需要更改每个输入字段的键
例如用于错误消息
messages.put("user", "Please enter username"); //for user
messages.put("firstName", "Please enter your first name"); //for first_name
.......
.......
.......
如果您想显示成功,那么您可以在地图中设置成功,否则不能。
访问JSP上的message
地图
<form action="Register">
<p>
User name: <br />
<input type="text" name="username" /><c:out value="${messages['user']}"/><br />
First Name: <br />
<input type="text" name="fist_name" /><c:out value="${messages['firstName']}"/><br />
.........
.........
.........
<input type="submit" value="Register" />
</p>
</form>
答案 1 :(得分:1)
要访问messages
地图,请使用以下代码:
<c:set var="what" value="error"/>
error: <c:out value="${messages[what]}"/>