Servlet实现请假-ajax方式 作者:马育民 • 2022-02-14 16:20 • 阅读:10063 # 说明 创建封装类 `JsonResult`,封装返回结果,并将 java对象 转成 `json` 字符串 # html ``` <script src="js/jquery-1.11.0.min.js" ></script> <script> function doSubmit(){ var type=$("#type").val() var start_date=$("#start_date").val() var end_date=$("#end_date").val() var reason=$("#reason").val() console.log("type:",type) if( type === '-1'){ $("#msg").html(" <div style='color:red;'> 请选择假期类型! </div>") return } if( start_date === ''){ $("#msg").html(" <div style='color:red;'> 请填写开始日期! </div>") return } if( end_date === ''){ $("#msg").html(" <div style='color:red;'> 请填写结束日期! </div>") return } if( reason === ''){ $("#msg").html(" <div style='color:red;'> 请填写请假事由! </div>") return } $("#msg").html("") $.ajax({ url:'leaveServlet2', type:'post', //请求方式,GET或POST data:{ //要提交给服务器的数据 type : type, start_date : start_date, end_date : end_date, reason : reason, }, dataType:'json',//服务器返回数据的类型 success:function(data){//成功时执行该函数,data就是服务器返回的数据 console.log("服务器返回的数据是:",data) //判断,登录成功后,跳转到首页 index.html if(data.code==0){ //跳转到 index.html 页面 location.href="index.html"; }else{ //登录失败 $("#msg").html(data.msg)//向id是msg的标签,显示文字 $("#msg").html(" <div style='color:red;'> "+data.msg+" </div>") } }, error:function(xhr,textStatus){//错误时执行该函数 console.log('错误') console.log(xhr) console.log(textStatus) } }) } </script> </head> <body> <h1>请假</h1> <div> 假期类型: <select name="type" id="type"> <option value='-1'>请选择</option> <option value='1'>事假</option> <option value='2'>病假</option> <option value='3'>产假</option> <option value='4'>婚假</option> </select> </div> <div> 开始日期:<input type="date" name="start_date" id="start_date"> </div> <div> 结束日期:<input type="date" name="end_date" id="end_date"> </div> <div> 请假事由:<input type="text" name="reason" id="reason"> </div> <input type="button" onclick="doSubmit()" value="提交"> <div id="msg"></div> ``` # servlet ``` package com.zrgj.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; 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.zrgj.util.DateUtil; import com.zrgj.util.DbUtils; import com.zrgj.vo.JsonResult; @WebServlet(urlPatterns="/leaveServlet2") public class LeaveServlet2 extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html; charset=UTF-8"); String type=req.getParameter("type"); //start_date命名方式不符合java的规范,应用小驼峰命名方式 String startDate=req.getParameter("start_date"); String endDate=req.getParameter("end_date");//ctrl+alt+↓ 复制一行 String reason=req.getParameter("reason");//ctrl+alt+↓ 复制一行 PrintWriter out = resp.getWriter(); //校验结束日期 大于等于 开始日期 //比较日期时 ,就不能用String,用Date类型 try{ boolean b=DateUtil.beforeAndEquals(startDate, endDate); if(!b){ // ! 表示取反 out.write( JsonResult.fail(100, "开始日期要早于结束日期,请重新填写!") ); return; } }catch(Exception e){ e.printStackTrace(); } DbUtils dbUtils=new DbUtils(); try { dbUtils.connect("com.mysql.jdbc.Driver" , "jdbc:mysql://127.0.0.1:3308/scott?useSSL=false&useUnicode=true&characterEncoding=utf-8" ,"root" ,""); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } String sql="INSERT INTO t_leave (l_type,start_date,end_date,reason,user_name) " + "VALUES (?,?,?,?,?)"; String[] paras={type,startDate,endDate,reason,null}; try { int i = dbUtils.update(sql, paras); System.out.println("影响的行数:"+i); //在公司开发中,不要打印 if(i>0){ //TODO:用转发实现 out.write( JsonResult.success("提交成功!") ); }else{ out.write( JsonResult.fail(100, "提交失败,请联系管理员!") ); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { dbUtils.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` # JsonResult 详见: https://www.malaoshi.top/show_1IX2rn0kwlcB.html 原文出处:/show_1IX2mFBwC1Ak.html