javaweb 测试
题目要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
代码:
1连接数据库
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库连接工具
* @author Hu
*
*/
public class DBUtil {
public static String db_url = "jdbc:mysql://localhost:3306/zzm?useSSL=false";
public static String db_user = "root";
public static String db_pass = "123456";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private int id;
private String zhanghao;
private String password;
private String sex;
private String number;
private String name;
private String mail;
private String xueyuan;
private String zhuanye;
private String banji;
private String year;
private String shengyuandi;
private String beizhu;
public int getId()
{
return id;
}
public void setId()
{
this.id=id;
}
public String getZhanghao()
{
return zhanghao;
}
public void setZhanghao()
{
this.zhanghao=zhanghao;
}
public String getPassword()
{
return password;
}
public void setPassword()
{
this.password=password;
}
public String getSex()
{
return sex;
}
public void setSex()
{
this.sex=sex;
}
public String getName()
{
return name;
}
public void setName()
{
this.name=name;
}
public String getMail()
{
return mail;
}
public void setMail()
{
this.mail=mail;
}
public String getNumber()
{
return number;
}
public void setNumber()
{
this.number=number;
}
public String getXueyuan()
{
return xueyuan;
}
public void setXueyuan()
{
this.xueyuan=xueyuan;
}
public String getZhuanye()
{
return zhuanye;
}
public void setZhuanye()
{
this.zhuanye=zhuanye;
}
public String getBanji()
{
return banji;
}
public void setBanji()
{
this.banji=banji;
}
public String getYear()
{
return year;
}
public void setYear()
{
this.year=year;
}
public String getShengyuandi()
{
return shengyuandi;
}
public void setShengyuandi()
{
this.shengyuandi=shengyuandi;
}
public String getBeizhu()
{
return beizhu;
}
public void setBeizhu()
{
this.beizhu=beizhu;
}
public Lzx() {}
public Lzx(int id,String zhanghao,String password,String sex,String name,String number,String mail,String xueyuan,String zhuanye,String banji,String year,String shengyuandi,String beizhu)
{
this.id=id;
this.zhanghao = zhanghao;
this.password = password;
this.name = name;
this.sex = sex;
this.number =number;
this.name = name;
this.mail = mail;
this.xueyuan = xueyuan;
this.zhuanye = zhuanye;
this.banji = banji;
this.year = year;
this.shengyuandi = shengyuandi;
this.beizhu = beizhu;
}
public Lzx(String zhanghao, String password,String sex, String name,String number,String mail,String xueyuan,String zhuanye,String banji,String year,String shengyuandi,String beizhu)
{
this.zhanghao = zhanghao;
this.password = password;
this.name = name;
this.sex = sex;
this.number = number;
this.mail = mail;
this.xueyuan = xueyuan;
this.zhuanye = zhuanye;
this.banji = banji;
this.year = year;
this.shengyuandi = shengyuandi;
this.beizhu = beizhu;
}
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
*
* Dao层操作数据
* @author Zhao
*
*/
public class lzxDao {
* 添加
* @param zzm
* @return
*/
public boolean add(Lzx zzm) {
String sql = "insert into zzm(zhanghao,password,sex,name,number,mail,xueyuan,zhuanye,banji,year,shengyuandi,beizhu) values('" + zzm.getZhanghao() + "','" + zzm.getPassword() + "','" + zzm.getSex() + "','"+zzm.getName()+"','" + zzm.getNumber() + "','" + zzm.getMail()+"','"+zzm.getXueyuan()+"','"+zzm.getZhuanye()+"','"+zzm.getBanji() +"','"+zzm.getYear()+"','"+zzm.getShengyuandi()+"','"+zzm.getBeizhu() +"')";
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
// TODO Auto-generated method stub
return false;
}
/**
* ZzmService
* 服务层
* @author Zhao
*
*/
public class LzxService {
/**
* 添加
* @param Zzm
* @return
*/
public boolean add(Lzx zzm) {
boolean f = false;
if(!cDao.zhanghao(zzm.getZhanghao())) {
cDao.add(zzm);
f = true;
}
return f;
}
}
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LzxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
if ("add".equals(method)) {
add(req, resp);
}
}
* 添加
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
String zhanghao = req.getParameter("zhanghao");
String password = req.getParameter("password");
String sex = req.getParameter("sex");
String number = req.getParameter("number");
String name = req.getParameter("name");
String mail = req.getParameter("mail");
String xueyuan = req.getParameter("xueyuan");
String zhuanye = req.getParameter("zhuanye");
String banji = req.getParameter("banji");
String year = req.getParameter("year");
String shengyuandi = req.getParameter("shengyuandi");
String beizhu = req.getParameter("beizhu");
Lzx zzm= new Lzx(zhanghao,password,sex,name,number,mail,xueyuan,zhuanye,banji,year,shengyuandi,beizhu);
//添加后消息显示
if(service.add(zzm)) {
req.setAttribute("message", "添加成功");
req.getRequestDispatcher("zzm.jsp").forward(req,resp);
} else {
req.setAttribute("message", "用户名重复,请重新输入");
req.getRequestDispatcher("zzm.jsp").forward(req,resp);
}
}
}
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="font/iconfont.css">
<script type="text/javascript" src="js/nav.js"></script>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<form action="LzxServlet?method=add" method="post" onsubmit="return check()">
<td width="100"><font color="black"style="font-family:宋体;">登录账号:</font></td>
<td width="400"><input id="zhanghao" name="zhanghao" placeholder="请输入账号"/></td>
</tr>
<tr height="50px" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">登录密码:</font></td>
<td width="400"><input type="password" name="password" placeholder="请输入密码"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">性别:</font></td>
<td><select id="sex" name="sex" >
<option value="男">男</option>
<option value="女">女</option>
<select></td>
</tr>
<tr height="50px" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">姓名:</font></td>
<td width="400"><input id="name" name="name" placeholder="请输入姓名"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">学号:</font></td>
<td width="400"><input id="number" name="number" placeholder="请输入学号"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">电子邮箱:</font></td>
<td width="400"><input id="mail" name="mail" placeholder="请输入邮箱" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在学院:</font></td>
<td width="400"><input id="xueyuan" name="xueyuan" placeholder="请输入学院名" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在专业:</font></td>
<td width="400"><input id="zhuanye" name="zhuanye" placeholder="请输入专业" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在班级:</font></td>
<td width="400"><input id="banji" name="banji" placeholder="请输入班级" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">入学年份:</font></td>
<td><select id="year" name="year" >
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<select></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">生源地:</font></td>
<td width="400"><input id="shengyuandi" name="shengyuandi" placeholder="请输入生源地" /></td>
</tr>
<tr height="100" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">备注:</font></td>
<td width="400"><input id="beizhu" name="beizhu" placeholder="无" /></td>
</tr>
<td width="400"><button type="submit" >添加<button></td>
<td width="400"><input type="reset" value="重新输入" /></td>
</tr>
</form>
</div>
<script type="text/javascript">
function check() {
var zhanghao = document.getElementById("zhanghao");
var password = document.getElementById("password");
var number = document.getElementById("number");
var mail = document.getElementById("mail");
if (!zhanghao.value.match(/^[a-zA-Z]\w{5,11}$/)) {
else if(number.value<"20180000"|| number.value>"20189999")
{
alert(" 学号由2018开头的八位组成");
number.focus();
}
</script>
</body>
</html>
javaweb 测试的更多相关文章
- Javaweb测试
1.建立web 工程 输入Project name.然后点击finish. 2.右键点击WebContent-New-JSP File,新建jsp文件. 3.然后编写一个jsp登录的程序 <%@ ...
- IDEA新建javaWeb以及Servlet简单实现
刚开始用IDEA开发,还不太熟悉,因此写一个教程,加深印象 1.新建一个Web项目 两种方法:java 和 Java Enterprise(推荐) 第一种)通过Java工程创建,这个方法需要手动导入T ...
- Intellij IDEA创建javaWeb以及Servlet简单实现
1.创建web工程 File --> New --> Project...
- Intellij IDEA创建javaweb步骤详解
一.创建并设置javaweb工程 1.创建javaweb工程File --> New --> Project... 设置工程名字: 创建完成后工程结构如下: 2. Web工程设置2.1 在 ...
- [转]Intellij idea创建javaWeb以及Servlet简单实现
准备:1. 安装jdk1.72. 安装tomcat1.8 一.创建并设置javaweb工程 1.创建javaweb工程File --> New --> Project... 设置工程名字: ...
- Inellij idea创建javaWeb以及Servlet简单实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 准备:1. 安装jdk1.7 2. 安装tomcat1.8 一.创建并设置javaweb工程 1.创建javaweb工程File --> N ...
- Intellij idea创建javaWeb:实现JSP/Servlet(转)
转自:https://www.jianshu.com/p/9684e90cf7b5 Intellij idea创建javaWeb:实现JSP/Servlet by_love唯念 关注 2016.12. ...
- IDEA下配置JavaWeb项目
前提 已安装IDEA专业版 已安装Tomcat 已安装JDK 创建JavaWeb项目 File --> New --> Project... 设置工程名字: 创建完成后工程结构如下: 设置 ...
- WebSocket简单介绍
Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...
随机推荐
- Windows配置Delve的测试环境
引言 自己最近在玩Go,在开发一些项目的时候需要调试,由于之前都是在GoLand上写的,但是这个IDE启动太慢并且不轻便.并且自己之前很多项目都是在Vscode中编写的,所以特意想在Vscode中配置 ...
- 数据可视化之DAX篇(二十四)Power BI应用技巧:在总计行实现条件格式
https://zhuanlan.zhihu.com/p/98975646 如何将表格或者矩阵中值的条件格式也应用于总计行? 目前PowerBI并不支持这种功能,无法在总计行或者小计行上应用条件格式, ...
- JavaScript 基础 学习 (一)
JavaScript 基础 学习 获取页面中的元素的方法 作用:通过各种方式获取页面中的元素 比如:id,类名,标签名,选择器 的方式来获取元素 伪数组: 长的和数组差不多,也是按照索引排 ...
- ref和动态组件
ref--------指引 另一种获取表单值的方法 是Vue环境中一个内置的属性.它可以使用this.$refs可以快速拿到DOM对象.
- 爆肝整理:Linux常用命令,建议收藏!
目录管理命令:mkdir.rmdir mkdir命令 rmdir命令 文件管理命令:cp.mv.rm cp命令 mv命令 rm命令 文件查看命令:cat.tac.head.tail.more.less ...
- 给咱的WP站点搬家
前言 WordPress 作为全球最流行的博客系统,使用简单,功能丰富,用它来建站的用户非常多.对于站长们来说,网站搬家也是少不了的,有时我们需要更换主机空间,把网站从一个服务器迁移到另一个服务器上, ...
- IDEA 2020.1.2安装破解激活 idea 2020.1.3下载 安装 一键破解
IDEA 2020.1.2 idea 2020.1.3下载 安装 破解 本项目只做个人学习研究之用,不得用于商业用途!若资金允许,请点击链接购买正版,谢谢合作!学生凭学生证可免费申请正版授权!创业公司 ...
- [redis] -- 为什么那么快
1 完全基于内存 2 数据结构简单,对数据操作也简单 3 只有单线程,避免了不必要的上下文切换,也不存在竞态,不存在多进程或多线程导致的切换而消耗CPU,不用开了各种锁的问题 4 使用多路IO复用模型 ...
- 二分图&网络流初步
链接 : 最小割&网络流应用 EK太低级了,不用. 那么请看:#6068. 「2017 山东一轮集训 Day4」棋盘,不用EK你试试? dinic模板及部分变形应用见zzz大佬的博客:网络流学 ...
- 附002.Nginx全系列大总结
Nginx全系列总结如下,后期不定期更新. 欢迎基于学习.交流目的的转载和分享,禁止任何商业盗用,同时希望能带上原文出处,尊重ITer的成果,也是尊重知识. 若发现任何错误或纰漏,留言反馈或右侧添加本 ...