首页
统计
4K壁纸
留言
Search
1
自动采集头像+壁纸+流量主小程序
935 阅读
2
【腾讯云】低价服务器活动和新用户活动
507 阅读
3
Spring MVC 结构体系
426 阅读
4
Spring Di依赖注入环境
384 阅读
5
springboot
341 阅读
技术分享
学习笔记
jsp
Maven
Mybatis
Spring
Hibernate
系统源码
网站源码
APP源码
微信程序
小程序
登录
/
注册
Search
标签搜索
学习笔记
Hibernate
小程序
Mybatis
源码
教程
笔记
Ajax
微信
活动
福利
MyEclipse 10
笔记fen'x
壁纸小程序
微信程序
HQL
笔记分享
Mybatis-Plus
小新
累计撰写
44
篇文章
累计收到
48
条评论
首页
栏目
技术分享
学习笔记
jsp
Maven
Mybatis
Spring
Hibernate
系统源码
网站源码
APP源码
微信程序
小程序
页面
统计
4K壁纸
留言
搜索到
7
篇与
jsp
的结果
2021-11-22
理解Ajax技术
好处无刷新:不刷新整个页面,之刷新局部无刷新的好处只更新部分页面,有效利用带宽提供连续的用户体验提供类似C/S的交互效果,操作更方便传统Web与Ajax的差异差异方式说明发送请求方式不同传统WebAjax技术浏览器发送同步请求异步引擎对象发送请求服务器响应不同传统WebAjax技术响应内容事一个完整的页面响应内容只是需要的数据客户端处理方式不同传统WebAjax技术传统WebAjax技术可以动态更新页面中的部分内容不影响用户在页面进行其他操作Ajax:异步刷新技术XMLHttpRequest整个Ajax技术的核心提供异步发送请求的能力常用方法方法说明open(String method,String url,boolean async,String user,String password)创建一个新的HTTP请求send(String data)发送当前请求abort()取消当前请求常用属性status:HTTP的状态码状态码说明200正确返回响应404请求的资源不存在500服务器内部错误403没有访问权限staatusText:返回当前请求的响应状态responseText:以文本形式获得响应的内容responseXML:将XML格式的响应内容解析成DOM对象返回使用Ajax验证用户名使用文本框的onblur时间使用Ajax技术实现异步交互通过XMKLHttpRequest对象通过XMLHttpRequest对象设置请求信息向服务器发送请求创建回调函数,工具响应状态动态更新页面编写服务器端处里客服端请求$.ajax()简介语法$.ajax([settings]) 常用属性参数参数类型说明urlString发送请求的参数,默认为当前页地址typeString请求方式,默认为GETdataPlainObjectStringArray发送到服务器的数据data TypeString预期服务器返回的数据类型,包括:xml,HTML Script JSON JSONP text常用函数参数 参数类型说明beforeSendFunction(jqXHR jqxhr,PlainObject settings)发送请求前调用的函数successFunction(任意类型 result,String textStatus,jqXHR jqxhr)请求成功后调用的函数参数result:可选,由服务器返回的数据。error请求失败的调用函数complete请求完成后调用的函数认识JSONJSON一种轻量级的数据交互格式。采用独立于语言的文本的文本格式通常用于在客户端和服务器之间传递数据JSON的优点轻量级交互语言结构简答易于解析定义JOSN定义JSON对象 语法var json对象 = {“name”:value}定义JSON数组 语法var JSON数组=[value ,value,....];
2021年11月22日
25 阅读
0 评论
1 点赞
【项目案例】论坛短信息
使用工具:MyExclipse10JDK1.7Navicat(Mysql)系统数据库设计根据需要创建数据库,用户表(msg_userinfo) 和短信息表(msg){tabs}{tabs-pane label="mysql代码"}create database message; use message; -- 用户表 create table msg_userinfo( username varchar(20) not null primary key comment'用户名', password varchar(20) not null comment'密码', email varchar(20) not null comment'邮箱地址' ) -- 短信息表 create table mag( msgid int not null primary key auto_increment comment'短信息id', username varchar(20) not null comment'短信息发送方', title varchar(40) not null comment'标题', msgcouent varchar(400) not null comment'消息内容', state int not null comment'消息状态', sendto varchar(20) not null comment'短信息接收方', msg_create_date datetime not null comment'消息发送时间' ) #这里好像是要连主键 ,我就不连了 {/tabs-pane}{tabs-pane label="添加数据"}-- 用户表 insert into msg_userinfo(username,password,email) values ('xiaoxin','123456','2534218070@qq.com'), ('zhanghao','123123','574455225@qq.com') -- 信息表 insert into mag(username,title,msgcouent,state,sendto,msg_create_date) values ('xiaoxin','小新博客','博客官网:www.yy0228.cn','1','zhanghao','2021-11-12'), ('zhanghao','小新主页','主页官网:yy0228.cn','1','xiaoxin','2021-11-12'){/tabs-pane}{/tabs}技术实现{tabs}{/tabs-pane}{tabs-pane label="配置文件"}<Resource name="jdbc/message" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/message?characterEncoding=utf-8" username="root" password="root" maxActive="100" maxIdle="30" maxWait="10000" />{/tabs-pane}{tabs-pane label="JNDI"}package com.xyh.dao; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class BaseJNDIDao { private Connection conn; private PreparedStatement ps; private ResultSet rs; /** *1. 打开数据库连接 * @throws Exception */ public void openConne() throws Exception{ Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/message"); conn = ds.getConnection(); } /** * 3.获取ps对象的公共方法 * @param sql * @param objs * @throws Exception */ public void getps(String sql,Object...objs) throws Exception{ ps = conn.prepareStatement(sql); for(int i = 0; i<objs.length;i++){ ps.setObject((i+1), objs[i]); } } /** * 4.通用的查询方法 * @param sql * @param objs * @return * @throws Exception */ public ResultSet executeQuery(String sql,Object... objs) throws Exception{ getps(sql,objs); rs = ps.executeQuery(); return rs; } /** * 5.通用的增删改方法 * @param sql * @param objs * @return * @throws Exception */ public int executeUpdate(String sql ,Object... objs) throws Exception{ getps(sql,objs); int count = ps.executeUpdate(); return count; } } {/tabs-pane}{tabs-pane label="Userinfo实体类"}package com.xyh.pojo; public class Userinfo { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Userinfo() { super(); } public Userinfo(String username, String password, String email) { super(); this.username = username; this.password = password; this.email = email; } @Override public String toString() { return "Userinfo [username=" + username + ", password=" + password + ", email=" + email + "]"; } } {/tabs-pane}{/tabs}项目需求用户登录和退出编写业务接口和实现类{tabs}{tabs-pane label="UserinfoDao"}package com.xyh.dao; import java.util.List; import com.xyh.pojo.Userinfo; public interface UserinfoDao { /** * 用户注册 添加数据 * @param u 注册的数据 * @return 成功数 * @throws Exception */ int addUser(Userinfo u) throws Exception; /** * 判断用户是否被注册 * @param username * @return * @throws Exception */ int selUser(String username) throws Exception; /** * 用户登录方法 * @param uf * @return 返回用户的数据 * @throws Exception */ Userinfo selLogin(Userinfo uf) throws Exception; /** * 查询全部用户账号 * @return * @throws Exception */ List<String> selusername() throws Exception; } {/tabs-pane}{tabs-pane label="UserinfoDaoImpl"}package com.xyh.dao.Impl; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.xyh.dao.BaseJNDIDao; import com.xyh.dao.UserinfoDao; import com.xyh.pojo.Userinfo; public class UserinfoDaoImpl extends BaseJNDIDao implements UserinfoDao{ @Override public int addUser(Userinfo u) throws Exception { this.openConne(); int count = 0; count = executeUpdate("insert into msg_userinfo(username,password,email) values (?,?,?)", u.getUsername(),u.getPassword(),u.getEmail()); return count; } @Override public int selUser(String username) throws Exception { this.openConne(); ResultSet rs = executeQuery("select count(1) from msg_userinfo where username =?", username); int count =0; if(rs.next()){ count = rs.getInt(1); } return count; } @Override public Userinfo selLogin(Userinfo uf) throws Exception { this.openConne(); ResultSet rs = executeQuery("select * from msg_userinfo where username=? and password=?", uf.getUsername(),uf.getPassword()); Userinfo uif = new Userinfo(); if(rs.next()){ uif.setUsername(rs.getString("username")); uif.setPassword(rs.getString("password")); uif.setEmail(rs.getString("email")); } return uif; } @Override public List<String> selusername() throws Exception { this.openConne(); List<String> namelist = new ArrayList<String>(); ResultSet rs = executeQuery("select * from msg_userinfo "); while(rs.next()){ namelist.add(rs.getString("username")); } return namelist; } } {/tabs-pane}{/tabs}{tabs}{tabs-pane label="MagDao"}package com.xyh.dao; import java.util.List; import com.xyh.pojo.Mag; public interface MagDao { /** * 根据用户名查询出信息 * @param username * @return * @throws Exception */ List<Mag> seluser(String username) throws Exception; /** * 显示一条信息 * @param id * @return * @throws Exception */ Mag selMag(int id) throws Exception; /** * 修改信息的阅读状态 * @param id * @return * @throws Exception */ int upstate(int id) throws Exception; /** * 发送信息记录 * @param m * @return * @throws Exception */ int addMag(Mag m) throws Exception; /** * 删除新闻信息 * @param id * @return * @throws Exception */ int delMag(int id) throws Exception; } {/tabs-pane}{tabs-pane label="MagDaoImpl"}package com.xyh.dao.Impl; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.xyh.dao.BaseJNDIDao; import com.xyh.dao.MagDao; import com.xyh.pojo.Mag; public class MagDaoImpl extends BaseJNDIDao implements MagDao{ @Override public List<Mag> seluser(String username) throws Exception { List<Mag> maglist = new ArrayList<Mag>(); this.openConne(); ResultSet rs = executeQuery("select * from mag where sendto =?", username); while(rs.next()){ Mag mag = new Mag(); mag.setMsgid(rs.getInt("msgid")); mag.setUsername(rs.getString("username")); mag.setTitle(rs.getString("title")); mag.setMsgcouent(rs.getString("msgcouent")); mag.setState(rs.getInt("state")); mag.setSendto(rs.getString("sendto")); mag.setMsg_create_date(rs.getString("msg_create_date")); maglist.add(mag); } return maglist; } @Override public Mag selMag(int id) throws Exception { Mag mag = new Mag(); this.openConne(); ResultSet rs = executeQuery("select * from mag where msgid=?", id); while(rs.next()){ mag.setMsgid(rs.getInt("msgid")); mag.setUsername(rs.getString("username")); mag.setTitle(rs.getString("title")); mag.setMsgcouent(rs.getString("msgcouent")); mag.setState(rs.getInt("state")); mag.setSendto(rs.getString("sendto")); mag.setMsg_create_date(rs.getString("msg_create_date")); } return mag; } @Override public int upstate(int id) throws Exception { this.openConne(); return executeUpdate("update mag set state = 1 where msgid=?", id); } @Override public int addMag(Mag m) throws Exception { this.openConne(); return this.executeUpdate("insert into mag(username,title,msgcouent,state,sendto,msg_create_date) values (?,?,?,?,?,?)", m.getUsername(),m.getTitle(),m.getMsgcouent(),m.getState(),m.getSendto(),m.getMsg_create_date()); } @Override public int delMag(int id) throws Exception { this.openConne(); return executeUpdate("delete from mag where msgid =?",id); } } {/tabs-pane}{/tabs}Service{tabs}{tabs-pane label="UserinfoService"}package com.xyh.Service; import java.util.List; import com.xyh.pojo.Userinfo; public interface UserinfoService { /** * 注册用户 * @param u * @return * @throws Exception */ int addUser(Userinfo u) throws Exception; /** * 判断用户是否被注册 * @param username * @return * @throws Exception */ int selUser(String username) throws Exception; /** * 用户登录方法 * @param uf * @return 返回用户的数据 * @throws Exception */ Userinfo selLogin(Userinfo uf) throws Exception; /** * 查询全部用户账号 * @return * @throws Exception */ List<String> selusername() throws Exception; } {/tabs-pane}{tabs-pane label="UserinfoServiceImpl"}package com.xyh.Service.Impl; import java.util.List; import com.xyh.Service.UserinfoService; import com.xyh.dao.UserinfoDao; import com.xyh.dao.Impl.UserinfoDaoImpl; import com.xyh.pojo.Userinfo; public class UserinfoSericeImpl implements UserinfoService{ UserinfoDao uf = new UserinfoDaoImpl(); @Override public int addUser(Userinfo u) throws Exception { return uf.addUser(u); } @Override public int selUser(String username) throws Exception { return uf.selUser(username); } @Override public Userinfo selLogin(Userinfo ufd) throws Exception { return uf.selLogin(ufd); } @Override public List<String> selusername() throws Exception { return uf.selusername(); } } {/tabs-pane}{/tabs}{tabs}{tabs-pane label="Mageservice"}package com.xyh.Service; import java.util.List; import com.xyh.pojo.Mag; public interface MagService { /** * 根据用户名查询出信息 * @param username * @return * @throws Exception */ List<Mag> seluser(String username) throws Exception; /** * 显示一条信息 * @param id * @return * @throws Exception */ Mag selMag(int id) throws Exception; /** * 修改信息的阅读状态 * @param id * @return * @throws Exception */ int upstate(int id) throws Exception; /** * 发送信息记录 * @param m * @return * @throws Exception */ int addMag(Mag m) throws Exception; /** * 删除新闻信息 * @param id * @return * @throws Exception */ int delMag(int id) throws Exception; } {/tabs-pane}{tabs-pane label="MageserviceImpl"}package com.xyh.Service.Impl; import java.util.List; import com.xyh.Service.MagService; import com.xyh.dao.MagDao; import com.xyh.dao.Impl.MagDaoImpl; import com.xyh.pojo.Mag; public class MagserviceImpl implements MagService{ MagDao md = new MagDaoImpl(); @Override public List<Mag> seluser(String username) throws Exception { return md.seluser(username); } @Override public Mag selMag(int id) throws Exception { return md.selMag(id); } @Override public int upstate(int id) throws Exception { return md.upstate(id); } @Override public int addMag(Mag m) throws Exception { return md.addMag(m); } @Override public int delMag(int id) throws Exception { return md.delMag(id); } } {/tabs-pane}{/tabs}JSP页面登录功能{tabs}{tabs-pane label="登录演示"} {/tabs-pane}{tabs-pane label="JSP排版代码"}<html> <head> <base href="<%=basePath%>"> <title>短信息管理系统</title> </head> <body> <div align="center"> <form action="UserServlet" method="post"> <div><img src="static/images/sms_login_title.png"></img></div><br/><br/> <input type="hidden" name="op" value="login"/> <div>用户名:<input type="text" name="username"/></div><br/> <div>密 码:<input type="password" name="password"/></div><br/><br/> <div><input type="submit" style="background:url('static/images/sms_btn_login.png');width:87px;height:39px; border:0" value="" /> <a href="reg.jsp"><img src="static/images/sms_btn_reg.png"></img></a></div> <br/> <div>${msg}</div> </form> </div> </body> </html>{/tabs-pane}{tabs-pane label="Servlet代码"}String username = req.getParameter("username"); String password = req.getParameter("password"); if ("".equals(username)) { req.setAttribute("msg", "用户名为空!不能登录"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { if ("".equals(password)) { req.setAttribute("msg", "密码为空!不能登录"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { try { Userinfo user = ufs.selLogin(new Userinfo(req .getParameter("username"), req .getParameter("password"))); if (user.getUsername() == null) { req.setAttribute("msg", "账号或密码错误!"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { req.setAttribute("user", user); req.getRequestDispatcher("admin.jsp").forward(req, resp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }{/tabs-pane}{/tabs}注册功能{tabs}{tabs-pane label="注册演示"} {/tabs-pane}{tabs-pane label="JSP排版代码"}<html> <head> <base href="<%=basePath%>"> <title>用户注册</title> </head> <body> <div align="center"> <form action="UserServlet" method="post"> <input type="hidden" name="op" value="reg"/> <div><img src="static/images/sms_reg_title.png"></img></div><br/><br/> <div>用 户 名: <input type="text" name="username"/></div><br/> <div>密 码: <input type="password" name="password"/></div><br/> <div>确认密码: <input type="password" name="passwords"/></div><br/> <div>邮 箱: <input type="email" name="email"/></div><br/><br/> <div><input type="submit" style="background:url('static/images/sms_btn_reg.png');width:87px;height:39px; border:0" value="" /> <input type="reset" style="background:url('static/images/sms_btn_reset.png');width:87px;height:39px; border:0" value="" /></div> <br/> <div>${msg}</div> <br/> <div><a href="index.jsp"><img src="static/images/sms_btn_goback.png"></img></a></div> </form> </div> </body> </html>{/tabs-pane}{tabs-pane label="Servlet代码"}req.setCharacterEncoding("utf-8"); UserinfoService ufs = new UserinfoSericeImpl(); if("reg".equals(req.getParameter("op"))){ String password = req.getParameter("password"); String passwords = req.getParameter("passwords"); if(password.equals(passwords)){ String username = req.getParameter("username"); try { if(ufs.selUser(username)>0){ req.setAttribute("msg", "提示:该用户名已经被注册!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); }else{ String eamil = req.getParameter("email"); if(eamil.equals("")){ req.setAttribute("msg", "提示:邮箱不能为空哦!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); }else{ Userinfo ui = new Userinfo(); ui.setUsername(username); ui.setPassword(passwords); ui.setEmail(eamil); int count = ufs.addUser(ui); if(count>0){ req.setAttribute("msg", "提示:注册成功赶紧登录吧!"); req.getRequestDispatcher("index.jsp").forward(req, resp); }else{ req.setAttribute("msg", "提示:不知道啥原因注册失败了"); req.getRequestDispatcher("reg.jsp").forward(req, resp); } } } } catch (Exception e) { e.printStackTrace(); } }else{ req.setAttribute("msg", "两次密码不一致!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); } }{/tabs-pane}{/tabs}主界面{tabs}{tabs-pane label="主界面"} {/tabs-pane}{tabs-pane label="排版代码"}<html> <head> <base href="<%=basePath%>"> <title>My JSP 'admin.jsp' starting page</title> <style type="text/css"> a { text-decoration: none; color: #000; } .da { width: 1000px; height: 800px; padding-top: 68px; padding-right: 20px; margin: 0 auto; background: url("static/images/sms_home_bg.png") no-repeat; margin: 0 auto } .da1 { padding-left: 30px; padding-right: 30px; } .da2 { padding-top: 28px; padding-left: 60px; padding-right: 60px; } </style> </head> <body> <div class="da"> <div class="da1"> <div align="left"> <img src="static/images/sms_my_message.png"></img> </div> <div align="right"> <p> 当前用户:${username} <a href="Magservlet?op=mag">发送消息</a> <a href="UserServlet?op=del">退出</a> </p> </div> </div> <div class="da2"> <!-- 判断是否有数据 --> <c:if test="${not empty Maglist }"> <c:forEach var="ms" items="${Maglist}" varStatus="st"> <div style="overflow:hidden"> <span style="float:left;"> <a href="Magservlet?op=sel&msgid=${ms.msgid }"> <c:choose> <c:when test="${ms.state==0 }"> <img src="static/images/sms_unReaded.png"></img> </c:when> <c:when test="${ms.state==1 }"> <img src="static/images/sms_readed.png"></img> </c:when> </c:choose> <span>${ms.title }</span> <span>${ms.msgcouent}</span> </a> </span> <span style="float:right;"> <a href="Magservlet?op=del&id=${ ms.msgid}">删除</a> <a href="Magservlet?op=mag&id=${ ms.msgid}">回信</a> <a href=""#>${ms.msg_create_date }</a> </span> </div> </c:forEach> </c:if> </div> </div> </body> </html>{/tabs-pane}{/tabs}发送信息{tabs}{tabs-pane label="发送页面"} {/tabs-pane}{tabs-pane label="排版代码"}<html> <head> <base href="<%=basePath%>"> <title>发送信息</title> <style type="text/css"> a { text-decoration: none; color: #000; } .da { width: 1000px; height: 800px; padding-top: 68px; padding-right: 20px; margin: 0 auto; background: url("static/images/sms_home_bg.png") no-repeat; margin: 0 auto } .da1 { padding-left: 30px; padding-right: 30px; } .da2 { padding-top: 28px; padding-left: 60px; padding-right: 60px; } </style> </head> <body> <div class="da"> <div class="da1"> <div align="left"> <img src="static/images/sms_my_message.png"></img> </div> ${msg } <div align="right"> <p> 当前用户:${username} <a href="#">发送消息</a> <a href="UserServlet?op=del">退出</a> </p> </div> </div> <div class="da2"> <form action="Magservlet?op=add" method="post"> <div> <span> 发送给 : <select name="user"> <c:if test="${not empty userlist}"> <c:forEach var="names" items="${userlist }"> <c:if test="${names.equals(Mag.username)}"> <option selected="selected">${names}</option> </c:if> <c:if test="${not names.equals(Mag.username)}"> <option>${names}</option> </c:if> </c:forEach> </c:if> </select> </span> <span> 标题:<input type="text" name="title" required="required" value="${Mag.title }"/> </span> </div> <br/> <div align="center"> <textarea rows="25" required="required" cols="100" name="msgcouent"></textarea> </div> <br/> <div align="center"> <input type="submit" style="background:url('static/images/sms_send.gif');width:120px;height:30px; border:0" value="" /> </div> </form> </div> </div> </body> </html>{/tabs-pane}{/tabs}Servlet代码{tabs}{tabs-pane label="Magservlet"}package com.xyh.Servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.xyh.Service.MagService; import com.xyh.Service.UserinfoService; import com.xyh.Service.Impl.MagserviceImpl; import com.xyh.Service.Impl.UserinfoSericeImpl; import com.xyh.pojo.Mag; import com.xyh.pojo.Userinfo; public class Magservlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { MagService ms = new MagserviceImpl(); req.setCharacterEncoding("utf-8"); String op = req.getParameter("op"); List<Mag> maglist = new ArrayList<Mag>(); if("sel".equals(op)){ int id = Integer.parseInt(req.getParameter("msgid")); try { ms.upstate(id); req.setAttribute("Mag",ms.selMag(id)); req.getRequestDispatcher("config.jsp").forward(req, resp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if("mag".equals(op)){ try { UserinfoService ufs = new UserinfoSericeImpl(); String idstr = req.getParameter("id"); if(idstr != null){ int id = Integer.parseInt(idstr); req.setAttribute("Mag",ms.selMag(id)); } List<String> userlist = ufs.selusername(); if(userlist.size()>0){ req.setAttribute("userlist", userlist); req.getRequestDispatcher("send.jsp").forward(req, resp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if("add".equals(op)){ String sendto = req.getParameter("user"); String title =req.getParameter("title"); String msgcouent = req.getParameter("msgcouent"); int state = 0; HttpSession session = req.getSession(); String username = (String) session.getAttribute("username"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(new Date())+""; Mag mg = new Mag(username,title,msgcouent,state,sendto,time); try { int count = ms.addMag(mg); if(count >0){ req.setAttribute("msg", "发送成功!"); req.getRequestDispatcher("Magservlet?op=mag").forward(req, resp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //删除信息功能 }else if("del".equals(op)){ try { int count = ms.delMag(Integer.parseInt(req.getParameter("id"))); HttpSession session = req.getSession(); String username = (String) session.getAttribute("username"); req.setAttribute("Maglist", ms.seluser(username)); req.getRequestDispatcher("admin.jsp").forward(req,resp); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } {/tabs-pane}{tabs-pane label="UserServlet"}package com.xyh.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.xyh.Service.MagService; import com.xyh.Service.UserinfoService; import com.xyh.Service.Impl.MagserviceImpl; import com.xyh.Service.Impl.UserinfoSericeImpl; import com.xyh.pojo.Userinfo; public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); UserinfoService ufs = new UserinfoSericeImpl(); HttpSession session = req.getSession(); if ("reg".equals(req.getParameter("op"))) { String password = req.getParameter("password"); String passwords = req.getParameter("passwords"); if (password.equals(passwords)) { String username = req.getParameter("username"); try { if (ufs.selUser(username) > 0) { req.setAttribute("msg", "提示:该用户名已经被注册!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); } else { String eamil = req.getParameter("email"); if (eamil.equals("")) { req.setAttribute("msg", "提示:邮箱不能为空哦!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); } else { Userinfo ui = new Userinfo(); ui.setUsername(username); ui.setPassword(passwords); ui.setEmail(eamil); int count = ufs.addUser(ui); if (count > 0) { req.setAttribute("msg", "提示:注册成功赶紧登录吧!"); req.getRequestDispatcher("index.jsp").forward( req, resp); } else { req.setAttribute("msg", "提示:不知道啥原因注册失败了"); req.getRequestDispatcher("reg.jsp").forward( req, resp); } } } } catch (Exception e) { e.printStackTrace(); } } else { req.setAttribute("msg", "两次密码不一致!"); req.getRequestDispatcher("reg.jsp").forward(req, resp); } } else if ("login".equals(req.getParameter("op"))) { String username = req.getParameter("username"); String password = req.getParameter("password"); if ("".equals(username)) { req.setAttribute("msg", "用户名为空!不能登录"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { if ("".equals(password)) { req.setAttribute("msg", "密码为空!不能登录"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { try { Userinfo user = ufs.selLogin(new Userinfo(req .getParameter("username"), req .getParameter("password"))); if (user.getUsername() == null) { req.setAttribute("msg", "账号或密码错误!"); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { /*req.setAttribute("user", user);*/ session.setAttribute("username",username ); MagService ms = new MagserviceImpl(); req.setAttribute("Maglist", ms.seluser(user.getUsername())); req.getRequestDispatcher("admin.jsp").forward(req, resp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }else if("del".equals(req.getParameter("op"))){ //使session立即失效 session.invalidate(); resp.sendRedirect("index.jsp"); } } } {/tabs-pane}{/tabs}{alert type="info"}完事! 以上代码先后顺序不标准 代码不标准 仅供参考!{/alert}
2021年11月15日
130 阅读
1 评论
0 点赞
2021-11-02
5.JSP开发业务应用
分页实现的思路分页显示的步骤确定每页显示的数据数量确定分页显示所需的总页数编写SQL查询语句,实现数据查询在JSP页面中进行分页显示设置Commons-FileUpload简介Commons-FileUpload组件特点使用简单:可以方便地嵌入到JSP文件中,编写少量代码既可完成文件上传的功能能够全程控制上传内容能够对上传文件的大小,类型进行控制组件的APIServletFileUpload类的常用方法方法名称方法描述public void setSizeMax(long sizeMax)设置请求信息实体内容的最大允许字节数public LIst parseRequest(HttpServletRequest req)解析form表单中的每个字符的数据,返回一个Fileltem对象集合public static final boolean isMultioartContent(HttpServletRequest req)判断请求信息中的内容 是否是multipart/form-data类型public void setHeaderEncoding(String encoding)设置转换时所使用的字符集编码FileItem接口的常用方法方法名称方法描述public boolean isFormField()判断FileItem对象封装的数据类型(普通表单字段返回true,文件表单字段返回false)public String getName()获取文件上传字段中的文件名(普通表单字段返回null)public String getFieldName()返回表单字段元素的name属性值public void write()将FileItem对象中保存的主体内容保存到指定文件public String getString()将FileItem对象中保存的主体内容以一个字符串返回,其重载方法public String getString(String encoding) 中的参数用指定的字符集编码方法是public long getSize()返回单个上传文件的字节数FileItemFactory 接口实现类:DIskFileItemFactory方法名称方法描述public void setSize Threshold(int size Threshold)设置内存缓冲区的大小public void setRepositoryPath(String path)设置临时文件存放的目录文件上传的实现编写上传文件处理页的实现步骤创建FileItemFactory对象创建ServletFileUpload对象通过构造方法:ServleFileUpload(FileItemFactory)解析form表单提交的所有表单元素数据如果是普通表单元素获取改元素的名和值使用如果是文件数据获取文件名的参数保存文件数据到服务器代码实例://1.判断请求类型是否是文件上传满足的类型(满足表单类型为enctype="multipart/form-data"并且method="post") if (ServletFileUpload.isMultipartContent(request)) { //2.创建文件缓冲区对象 FileItemFactory f = new DiskFileItemFactory(); //3.创建文件上传管理对象 ServletFileUpload sf = new ServletFileUpload(f); //4.转发请求对象中的参数为flieItem对象集合 List<FileItem> items = sf.parseRequest(request); for (FileItem it : items) { //5.判断是否为普通表单 if (it.isFormField()) { String name = it.getFieldName(); String value =it.getString("utf-8"); out.print("<p>"+name+":"+value+"</p>"); } else { String name = it.getFieldName(); String oldName = it.getName(); out.print("<p>"+"字段名:"+name+",文件名:"+oldName+"</p>"); //获取文件后缀 String suffix = FilenameUtils.getExtension(oldName); out.print("<p>文件后缀:"+suffix+"</p>"); //拼接新文件名 = 从1970.1.1到当前时间的毫秒数+四位随机数+自定义名称+.+后缀 String newName = System.currentTimeMillis() +""+(int)(Math.random() * (9999-1000 +1)+1000)+"_xyh."+suffix; out.print("<p>新文件名:"+newName+"</p>"); //获取服务器的文件上传地址: String realPath = application.getRealPath("/static/upload"); out.print("<p>服务器存储地址:"+realPath+"</p>"); //拼接上传路径 = 服务器上传地址 + 新文件名称 String uploadPath = realPath + "/"+newName; //上传文件 it.write(new File(uploadPath)); out.print("<p>上传成功</p>"); } } }
2021年11月02日
46 阅读
0 评论
1 点赞
2021-10-21
4.JSP使用分层实现业务处理
分层原则 上层依赖其下层,依赖关系不跨层 表示层不能直接访问数据访问层 上层调用下层的结果,取决于下层的实现 下层不能调上层 下层不能依赖上一层 上层的改变不会影响下一层 下层的改变会影响上一层的得到的结果 在上一层中不能出现下一层的概念 分工明确,各司其职
2021年10月21日
181 阅读
0 评论
0 点赞
2021-10-18
3.JSP数据交互(二)
page作用域 page作用域指本JSP页面的范围 requst作用域 request作用域内的对象则是与客户端的请求绑定在一起 session作用域 session作用域:一次会话 application作用域 application作用域:面对整个Web应用程序
2021年10月18日
98 阅读
0 评论
0 点赞
2021-10-16
2.JSP数据交互(一)
什么是JSP内置对象 JSP内置对象是Web容器创建的一组对象
2021年10月16日
21 阅读
0 评论
1 点赞
2021-10-16
1.JSP动态网页开发基础
为什么需要动态页面 - 无法实现搜索,购买,登录登交互功能 - 无法对静态页面的内容进行实时更新
2021年10月16日
17 阅读
0 评论
0 点赞