一:通过request对象获取客户端的信息
request.setCharacterEncoding("UTF-8");//不乱码
//通过request对象获取客户端的信息
String pass=request.getParameter("password");
String mail=request.getParameter("mail");
二:界面跳转:
//跳转"主面面main.jsp"
request.getRequestDispatcher("main.jsp").forward(request,response);//地址栏不变
三:判断邮箱格式时要用正则表达式
常用正则表达式:
参考原文链接:https://blog.csdn.net/qq_37870901/article/details/81079554
正则表达式学习:
https://www.runoob.com/regexp/regexp-tutorial.html
四:今日项目:
将注册页面register.jsp提交的信息显示在main.jsp,要求密码6位,邮箱要有@,电话11位。
***学有余力的同学,在注册页面中多添加一行:
提示:String[] getParameterValues(String name) 获得如checkbox类(名字相同,但值有多个)的数据。
源代码:
Register.jsp:
<%--
Created by IntelliJ IDEA.
User: ls010
Date: 2021/3/4
Time: 12:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>+
<html>
<head>
<title>Title</title>
<style type="text/css">
.style1 {
font-family: "华文行楷";
font-size: 36px;
color: #CC6;
text-align:center;
}
p{
text-align:center;
}
</style>
</head>
<body>
<form action="doRegister.jsp" method="post" name="form1" id="form1">
<p class="style1">注册页面</p>
<p>用户名:
<label>
<input type="text" name="username" />
</label>
</p>
<p>密 码:
<label>
<input type="password" name="password" />
</label>
</p>
<p>性 别:
<label>
<input name="sex" type="radio" value="男" />男
<input name="sex" type="radio" value="女" />女
</label>
</p>
<p> 邮 箱:
<label>
<input type="text" name="mail" />
</label>
</p>
<p>电 话:
<label>
<input type="text" name="tel" />
</label>
</p>
<p> 爱 好:
<label>
<input type="checkbox" name="hobby" value="跑步" />
跑步</label>
<label>
<input type="checkbox" name="hobby" value="上网" />
上网</label>
<label>
<input type="checkbox" name="hobby" value="阅读" />
阅读</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
<label>
<input type="reset" name="reset" value="重置" />
</label>
</p>
</form>
</body>
</html>
doRegister.jsp:
<%--
Created by IntelliJ IDEA.
User: ls010
Date: 2021/3/4
Time: 12:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 这是一个处理注册信息的页面 --%>
<h1 align="center">Errors!!!</h1>
<%
request.setCharacterEncoding("UTF-8");
//通过request对象获取客户端的信息
String pass=request.getParameter("password");
String mail=request.getParameter("mail");
String tel=request.getParameter("tel");
//out.print("name:"+name+",pass:"+pass);
if(tel.length()==11&&pass.length()==6&&mail.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")){
//跳转"主面面main.jsp"
request.getRequestDispatcher("main.jsp").forward(request,response);//地址栏不变
}
else{
out.println("要求密码6位,邮箱要有@,电话11位。\t");
}
%>
</body>
</html>
Main.jsp:
<%--
Created by IntelliJ IDEA.
User: ls010
Date: 2021/3/4
Time: 13:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Tiltel</title>
</head>
<body>
<h1 align="center">信息显示</h1>
<%
String name=request.getParameter("username");
String pass=request.getParameter("password");
String mail=request.getParameter("mail");
String tel=request.getParameter("tel");
String sex=request.getParameter("sex");
String[] hobby= request.getParameterValues("hobby");
out.println("用户名:"+name+"<br>"+ "密码:"+pass+"<br>"+ "邮箱:"+mail+"<br>"+ "电话号码:"+tel+"<br>"+ "性别:"+sex+"<br>"+"爱好:");
for(int i=0;i<hobby.length;i++)
out.println(hobby[i]);
%>
</body>
</html>
今日总结:
- 学习了如何使用request对象进行获取客户端信息和跳转界面
- 学习了如何用正则表达式解决判断邮箱格式问题
- 学习了如何获取复选框中的多个值(request.getParameterValues)