欢迎光临散文网 会员登陆 & 注册

会议室预定系统过滤器项目Java web服务器运行错误,JS正则表达式判断非空【诗书画唱】

2020-10-18 16:29 作者:诗书画唱  | 我要投稿

个人的注释:







create table huiyiInfo(

id int primary key auto_increment,



huiYiShiName varchar(100) ,


yuShiRenName varchar(100) ,

riQi  date);



insert into huiyiInfo(

huiYiShiName ,

yuShiRenName ,


riQi

) values ("第一会议室",'诗书画唱1','2020-06-26'),

("第二会议室",'诗书画唱2','2020-05-20'),("第三会议室",'诗书画唱3','2020-09-09');


--drop table huiyiInfo

--select * from huiyiInfo


加个过滤器后:

1.png


package com.SSHC.Filter;


import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;




@WebFilter("/*")

public class CodeFilter implements Filter {


   

    public CodeFilter() {

        // TODO Auto-generated constructor stub

    }


public void destroy() {

// TODO Auto-generated method stub

}


public void doFilter(ServletRequest request, 

ServletResponse response, FilterChain chain)

throws IOException, ServletException {

// TODO Auto-generated method stub

// place your code here

request.setCharacterEncoding("utf-8");

chain.doFilter(request, response);

}


public void init(FilterConfig fConfig) throws ServletException {

// TODO Auto-generated method stub

}


}



package com.SSHC.bean;


import java.util.Date;


public class HuiyiInfo {

    private Integer id;

    private String huiYiShiName;

    private String yuShiRenName;

//    

//    个人的注释:因为数据库有时会默认把日期字符串处理为Date类型

//    ,所以这里可以声明为String类型

    private String riQi;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getHuiYiShiName() {

return huiYiShiName;

}

public void setHuiYiShiName(String huiYiShiName) {

this.huiYiShiName = huiYiShiName;

}

public String getYuShiRenName() {

return yuShiRenName;

}

public void setYuShiRenName(String yuShiRenName) {

this.yuShiRenName = yuShiRenName;

}

public String getRiQi() {

return riQi;

}

public void setRiQi(String riQi) {

this.riQi = riQi;

}


}



package com.SSHC.controller;


import java.io.IOException;

import java.util.Date;


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.SSHC.DAO.Dao;

import com.SSHC.bean.HuiyiInfo;


/**

 * Servlet implementation class addOKServlet

 */

@WebServlet("/AddServlet")

public class AddServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


    /**

     * @see HttpServlet#HttpServlet()

     */

    public AddServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest 

* request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, 

HttpServletResponse response) 

throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest 

* request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest

request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub


//乱码处理:

//     request.setCharacterEncoding("utf-8");

    //1、获取表单中输入的数据:

    String  HuiYiShiName = request.getParameter("selectName");

    String YuShiRenName= request.getParameter("YuShiRenName");


String RiQi= request.getParameter("RiQi");

 

System.out.println(HuiYiShiName+"   "+YuShiRenName+"  "+RiQi);

//   // Integer gyear2=Integer.valueOf(gyear);

// Integer gyearInteger=0;


// if(gyear != null && gyear.length() > 0) {

//

// RiQi=Date.valueOf(gyear);

//

// }


    HuiyiInfo u = new HuiyiInfo();

    u.setHuiYiShiName(HuiYiShiName);

    u.setYuShiRenName(YuShiRenName);

 

    u.setRiQi(RiQi);

   

   

      

Dao ud = new Dao();


  ud.add(u);

   

//Integer count = ud.add(u);


String S=null;

    if(ud.count>0) {

  S="会议室预定成功!";

    }else 

    {

    S="会议室预定失败!";

    }  



  request.setAttribute("S", S);

 

  request.getRequestDispatcher("addOK.jsp")

      .forward(request, response);

}


}



package com.SSHC.controller;


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.SSHC.DAO.GameDao;

import com.SSHC.bean.HuiyiInfo;


@WebServlet("/FirstPageServletStart")

public class FirstPageServletStart extends HttpServlet {

private static final long serialVersionUID = 1L;

  

    public FirstPageServletStart() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request,

*  HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, 

HttpServletResponse response) 

throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}



protected void doPost(HttpServletRequest request,

HttpServletResponse response) 

throws ServletException, IOException {

// TODO Auto-generated method stub

//乱码处理:

request.setCharacterEncoding("utf-8");


GameDao gd = new GameDao();

List<HuiyiInfo>list = gd.selectAll();


StringBuilder html = new StringBuilder();

for(HuiyiInfo g : list) {


Integer gI= g.getId();

String gH = g.getHuiYiShiName();

String gY = g.getYuShiRenName();

String gR = g.getRiQi();

System.out.println(gH+"   "+gY+"   "+gR);


html.append("<tr>");

  html.append("<td class='inp'>" + gI + "</td>");

        html.append("<td class='inp'>" + gH + "</td>");

        html.append("<td class='inp'>" + gY + "</td>");

        html.append("<td class='inp'>" + gR + "</td>");


        html.append("</tr>");

}


request.setAttribute("html", html);

request.getRequestDispatcher("firstPage.jsp")

    .forward(request, response);

}


}


package com.SSHC.DAO;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import com.SSHC.bean.HuiyiInfo;

import com.SSHC.utils.DBUtils;


//类名就是对应的bean的名字后面加上Dao

//Dao就是数据访问类的意思,就是对Game表进行增删改查的代码都写在这个类中

public class Dao {


public List<HuiyiInfo>selectAll(){


String sql = "select * from huiyiInfo";


Connection conn = null;


PreparedStatement pstm = null;


ResultSet rs = null;


List<HuiyiInfo>list = new ArrayList<HuiyiInfo>();


try {



conn = DBUtils.getConn();


pstm = conn.prepareStatement(sql);


rs = pstm.executeQuery();


while(rs.next()) {


Integer id = rs.getInt("id");


String huiYiShiName = rs.getString("huiYiShiName");

String yuShiRenName = rs.getString("yuShiRenName");

String riQi = rs.getString("riQi");

System.out.println(huiYiShiName);


//进行打包:


HuiyiInfo g = new HuiyiInfo();


g.setId(id);

g.setHuiYiShiName(huiYiShiName);

g.setYuShiRenName(yuShiRenName);

g.setRiQi(riQi);


//继续打包:

list.add(g);


}


} catch (SQLException e) {


// TODO Auto-generated catch block


e.printStackTrace();


}


return list;


}


public  int count=0;

 public Integer add(HuiyiInfo  u) {

  

    String sql = "insert into huiyiInfo("

+ "huiYiShiName,yuShiRenName,riQi) values(?,?,?);";

    Connection conn = null;

    PreparedStatement pstm = null;

    ResultSet rs = null;


    try {

        conn = DBUtils.getConn();

pstm = conn.prepareStatement(sql);

//设置占位符


pstm.setObject(1,u.getHuiYiShiName());

pstm.setObject(2,u.getYuShiRenName() );

pstm.setObject(3,u.getRiQi());


// pstm.setObject(1,"三连会议室");

// pstm.setObject(2,"诗书画唱");

// pstm.setObject(3,"2020-7-8");

//

count=pstm.executeUpdate();

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

 

           

DBUtils.close(rs, pstm, conn);

}

    return 0;

    }

}



package com.SSHC.utils;


import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Properties;


public class DBUtils {

    private static String driverName;

    private static String url;

    private static String userName;

    private static String pwd;

    //静态块,随着类加载而运行的

    static{

    //读取db.properties文件中的内容

    Properties prop = new Properties();

    InputStream is = DBUtils.class.getClassLoader()

    .getResourceAsStream("db.properties");

    try {

prop.load(is);

driverName = prop.getProperty("dn");

url = prop.getProperty("url");

userName = prop.getProperty("un");

pwd = prop.getProperty("up");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }

    

    public static Connection getConn(){

    Connection conn = null;

    try {

Class.forName(driverName);

conn = DriverManager.getConnection(url,userName,pwd);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    return conn;

    }

    

    public static void close(ResultSet rs,PreparedStatement pstm

    ,Connection conn){

    try {

    if(rs != null) {

    rs.close();

    }

    if(pstm != null) {

    pstm.close();

    }

    if(conn != null) {

    conn.close();

    }

    } catch(Exception e) {

    e.printStackTrace();

    }

    }

}

dn=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/firstjsp?useUnicode=true&characterEncoding=UTF-8

un=root

up=root

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <base hreff="<%=basePath%>">

        <title></title>

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

        <meta http-equiv="description" content="This is my page">

        <style type="text/css">

            * {

                font-size: 23px;

            }

          

        .inp {

                box-shadow: 5px 5px 5px #888888;

                border: 0px;

                border-radius: 5px;

            }

            body{


background-image: url('img/1.png');


background-repeat: no-repeat;


background-size: 100%;


}

        </style>

        <script type="text/javascript">

            function cancel(){

            window.location.href = 'add.jsp';

            }

          

         

            //表单验证的方法

            function doCheck(){

<%--    下面是JS获取选中的下拉框的值的方法,我记得我以前尝试过

下面两句不可以合起来写要分开写,

 不然会没效果,报错等:

            var myselect=document.getElementById("selectId");

           


            var myselectValue=myselect.options[index].value;

            --%>  

var yuShiRenName= document.getElementsByName('YuShiRenName')[0].value;

 var riQi= document.getElementsByName('RiQi')[0].value;

   

   <%-- 下面是我原创的自己想出来的用JS的正则表达式和test【

   test() 方法用于检测一个字符串是否匹配某个模式.

如果字符串中有匹配的值返回 true ,否则返回 false。

个人理解:用于判断两个字符串等是否相等。】

   来判断是否为非空的方法

   --%>  

   <%--  .可以替换任意的除了\n(换行符)以外的任意字符。

"{n,}"——匹配前一个字符n次或更多次。

--%>    var reg = /.{1,}/;

                   <%-- 其实可以用正则表达式来判断输入格式的对出的

                但是有些月的天数等随平年和闰年等而改变等,所以

                还是直接用网上找的js日期框方便 

 var reg2 = /^\d{4}-[1-12]{1-2}-[1-31]{1-2}/;

 

 因为我设置了默认的选中的会议室,所以不必判断会议室的部分是否为空 --%>

   //如果year是一个四位数字,那么r就为true,否则就为false

                var r = reg.test(riQi);

                var y = reg.test(yuShiRenName);

                if(!r) {

                alert('预定日期和预定者不能为空!');

            return r;

                }

               

                if(!y) {

                        alert('预定日期和预定者不能为空!');

                    return y;

                        }

               

            }

           

          

    

        </script>

    </head>

    <body>

        <table style="width:100%;" >

            <tr>

 <td style="background-color:gray;text-align:center;">预定会议室</td>

            </tr>

            <tr>

                <td align="center">

                   <%--<form action="addOK.jsp" method="post"> --%> 

 <form action="AddServlet" method="post" onsubmit="return doCheck();">

<table>

    <tr>

        <td>会议室名称:</td>


<td>

<%-- <input class="inp" type="text" name="HuiYiShiName" />--%>


 <select id="selectId" name="selectName">



<option value='第一会议室' selected>第一会议室</option>

<option value='第二会议室' >第二会议室</option>

<option value='第三会议室' >第三会议室</option>

<%--${xiaLaKuangHtml }; --%>


</select>


      <span style="color:red;">*</span>

        </td>

    </tr>

    <tr>

        <td>预定日期:</td>

<td><input class="inp" type="text" name="RiQi" />

<span style="color:red;"> 如:yyyy-mm-dd</span></td>

    </tr>

    <tr>

        <td>预定者:</td>

 <td><input class="inp" type="text" name="YuShiRenName" /></td>

    </tr>

   

    <tr>

        <td colspan="2" align="center">

  <input type="submit" value="预定" onclick='doCheck();' />

 <input type="button" value="重置" onclick="cancel();" />

        </td>

    </tr>

</table>

</form>

                </td>

            </tr>

        </table>

    </body>

</html>

<%@ page language="java" contentType=

"text/html; charset=utf-8"

    pageEncoding="utf-8"%>


      <%@page import="com.SSHC.DAO.GameDao"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01

 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<script type="text/javascript">

alert('${S }');

</script>

<body>


</body>

</html>

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <base hreff="<%=basePath%>">

        <title></title>

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

        <meta http-equiv="description" content="This is my page">

        <style type="text/css">

            * {

                font-size: 23px;

            }

            td{text-align: center;vertical-align: middle;}

           

 .inp {

                box-shadow: 5px 5px 5px #888888;

                border: 0px;

                border-radius: 5px;

            }

body{


background-image: url('img/1.png');


background-repeat: no-repeat;


background-size: 100%;


}


        </style>

        <script type="text/javascript">

            function bk(){

            window.location.hreff = 'add.jsp';

            }

        </script>

    </head>

    <body>

        <table cellspacing="0px" border="0px"  style="width:100%;" >

           

            <tr>

                <td style="background-color:gray;

                text-align:center;" class='inp'>会议室信息</td>

            </tr>

            <tr>

                <td>

 <table cellspacing="0px" border="0px" style="width:100%;">

                        <tr >

                            <th class='inp'>编号</th>

                            <th class='inp'>会议室名称</th>

                            <th class='inp'>预定人</th>

                            <th class='inp'>日期</th>

                        </tr>

                        ${html }

                    </table>

                    

                </td>

            </tr>

             <tr>

                <td class='inp'><a hreff="javascript:bk()" 

                style="float:right;">预定</a></td>

            </tr>

        </table>

    </body>

</html>

1
2
3
4




会议室预定系统过滤器项目Java web服务器运行错误,JS正则表达式判断非空【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律