(5).ssm下web

2021-02-10 14:18

阅读:439

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:定义   vax   href   address   script   tis   head   确认密码   equal   

在过滤器编写完成之后:

我们开始创建web界面与servlet:

 

一:创建servlet,本次创建的为UsersServlet:

技术图片

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");
    }
}

注意:href= 与form的action=,,链接到servlet上时,需与servlet的urlPatterns 值相同。

二。创建web界面

登录:

登录

登录

请登录

用户名:

密码:

主页;


  首页

首页

欢迎${user.nickname}登陆

  • 新闻

新闻列表:



  新闻列表

首页/新闻

新闻列表

全部结果> ${params.keywords}
新闻分类 null ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=${params.interval}">全部  } href="/news?op=list&category_id=${category.id}&keywords=${params.keywords}&interval=${params.interval}"> ${category.name}  
发布时间 null ? "style=\"color:red;\"":""} href="/news?op=list&keywords=${params.keywords}&interval=0&category_id=${params.category_id}">最近  } href="/news?op=list&keywords=${params.keywords}&interval=1&category_id=${params.category_id}">1月内  } href="/news?op=list&keywords=${params.keywords}&interval=3&category_id=${params.category_id}">3月内  } href="/news?op=list&keywords=${params.keywords}&interval=6&category_id=${params.category_id}">半年内  } href="/news?op=list&keywords=${params.keywords}&interval=12&category_id=${params.category_id}">1年内
编号 标题 内容 关键字 作者 发布 审批 分类
${news.id} ${news.title} ${news.content} ${news.keywords} class="users">${news.owner.nickname}
class="usersInfo">
账号 ${news.owner.nickname}
姓名 ${news.owner.realname}
电话 ${news.owner.phone}
邮件 ${news.owner.email}
地址 ${news.owner.address}
${news.pubtimeString} ${news.checkUsersName} ${news.categoryName}

注意:1.  这个是jstl中的,需引入配置。  2.其中的list是在servlet中定义的,返回到界面即可使用,如下;

        servlet中的定义:
     List list=newDao.findAll(); //将查询到的数据库信息存储得到“请求作用域” req.setAttribute("list",list);//其中前一个定义的是页面接收对象,须于页面中接收保持一致。 req.getRequestDispatcher("/news/list.jsp").forward(req,resp);

 

   /*list是接收servlet中的对象,而var代表每次循环定义的对象*/
      
${news.id}
        ${news.title}
        ${news.content}
        ${news.keywords}
        ${news.usersId}>
        ${news.pubtime}
        ${news.checkUsersId}
        ${news.categoryId}
      

 

 

 

(5).ssm下web

标签:定义   vax   href   address   script   tis   head   确认密码   equal   

原文地址:https://www.cnblogs.com/zxdup/p/12895495.html


评论


亲,登录后才可以留言!