第一个JavaWeb项目

document.getElementById().value

这个卡了很久,一直报错null,最后问学长发现是input的id忘了设置…

运行截图:

 

 

 

 

 

 

以下是源代码部分:

网页部分:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息添加页面</title>
<link href="Resource/image/logo.ico" rel="icon" sizes="32x32">
</head>
<style>
input[type=submit]{
    padding:5px 100px;
    border-style:none;
    background-color:#e0e0e0;
    font-family:Sans-serif;
}
#top_header{
    font-family:Sans-serif;
    padding:5px;
    margin:0px;
    background-color:#e0e0e0;
    text-algin:center;
}
#lInfo{
    width:11em;
    padding:10px 10px;
    border:0px;
    margin:0px 0px 0px 0px;
    position:relative;
    float:left;
}
#rInfo{
    width:10em;
    padding:10px 10px;
    margin:0px;
    position:relative;
    float:left;
}
textarea{
    height:100px;
    width:300px;
}
input[type=text]{
    width:300px;
}
</style>
<body>
<div id="top_header"><div align=left>当前位置:添加学生信息</div></div>
<div><div id="lInfo"><div align=right><p>登陆账号:</p><p>登陆密码:</p><p>性别:</p><p>姓名:</p><p>学号:</p><p>电子邮件:</p>
<p>所在学院:</p><p>所在系:</p><p>所在班级:</p><p>入学年份(届):</p><p>生源地:</p><p>备注:</p></div></div><div id="rInfo"><div align=left>
<form action=Register method=post>
<p><input name="stuUserName" id="SUNI" type=text placeholder="请输入登陆账号"></p>
<p><input name="stuPwd" id="SPWI" type=password style="width:300px;" placeholder="请输入登陆密码"></p>
<p><input name="stuSex" id="SSEI" type=radio value="男">&nbsp;&nbsp;&nbsp;&nbsp;<input name="stuSex" type=radio value="女"></p>
<p><input name="stuName" id="SNAI" type=text placeholder="请输入姓名"></p>
<p><input name="stuNum" id="SNUI" type=text placeholder="请输入学号"></p>
<p><input name="stuMail" id="SMAI" type=text placeholder="请输入电子邮件"></p>
<p><input name="stuInst" id="SINI" type=text placeholder="请输入所在学院"></p>
<p><input name="stuSub" id="SSUI" type=text placeholder="请输入所在系"></p>
<p><input name="stuClass" id="SCLI" type=text placeholder="请输入所在班级"></p>
<p><select name="stuYear">
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<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></p>
<p><input name="stuLocal" id="SLOI" type=text placeholder="请输入生源地"></p>
<p><textarea name="stuMemo" placeholder="无" ></textarea></p>
<p><input type=submit value="提交" onclick="return InfoChk();"></p>
<script type = "text/javascript">
function InfoChk(){
    var SMAChk = /^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$/;
    var SUNChk = /^[A-Za-z][A-Za-z0-9]{5,11}$/;
    var SPWChk = /^[A-Za-z0-9]{8,}$/;
    var SNUChk = /^2018[0-9]{4}$/;
    var SUN = document.getElementById("SUNI").value;
    var SPW = document.getElementById("SPWI").value;
    var SSE = document.getElementById("SSEI").value;
    var SNA = document.getElementById("SNAI").value;
    var SNU = document.getElementById("SNUI").value;
    var SMA = document.getElementById("SMAI").value;
    var SIN = document.getElementById("SINI").value;
    var SSU = document.getElementById("SSUI").value;
    var SCL = document.getElementById("SCLI").value;
    var SLO = document.getElementById("SLOI").value;
    if (SUN==""){
        alert("您的登陆账号未填写!");
        return false;
    }else if(SPW==""){
        alert("您的登陆密码未填写!");
        return false;
    }else if(SSE==""){
        alert("您的性别未选择!");
        return false;
    }else if(SNA==""){
        alert("您的姓名未填写!");
        return false;
    }else if(SNU==""){
        alert("您的学号未填写!");
        return false;
    }else if(SMA==""){
        alert("您的电子邮件未填写!");
        return false;
    }else if(SIN==""){
        alert("您的所在学院未填写!");
        return false;
    }else if(SSU==""){
        alert("您的所在系未填写!");
        return false;
    }else if(SCL==""){
        alert("您的所在班级未填写!");
        return false;
    }else if(SLO==""){
        alert("您的生源地未填写!");
        return false;
    }else{
        if(!SUNChk.exec(SUN)){
            alert("您的登陆账号格式有误!");
            return false;
        }else if(!SPWChk.exec(SPW)){
            alert("您的登陆密码格式有误!");
            return false;
        }else if(!SMAChk.exec(SMA)){
            alert("您的电子邮件格式有误!");
            return false;
        }else if(!SNUChk.exec(SNU)){
            alert("您的学号格式有误!");
            return false;
        }
    }
}
</script>
</form>
</div>
</div>
</div>
</body>
</html>
StudentRegisterPage.jsp

servlet:

package servlet;

import java.io.IOException;

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 dao.ConfirmSUN;
import dao.RegisterAdd;
import dataCla.DatChk;
import dataCla.StuInfo;
import dataCla.calMD5;

/**
 * Servlet implementation class Register
 */
@WebServlet("/Register")
public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request00, HttpServletResponse response00)
            throws ServletException, IOException {
        boolean flag = false;
        request00.setCharacterEncoding("utf-8");
        response00.setContentType("text/html;charset=UTF-8");

        if (request00.getParameter("stuUserName").equals("") || request00.getParameter("stuPwd").equals("")
                || request00.getParameter("stuSex").equals("") || request00.getParameter("stuName").equals("")
                || request00.getParameter("stuNum").equals("") || request00.getParameter("stuMail").equals("")
                || request00.getParameter("stuInst").equals("") || request00.getParameter("stuSub").equals("")
                || request00.getParameter("stuClass").equals("") || request00.getParameter("stuYear").equals("")
                || request00.getParameter("stuLocal").equals("")) {
            response00.getWriter().print("<script language='javascript'>alert('添加失败!有项目未填写')</script>");
        } else {
            if (!DatChk.SUNChk(request00.getParameter("stuUserName"))
                    || !DatChk.SPWChk(request00.getParameter("stuPwd"))
                    || !DatChk.SNUChk(request00.getParameter("stuNum"))
                    || (request00.getParameter("stuUserName").length() < 6)
                    || (request00.getParameter("stuUserName").length() > 12)
                    || (request00.getParameter("stuPwd").length() < 8)
                    || (request00.getParameter("stuNum").length() != 8)) {
                response00.getWriter().print("<script language='javascript'>alert('添加失败!信息不符合要求')</script>");
            } else {
                if (ConfirmSUN.ConfSUN(request00.getParameter("stuUserName"))) {
                    response00.getWriter().print("<script language='javascript'>alert('添加失败!登陆账号被使用')</script>");
                } else {
                    StuInfo log;
                    try {
                        log = new StuInfo(request00.getParameter("stuUserName"),
                                calMD5.getMD5Str(request00.getParameter("stuPwd")), request00.getParameter("stuSex"),
                                request00.getParameter("stuName"), request00.getParameter("stuNum"),
                                request00.getParameter("stuMail"), request00.getParameter("stuInst"),
                                request00.getParameter("stuSub"), request00.getParameter("stuClass"),
                                request00.getParameter("stuYear"), request00.getParameter("stuLocal"),
                                request00.getParameter("stuMemo"));
                        flag = RegisterAdd.add(log);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (flag == false) {
                        response00.getWriter()
                                .print("<script language='javascript'>alert('添加失败!数据库连接错误或您的信息过长')</script>");
                    } else {
                        response00.getWriter().print("<script language='javascript'>alert('添加完成!')</script>");
                    }
                }
            }
        }
        response00.setHeader("refresh", "1;URL=StudentRegisterPage.jsp");
    }
}
Register.java

dao:

package dao;

import java.sql.Connection;
import java.sql.Statement;

import DBUL.DBUtil;
import dataCla.StuInfo;

public class RegisterAdd {
    public static boolean add(StuInfo ipt) {
        String sql = "insert into stu_base_log_00(stuUserName,stuPWD,stuSex,stuName,stuNum,stuMail,stuInst,stuSub,stuClass,stuYear,stuLocal,stuMemo) values('"
                + ipt.getSUN() + "','" + ipt.getSPW() + "','" + ipt.getSSE() + "','" + ipt.getSNA() + "','"
                + ipt.getSNU() + "','" + ipt.getSMA() + "','" + ipt.getSIN() + "','" + ipt.getSSU() + "','"
                + ipt.getSCL() + "','" + ipt.getSYE() + "','" + ipt.getSLO() + "','" + ipt.getSME() + "')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
}
RegisterAdd.java
package dao;
//查询,用来检查用户名是否重复

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import DBUL.DBUtil;

public class ConfirmSUN {
    public static boolean ConfSUN(String iptSUN) {
        boolean flag = false;
        String sql = "select userUName from user_base_log where userUName ='" + iptSUN + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            if (rs.next()) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return flag;
    }
}
ConfirmSUN.java

dataCla(保存数据的类,以及网上提供的MD5算法和后台的第二次校验输入是否有效的方法类):

package dataCla;

public class StuInfo {
    private String stuUserName = new String("");
    private String stuPwd = new String("");
    private String stuSex = new String("");
    private String stuName = new String("");
    private String stuNum = new String("");
    private String stuMail = new String("");
    private String stuInst = new String("");
    private String stuSub = new String("");
    private String stuClass = new String("");
    private String stuYear = new String("");
    private String stuLocal = new String("");
    private String stuMemo = new String("");

    public StuInfo(String SUN, String SPW, String SSE, String SNA, String SNU, String SMA, String SIN, String SSU,
            String SCL, String SYE, String SLO, String SME) {
        stuUserName = SUN;
        stuPwd = SPW;
        stuSex = SSE;
        stuName = SNA;
        stuNum = SNU;
        stuMail = SMA;
        stuInst = SIN;
        stuSub = SSU;
        stuClass = SCL;
        stuYear = SYE;
        stuLocal = SLO;
        stuMemo = SME;
    }

    public String getSUN() {
        return stuUserName;
    }

    public String getSPW() {
        return stuPwd;
    }

    public String getSSE() {
        return stuSex;
    }

    public String getSNA() {
        return stuName;
    }

    public String getSNU() {
        return stuNum;
    }

    public String getSMA() {
        return stuMail;
    }

    public String getSIN() {
        return stuInst;
    }

    public String getSSU() {
        return stuSub;
    }

    public String getSCL() {
        return stuClass;
    }

    public String getSYE() {
        return stuYear;
    }

    public String getSLO() {
        return stuLocal;
    }

    public String getSME() {
        return stuMemo;
    }
}
StuInfo.java
package dataCla;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DatChk {

    public static boolean SUNChk(String ipt) {
        boolean tag = true;
        final String pattern1 = "^[A-Za-z]\\w+$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(ipt);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

    public static boolean emailFormat(String email) {
        boolean tag = true;
        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

    public static boolean SPWChk(String ipt) {
        boolean tag = true;
        final String pattern1 = "^[A-Za-z0-9]+$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(ipt);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

    public static boolean SNUChk(String ipt) {
        boolean tag = true;
        final String pattern1 = "^2018[0-9]+$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(ipt);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }
}
DatChk.java
posted @ 2019-10-21 20:11  水無月鈴乃  阅读(384)  评论(0编辑  收藏  举报