欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Srevlet开发的项目——2.登录界面

程序员文章站 2023-12-31 18:50:04
login.jsp页面<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@page isELIgnored="false" %>&...

login.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=request.getContextPath()%>/"/>
    <title>用户登录</title>
    <link type="text/css" rel="stylesheet" href="resources/css/style.css;">
</head>
<body bgcolor="#E7ECEF">
    <center>
        <form action="/loginServlet" method="post">
            <table border="0" cellspacing="0" cellpadding="0" style="margin-top:130px">
                <tr><td><img src="resources/images/logon_top.gif"></td></tr>
                <tr height="180">
                    <td background="resources/images/logon_middle.gif" align="center" valign="top">
                         <table border="0" width="90%" cellspacing="0" cellpadding="0">
                             <tr height="50"><td colspan="2"></td></tr>
                             <tr height="30">
                                 <td align="right" width="40%">用户名:&nbsp;&nbsp;</td>
                                 <td style="text-indent:5px">
                                     <input type="text" name="username" size="30px" value="" id="log_Login_action_user_userName"/>
                                     <span>${error}</span>
                                 </td>
                             </tr>                
                             <tr height="30">
                                 <td align="right">&nbsp;&nbsp;码:&nbsp;&nbsp;</td>
                                 <td style="text-indent:5px"><input type="password" name="password" size="30px" id="log_Login_action_user_userPassword"/></td>
                             </tr>
                             <tr height="60">
                                 <td></td>
                                 <td>
								
                                     <input type="submit" id="" value="登录"/>

                                     <input type="reset" value="重置"/>

                                     <a id="log_Login_action_" href="resources/view/indextemp.jsp">[返回首页]</a>
                                 </td>
                             </tr>
                         </table>
                    </td>
                </tr>
                <tr><td><img src="resources/images/logon_end.gif"></td></tr>
            </table>
        </form>



     
    </center>
</body>
</html>

AdminTOLoginServlet.java

package controller.admin;

import dao.UserDao;
import pojo.User;

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 javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 管理员登录
 */
@WebServlet(name = "AdminTOLoginServlet",urlPatterns = "/loginServlet")
public class AdminTOLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理中文乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //根据管理员的账号 查询管理员的信息(账号和密码)
        UserDao userDao=new UserDao();
       User user= userDao.selectAdminInfo(username);
       //对管理员登录,我们只需要判断密码和账号即可
        if(user.getUserName().equals(username)&&user.getUserPassword().equals(password)){
            //用户名和密码都匹配上了  去首页   同时绑定用户名到session中
            HttpSession session= req.getSession();//默认生命时长30m
            session.setAttribute("loginName",username);
           //将session绑定到
            //跳转到主页 转发
            req.getRequestDispatcher("/admin/AdminTemp.jsp").forward(req,resp);

        }else{
            //用户名或密码错误  将错误信息显示在登录页面
            req.setAttribute("error","用户名或者密码错误!");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }




    }
}

@WebServlet(name = “AdminTOLoginServlet”,urlPatterns = “/loginServlet”)

上面的两句就是servlet实现前台与后台交互的关键,所以一定要把名字写对。

本文地址:https://blog.csdn.net/sfz1234567890/article/details/109259201

上一篇:

下一篇: