标签:定义 vax href address script tis head 确认密码 equal
在过滤器编写完成之后:
我们开始创建web界面与servlet:
package com.web; import com.bean.Users; import com.dao.UsersMapper; import com.service.UsersService; import com.util.MybatisUtils; import com.util.StringUtil; 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 java.io.IOException; import java.util.Date; @WebServlet(urlPatterns = "/user",name = "UsersServlet") public class UsersServlet 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 { String op = req.getParameter("op"); if("login".equals(op)){ login(req,resp); }else if("reg".equals(op)){ reg(req,resp); }else if("toPassword".equals(op)){ if(req.getSession().getAttribute("user")!=null){ req.getRequestDispatcher("/WEB-INF/password.jsp").forward(req,resp); }else{ req.getRequestDispatcher("/login.jsp").forward(req,resp); } }else if("doPassword".equals(op)){ String pwd = StringUtil.requiredString("pwd","原有密码",req); String newpwd = StringUtil.requiredString("newpwd","新的密码",req); String repwd = StringUtil.requiredString("repwd","确认密码",req); // 原有密码是否正确? Users user = (Users) req.getSession().getAttribute("user"); if(!user.getPwd().equals(pwd)){ req.setAttribute("pwd_error","原有密码录入错误!"); req.getRequestDispatcher("/WEB-INF/password.jsp").forward(req,resp); return; } // 两次录入的密码是否一致? if(!StringUtil.fieldsCompare("newpwd","repwd",req)){ req.getRequestDispatcher("/WEB-INF/password.jsp").forward(req,resp); return; } // 修改密码是否成功? UsersService usersService = new UsersService(); usersService.updatePassword(user,newpwd); // 跳转页面,提示成功?1、转到成功页面,2、javascript弹窗口提示 StringUtil.showSuccess("操作成功!","/index.jsp",req,resp); } else{ throw new ServletException("不支持的操作!"); } } private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("account"); String pwd = req.getParameter("pwd"); UsersService usersService = new UsersService(); Users user = usersService.login(name,pwd); if(user!=null){ req.getSession().setAttribute("user",user); resp.sendRedirect("/index.jsp"); }else{ resp.sendRedirect("/login.jsp"); } } private void reg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String nickname = StringUtil.requiredString("nickname","用户名",req); String realname = StringUtil.requiredString("realname","真实姓名",req); String pwd = StringUtil.requiredString("pwd","密码",req); String repwd = StringUtil.requiredString("repwd","确认密码",req); String phone = StringUtil.requiredString("phone","电话",req); String email = StringUtil.requiredString("email","邮件",req); String address = req.getParameter("address"); String realid = req.getParameter("realid"); // 校验结果判断 if(nickname == null || realname == null || pwd == null || repwd == null || phone == null || email == null || !StringUtil.fieldsCompare("pwd","repwd",req) ){ req.getRequestDispatcher("/reg.jsp").forward(req,resp); return; // 一定要做 } Users user = new Users(nickname,realname,pwd,phone,email,address,new Date(), 1,realid); // 调用业务层进行数据库操作 UsersService usersService = new UsersService(); usersService.reg(user); // 重定向到登录页面 resp.sendRedirect("/login.jsp"); } }
登录:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <p>登录</p> <h1>请登录</h1> <form action="/user" method="post"> <input type="hidden" name="op" value="login"/> <p>用户名:<input type="text" name="account"></p> <p>密码:<input type="password" name="pwd"></p> <p><input type="submit" value="登录"></p> </form> </body> </html>
主页;
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <p>首页</p> <h1>欢迎${user.nickname}登陆</h1> <ul> <li><a href= "/news/list">新闻</a></li> <!--<li><a href="/news?op=list">新闻1</a> </li>--> </ul> </body> </html>
新闻列表:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>新闻列表</title> <style type="text/css"> .usersInfo{ display:none; width:200px; background:#eee; border:1px solid #333; position:absolute; } </style> <script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ // 保证页面加载完成后在执行 // 选择器.动作 $(".users").hover(function(){ $(".usersInfo",this).css("display","block"); },function(){ $(".usersInfo",this).css("display","none"); }); }); </script> </head> <body> <p><a href="/index.jsp">首页</a>/新闻</p> <h1>新闻列表</h1> <div> <div>全部结果> ${params.keywords} </div> <div> <table> <tr> <td>新闻分类</td> <td> <span><a ${params.category_id == null ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=${params.interval}">全部</a></span> <c:forEach items="${categorys}" var="category"> <span> <a ${category.id == params.category_id ? "style=\"color:red;\"":""} href="/news?op=list&category_id=${category.id}&keywords=${params.keywords}&interval=${params.interval}"> ${category.name} </a> </span> </c:forEach> </td> </tr> <tr> <td>发布时间</td> <td> <span><a ${params.interval <= 0 || params.interval == null ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=0&category_id=${params.category_id}">最近</a></span> <span><a ${params.interval == 1 ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=1&category_id=${params.category_id}">1月内</a></span> <span><a ${params.interval == 3 ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=3&category_id=${params.category_id}">3月内</a></span> <span><a ${params.interval == 6 ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=6&category_id=${params.category_id}">半年内</a></span> <span><a ${params.interval == 12 ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=12&category_id=${params.category_id}">1年内</a></span> </td> </tr> </table> </div> </div> <table width="800" border="1"> <tr> <th>编号</th> <th>标题</th> <th>内容</th> <th>关键字</th> <th>作者</th> <th>发布</th> <th>审批</th> <th>分类</th> </tr> <c:forEach items="${list}" var="news"> <tr> <td><a href="/news?op=find&id=${news.id}">${news.id}</a></td> <td>${news.title}</td> <td>${news.content}</td> <td>${news.keywords}</td> <td class="users">${news.owner.nickname} <div class="usersInfo"> <table> <tr> <td>账号</td> <td>${news.owner.nickname}</td> </tr> <tr> <td>姓名</td> <td>${news.owner.realname}</td> </tr> <tr> <td>电话</td> <td>${news.owner.phone}</td> </tr> <tr> <td>邮件</td> <td>${news.owner.email}</td> </tr> <tr> <td>地址</td> <td>${news.owner.address}</td> </tr> </table> </div> </td> <td>${news.pubtimeString}</td> <td>${news.checkUsersName}</td> <td>${news.categoryName}</td> </tr> </c:forEach> </table> </body> </html>
servlet中的定义:
List<News> list=newDao.findAll(); //将查询到的数据库信息存储得到“请求作用域” req.setAttribute("list",list);//其中前一个定义的是页面接收对象,须于页面中接收保持一致。 req.getRequestDispatcher("/news/list.jsp").forward(req,resp);
<c:forEach items="${list}" var="news"> /*list是接收servlet中的对象,而var代表每次循环定义的对象*/ <tr> <td>${news.id}</td> <td>${news.title}</td> <td>${news.content}</td> <td>${news.keywords}</td> <td>${news.usersId}</td>> <td>${news.pubtime}</td> <td>${news.checkUsersId}</td> <td>${news.categoryId}</td> </tr> </c:forEach>
标签:定义 vax href address script tis head 确认密码 equal
原文地址:https://www.cnblogs.com/zxdup/p/12895495.html