码迷,mamicode.com
首页 > 其他好文 > 详细

简单servlet调用dao层完整步骤

时间:2019-06-24 21:16:40      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:nbsp   classes   表单提交   vax   intellij   编译   isp   glib   amp   

导入包lib(文件名称)

目录结构:web下:views、web-inf、index.jsp

views下各种jsp文件和js(里面放封装好的jquery包)

js下:jquery包(js文件后缀)

web-inf目录下:classes(编译输出路径)、lib、 web.xml

技术图片

 

首先编写index.jsp文件

 

<form method="post" action="/touhou/isLogin">
    <input type="text" required name="admin" ><br/><br/>
    <input type="password" required name="password" ><br/><br/>
    <span id="errorMessage">&nbsp;${error}</span><br/><br/>
    <input type="submit" value="LinkStart">
  </form>

 

 

 

 接着响应表单提交的servlet    “isLogin”

package com.aaa.servlet;

import com.aaa.dao.IUserDAO;
import com.aaa.dao.impl.UserDAOImpl;

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.Map;

@WebServlet("/isLogin")
public class IsLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式为UTF-8
        req.setCharacterEncoding("UTF-8");
        //将表单中获取到的元素提取出来
        String admin=req.getParameter("admin");
        String password=req.getParameter("password");
        //将这些元素引入dao层进行比对
        IUserDAO userDAO = new UserDAOImpl();
        Map<String, Object> map = userDAO.isLogin(admin, password);
        //查看比对结果并进行相应后续处理
        if(map!=null){
            //map不为空时设置map属性值并请求转发到登陆成功的主页
            req.setAttribute("map",map);
            System.out.println(map);
            req.getRequestDispatcher("/views/loginSuccess.jsp").forward(req,resp);
        }else {
            //失败时将错误信息传递给
            req.setAttribute("error","账号或者密码不正确");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }

    }
}

当然,最后判断是否成功

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/24
  Time: 10:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%--进行list循环时需要用到的--%>
<html>
<head>
    <title>Title</title>
</head>
<%--添加jquery代码实现--%>
<script src="js/jquery-2.1.0.js"></script>
<body>
<span>欢迎你,可爱的${map.mininame}</span>
</body>
</html>

 

简单servlet调用dao层完整步骤

标签:nbsp   classes   表单提交   vax   intellij   编译   isp   glib   amp   

原文地址:https://www.cnblogs.com/fanqiexin/p/11079330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!