学生管理系统开发,框架

服务器端

package pandy.test.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import pandy.test.dao.UserDao;
import pandy.test.model.User;
import pandy.test.util.DbUtil;
import pandy.test.util.StringUtil;

public class LoginServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doPost(request,response);
    }
    DbUtil dbUtil = new DbUtil();
    UserDao userDao= new UserDao();
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String userName=request.getParameter("userName");
        String password = request.getParameter("password");
        request.setAttribute("userName", userName);
        request.setAttribute("password", password);
        if(StringUtil.isEmpty(userName)||StringUtil.isEmpty(password)) {
            
            request.setAttribute("error", "用户名或密码为空!");
            //实行服务器跳转 跳转到首页
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }
        User user = new User(userName,password);
        Connection con = null;
        try {
            con=(Connection) dbUtil.getCon();
            User currentUser = userDao.login(con, user);
            if(currentUser==null) {
                request.setAttribute("error", "用户名或密码错误!");
                //实行客户端跳转 跳转到首页
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
//          else {
//              response.sendRedirect("main.jsp");
//          }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
            
        }
    
    
}

客户端

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




学生信息管理系统登录




    
用户名:
密码:
${error }

数据库中新建数据库


学生管理系统开发,框架_第1张图片

将以上的代码部署到tomcat中,尝试进行错误运行
不输入密码:


学生管理系统开发,框架_第2张图片

输入错误的用户名密码:

学生管理系统开发,框架_第3张图片
image.png

你可能感兴趣的:(学生管理系统开发,框架)