public String realPath(String path){ SimpleDateFormat y=new SimpleDateFormat("yyyy"); SimpleDateFormat m=new SimpleDateFormat("MM"); SimpleDateFormat d=new SimpleDateFormat("dd"); Date date=new Date(System.currentTimeMillis()); String year = y.format(date); String month = m.format(date); String day = d.format(date); String realPath=path+ File.separator+year+File.separator+month+File.separator+day; File file=new File(realPath); if(!file.exists()) file.mkdirs(); return realPath; } public String newFilename(String username,String filename){ Date date=new Date(); SimpleDateFormat time=new SimpleDateFormat("yyyyMMddHHmmss"); String times = time.format(date); return times+"_"+username+"_"+filename; } // 单文件上传 @RequestMapping("/upload") public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Part part = request.getPart("file"); String realPath = request.getServletContext().getRealPath("/WEB-INF/upload"); String s = realPath(realPath); // 判断上传目录是否存在,如果不存在则创建反之直接存 File file1=new File(s); if(!file1.exists())file1.mkdirs(); part.write(s+File.separator+newFilename(null,part.getSubmittedFileName())); String newFileName=s+File.separator+newFilename(null,part.getSubmittedFileName()); response.getWriter().print(part.getSubmittedFileName()+"上传成功"); request.setAttribute("filename",newFileName); return "/admin/lunboForm"; }
@RequestMapping("/login")//登录 public String login(String username, String password, HttpServletRequest request, HttpServletResponse response, HttpSession session){ FarmUserMapper farmUserMapper = ss.getMapper(FarmUserMapper.class); FarmUser user = farmUserMapper.queryUser(username, password); if (user!=null) { Cookie cookie=new Cookie("userinfo",username+"#"+password); cookie.setMaxAge(60*60*24*7); cookie.setPath("/"); response.addCookie(cookie); session.setAttribute("user",user); if(user.getType()==1)return "redirect:/admin/"; else if(user.getType()==0) return "redirect:/user/"; else { request.setAttribute("accountErr","accountErr"); return "public/error"; } }else{ request.setAttribute("loginErr","loginErr"); return "public/error"; } }
// 注销登录 @RequestMapping("/logout") public String logout(HttpSession session, HttpServletRequest request){ FarmUser user = (FarmUser) session.getAttribute("user"); if (user!=null) { session.removeAttribute("user"); session.invalidate(); }else{ Cookie[] cookies = request.getCookies(); if (cookies.length!=0) { for (Cookie cookie : cookies) { String name = cookie.getName(); if (name.equals("userinfo")) { cookie.setMaxAge(0); } } } } return "redirect:/"; }
2020javaweb教程之mysql习题集合
回顾知识
简单查询:select*from 表名;
查询指定列:select sname,score from 表名;
条件查询:
比较运算符:
查询性别为女,并且年龄小于50的记录:
select*from 表名 where sex=’女’ and age<50;
查询学号为S_1001,或者姓名为liSi的记录
select*from 表名 wher sid=’S_1001′ or sname=’lisi’;
查询学号为S _ 1001 ,S _ 1002,S _ 1003的记录 IN(set)
select*from 表名 where sid=’S_1001′ or sid=’S_1002′ or sid=’S_1003′;
或者:
select*from 表名 where sid in(‘S_1001′,’S_1002′,’S_1003’);
查询学号不是S_1001,S_1002,S_1003的记录
select*from 表名 where sid not in(‘S_1001′,’S_1002′,’S_1003’);
(更多…)
2020javaweb教程之利用jQueryAjax查询数据和删除数据
查询所有:
html:
<a href="add.jsp">添加用户</a> <div id="div1"> <table> <tbody id="a"> <tr> <th>编号</th> <th>用户名</th> <th>密码</th> <th>权限</th> <th>操作</th> </tr> </tbody> <tbody id="b"></tbody> </table> </div> <button onclick="gets()">获取最新</button>
script:
<script> var count=0; function gets(){ var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ var objs=JSON.parse(xhr.responseText); if(xhr.responseText=="bad"){ alert("查询失败"); }else{ var b=document.getElementById("b"); var tr=document.createElement("tr"); for(var i=0;i<objs.length;i++){ count++; if(count<=objs.length){ tr=document.createElement("tr"); var tdid=document.createElement("td"); var tdname=document.createElement("td"); var tdpwd=document.createElement("td"); var tdacc=document.createElement("td"); var tddel=document.createElement("td"); tdid.innerText=objs[i].id; tr.append(tdid); tdname.innerText=objs[i].username; tr.append(tdname); tdpwd.innerText=objs[i].password; tr.append(tdpwd); tdacc.innerText=objs[i].access; tr.append(tdacc); var s="id="+objs[i].id+"&username="+objs[i].username+"&password="+objs[i].password; tddel.innerHTML="<a href='update.html?"+s+"'>修改</a>" + "<a href='javascript:void(0)' onclick='dels(this,"+objs[i].id+")'>删除</a>"; tr.append(tddel); b.append(tr); }else{ tr.remove(); } } } } } xhr.open("get","/test/getAll"); xhr.send(); } /*function del(tr,id){//jQueryAjax方法 var result=confirm("确定要删除吗?操作不可逆"); $.post("/test/del","id="+id,function (data) { if(result==true){ if(data=="1"){ alert("删除成功"); tr.parent().parent().remove(); location.href="getAll.html"; }else alert("删除失败"); } }) }*/ function dels(tr,id){ var xhr=new XMLHttpRequest(); var result=confirm("确定要删除吗?操作不可逆"); xhr.onreadystatechange=function(data){ if(xhr.readyState==4&&xhr.status==200){ if(result==true){ if(xhr.responseText=="1"){ alert("删除成功"); var fa=tr.parentNode.parentNode; fa.remove(); }else alert("删除失败"); } } } xhr.open("post","/test/del"); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("id="+id); } </script>
查询所有的servlet
PrintWriter writer = response.getWriter(); UsersService us=new UsersServiceImpl(); List<Users> all = us.getAll(); Object o = JSON.toJSON(all); if(o!=null) writer.print(o); else writer.print("bad");
删除的servlet
String id = request.getParameter("id"); PrintWriter writer = response.getWriter(); UsersService us=new UsersServiceImpl(); int delete = us.delete(Integer.parseInt(id)); if(delete>0) writer.print("1"); else writer.print("0");
修改的html
<form method="post"> id : <input type="text" readonly value="" name="id" id="id"><br> username : <input type="text" name="username" value="" id="username"><br> password : <input type="text" name="password" value="" id="password"> </form> <button onclick="changes()">修改</button>
修改的script
<script> $(function () { var search = location.search.substring(1); var str=search.split("&"); $.each(str,function(i){ var v1=str[i].split("="); $("#"+v1[0]).val(v1[1]); }) }) function changes() { var id=$("#id").val(); var username=$("#username").val(); var password=$("#password").val(); var userinfo={"id":id,"username":username,"password":password}; var s=JSON.stringify(userinfo); $.post("/test/update","userinfo="+s,function (data) { if(data=="1"){ alert("修改成功"); location.href="getAll.html"; }else alert("修改失败"); }) } </script> </body>
修改的servlet
String userinfo = request.getParameter("userinfo"); UsersService us=new UsersServiceImpl(); int update = us.update(userinfo); PrintWriter writer = response.getWriter(); if(update>0) writer.print("1"); else writer.print("0");
添加数据
<body> <% UsersService us=new UsersServiceImpl(); int id = us.getAllID(); request.setAttribute("id",id); %> <form method="post"> id : <input type="text" readonly value="${id+1}" name="id" id="id"><br> username : <input type="text" name="username" id="username"><br> password : <input type="text" name="password" id="password"> </form> <button onclick="adds()">添加用户</button> <script> function adds() { var id=$("#id").val(); var username=$("#username").val(); var password=$("#password").val(); var str={"id":id,"username":username,"password":password}; var s=JSON.stringify(str); $.post("/test/add","userinfo="+s,function (data) { if(data=="1"){ alert("添加成功"); location.href="getAll.html"; }else alert("添加失败"); }) } </script> </body>
添加的servlet
String userinfo = request.getParameter("userinfo"); UsersService us=new UsersServiceImpl(); int add = us.add(userinfo); PrintWriter writer = response.getWriter(); if(add>0) writer.print("1"); else writer.print("0");
2020javaweb教程之利用jQueryAjax进行查询与删除
<div id="div1"> <table> <tbody id="a"> <tr> <th>编号</th> <th>用户名</th> <th>密码</th> <th>权限</th> <th>操作</th> </tr> </tbody> <tbody id="b"></tbody> </table> </div> <button onclick="gets()">获取</button>
<script> //原生Ajax实现json查询 function gets(){ var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ if(xhr.responseText=="error"){ alert("查询失败") }else{ //清空内容 $("#b").empty(); var objs=JSON.parse(xhr.responseText); $.each(objs,function(i){ //创建标签 var tr=document.createElement("tr"); var tdid=document.createElement("td"); var tdname=document.createElement("td"); var tdpw=document.createElement("td"); var tdac=document.createElement("td"); tdid.innerText=objs[i].id; tr.append(tdid);//tr追加td tdname.innerText=objs[i].username; tr.append(tdname); tdpw.innerText=objs[i].password; tr.append(tdpw); tdac.innerText=objs[i].access; tr.append(tdac); //创建删除的 td标签 var tddel=document.createElement("td"); //添加内容 tddel.innerHTML="<a href='javascript:void(0);'" + " onclick='del(this,"+objs[i].id+")'>删除</a>"; //追加 tr.append(tddel); $("#b").append(tr);//tbody追加tr }) } } } xhr.open("get","/day23/getAll"); xhr.send(); } function del(tr,id){//原生Ajax的 post 方法 var xhr=new XMLHttpRequest(); var result=confirm("确定删除吗?操作不可逆"); xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ if(result==true){ var v=xhr.responseText; if(v=="ok"){ alert("删除成功"); var fa=$(tr).parent().parent(); fa.remove(); }else alert("删除失败") } } } xhr.open("post","/day23/delete"); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("id="+id); } </script>
2020javaweb教程之ajax方法
常用参数:
contentType:(默认: “application/x-www-form-urlencoded”) 发送信息至服务器时内容编码类型。默认值适合大多数情况。
data:发送到服务器的数据
dataType:预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断。
“xml”: 返回 XML 文档,可用 jQuery 处理。
“html”: 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。
“script”: 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了”cache”参数。”’注意:”’在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载)
“json”: 返回 JSON 数据 。 (更多…)
2020javaweb教程之jQuery全选、反选、全不选
<table> <tr> <td><input type="checkbox" id="allbox">全选</td> <td><input type="checkbox" id="allnobox">全不选</td> <td><input type="checkbox" id="fanbox">反选</td> </tr> <tr> <td><input type="checkbox" class="box"></td> <td></td> </tr> <tr> <td><input type="checkbox" class="box"></td> <td></td> </tr> <tr> <td><input type="checkbox" class="box"></td> <td></td> </tr> </table> <script> $(function(){ var allbox=$("#allbox");//获取全选 var boxs=$(".box");//获取所有复选框 //反选 var fx=$("#fanbox"); fx.click(function(){ for(var i=0;i<boxs.length;i++){ var r=$(boxs[i]).prop("checked"); if(r==true){ $(boxs[i]).prop("checked",false) }else $(boxs[i]).prop("checked",true) } }) //全选 allbox.click(function(){ var result=$(this).prop("checked"); if(result==true){ boxs.prop("checked",true); }else boxs.prop("checked",false); }) //全不选 var allnobox=$("#allnobox"); allnobox.click(function(){ var lists=$("input[type='checkbox']"); for(var i=0;i<lists.length;i++){ var r=$(lists[i]).prop("checked"); if(r==true) { $(lists[i]).prop("checked",false) } } }) //反向全选 2.利用 each迭代 boxs.click(function(){ var c=0; $.each(boxs,function(i,v){ if($(boxs[i]).prop("checked")==true){ c++; if(c==boxs.length){ allbox.prop("checked",true); } else allbox.prop("checked",false); } }) }) //反向全选 1.利用for循环 boxs.click(function(){ var c=0;//计数器 for(var i=0;i<boxs.length;i++){ //如果多个复选框都为true var r=$(boxs[i]).prop("checked"); if(r==true){ c++;//计数器++ //如果计数器等于多个复选框,将全选框设置true if(c==boxs.length) { allbox.prop("checked",true); } //否则全选为false else allbox.prop("checked",false); } } }) }) </script>
2020javaweb教程之jQuery移动
<style> select{width: 200px;height: 100px;} </style> <select name="" id="s1" size="10"> <option value="1">北京</option> <option value="2">天津</option> <option value="3">上海</option> <option value="4">重庆</option> <option value="5">石家庄</option> </select> <button id="btn1">右移>></button> <button id="btn2">全部右移</button> <button id="btn3">左移<<</button> <button id="btn4">全部左移</button> <select name="" id="s2" size="10"> </select> <script> $(function(){ var s1=document.getElementById("s1"); var s2=document.getElementById("s2"); $("button:first").click(function(){ $("#s2").append(s1.options[s1.selectedIndex]); }) $("button:eq(2)").click(function(){ $("#s1").append(s2.options[s2.selectedIndex]); }) $("button:eq(1)").click(function(){ $("#s2").append(s1.options); }) $("button:eq(3)").click(function(){ $("#s1").append(s2.options); }) }) </script>
2020javaweb教程之jQuery省市级联
<select name="" id="a"> <option value="0">请选择</option> </select> <select name="" id="b"> <option value="0">请选择</option> </select> <script> $(function(){ var arr=new Array(); arr["河北省"]=["保定","石家庄","张家口"]; arr["四川省"]=["成都","绵阳","乐山"]; arr["山东省"]=["济南","青岛","烟台","泰安"]; for(var key in arr){ $("#a").append("<option value='"+key+"'>"+key+"</option>") } $("#a").change(function(){ $("#b").empty(); $("#b").append("<option value=\"0\">请选择</option>"); var values=arr[$(this).val()]; for(var i in values){ $("#b").append("<option value='"+values[i]+"'>"+values[i]+"</option>") } }) }) </script>
2020javaweb教程之jQuery节点
父节点:parent()
子节点:children()
同胞节点:siblings()
<div id="div"> <p>div_1</p> <p>div_2</p> <p id="p3">div_3</p> <p>div_4</p> <p>div_5</p> <div id="div2"> <p>div2_1</p> <p>div2_2</p> </div> </div> (更多…)
2020javaweb教程之jQuery的css样式
css样式
<style> .a{color:red;font-size: 40px;} .b{font-style: italic;color: aqua;font-size: 40px;} </style>
jQuery
<p>这是一段文本</p> <script> $(function(){ $("p").mouseover(function(){ $(this).removeClass();//移除所有的样式 $(this).addClass("a");//添加样式 a }).mouseout(function(){ $(this).removeClass();//移除所有的样式 $(this).addClass("b");//添加样式 b }) }) </script>
2020javaweb教程之jQuery数组遍历
数组遍历
<script> var arr=[1,2,3,4,5]; $(function(){ $.each(arr,function(i,v){ console.log(i+"..."+v) }) }) </script>
2020javaweb教程之jQuery添加元素和删除元素
添加元素
<p id="p1">这是条默认的文本</p> <button>在默认文本内部前面添加文本</button> <button>在默认文本内部后面添加文本</button><br> <button>在默认文本外部上面添加元素</button> <button>在默认文本外部后面添加元素</button> <script> (更多…)
2020javaweb教程之jQuery设置属性、获取属性
获取
DOM:document.getElementById(“ele”);
jquery:$(“#ele”)
DOM:操作的是JavaScript原生对象
jQuery:操作的是自己封装的对象,当中包含了原生对象
注意:只有jQuery对象才可以操作jQuery函数
获得内容
<p>这是第一段文本</p> <p><span>这是第二段文本</span></p> <p>这是第三段文本</p> <input type="password" id="pwd"><br> <button id="btn1">获取第一段文本内容</button> <button id="btn2">获取第二段文本html内容</button> <button id="btn3">获取密码框内容</button> (更多…)
2020javaweb教程之jQuery效果
显示、隐藏、显示及隐藏
<div id="box">这是个内容</div> <style> #box{width:500px;height:300px;border:1px solid red;} </style> <button id="btn1">3秒隐藏div</button> <button id="btn2">5秒显示div</button> <button id="btn3">5秒显示/隐藏div</button> <script> $(function(){ $("#btn1").click(function () { $("#box").hide(3000);//3秒隐藏 (更多…)
2020javaweb教程之jQuery事件
jQuery获得焦点和失去焦点
<input type="text" id="user"> <script> $(function(){ $("#user").focus(function(){//获得焦点 $(this).css("backgroundColor","red") }).blur(function(){ //失去焦点 $(this).css("backgroundColor","orange") }) }) </script>
单击、双击、鼠标移入和移出
<p>这是第一段文本</p> <p>这是第二段文本</p> <p>这是第三段文本</p> <script> $(function(){ $("p").click(function(){//单击事件 $(this).css("color","red"); }) $("p").dblclick(function () {//双击事件 $(this).css("fontSize","50px") }) $("p").hover(function(){//鼠标移入 $(this).css("color","orange") },function () { //鼠标移出 $(this).css("color","black") }) }) </script>
鼠标按下和鼠标松开
<p>你点我干哈</p> <script> $(function(){ $("p").mousedown(function(){ $(this).text("你点击了我") }).mouseup(function () { $(this).text("你点我干哈") }) }) </script>
2020javaweb教程之jQuery和jQuery选择器
概述:
jQuery是一个快速、简洁的JavaScript框架。
jQuery设计的宗旨是“Write Less,Do More”,即倡导写更少的代码,做更多的事情。
什么是jQuery?
jQuery是一个JavaScript函数库
下载jQuery:
1 jQuery.com 下载jQuery库
2.从CDN中载入jQuery
语法:<script src=”uri”></script>
(更多…)
2020javaweb教程之Ajax效验用户名
固定值测试数据
<form action="reg" method="post"> username : <input type="text" name="username" onchange="check(this)"> <span id="sp"></span><br> password : <input type="password" name="password"><br> <input type="submit" value="注册"> </form>
通过数据库查询 数据
2020javaweb教程之Ajax
概述
AJAX 是一种在无需重新加载整个网页的情况下,能够实现局部更新的技术。
什么是ajax?
AJAX = 异步 JavaScript 和 XML。 (Asynchronized JavaScript And XML)
AJAX 是一种用于快速创建动态网页的技术。
AJAX工作原理
2020javaweb教程之JavaScript的json对象
json概述
json(JavaScript object notation)JavaScript对象表示,是一种轻量级的数据交换格式。
json语法:
[] :表示数组
{} : 表示对象
” ” :表示是属性名或字符串类型的值
: 表示属性和值之间的间隔符
, 表示多个属性的间隔符或者是多个元素的间隔符 (更多…)