intellij +servlet 实现登陆界面

intellij +servlet 实现登陆界面

1 登录界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
<div id="login">
    <h1>新闻管理系统</h1>
    <form method="post" action="userLogin">
        <input type="text" required="required" placeholder="用户名" name="username"></input>
        <input type="password" required="required" placeholder="密码" name="passwd"></input>
        <button class="but" type="submit">登录</button>
        <a href="userRegister.html" class="buta">注册</a>

    </form>
</div>
</body>
</html>
<style>
    html{
        width: 100%;
        height: 100%;
        overflow: hidden;
        font-style: italic;
    }
    body{
        width: 100%;
        height: 100%;
        font-family: 'Open Sans',sans-serif;
        margin: 0;
        background-color: #4A374A;
    }
    #login{
        position: absolute;
        top: 50%;
        left:50%;
        margin: -150px 0 0 -150px;
        width: 300px;
        height: 300px;
    }
    #login h1{
        color: #fff;
        text-shadow:0 0 10px;
        letter-spacing: 1px;
        text-align: center;
    }
    h1{
        font-size: 2em;
        margin: 0.67em 0;
    }
    input{
        width: 278px;
        height: 18px;
        margin-bottom: 10px;
        outline: none;
        padding: 10px;
        font-size: 13px;
        color: #fff;
        text-shadow:1px 1px 1px;
        border-top: 1px solid #312E3D;
        border-left: 1px solid #312E3D;
        border-right: 1px solid #312E3D;
        border-bottom: 1px solid #56536A;
        border-radius: 4px;
        background-color: #2D2D3F;
    }
    .but{
        width: 100px;
        min-height: 20px;
        position:relative;
        top:17px;
        background-color: #4a77d4;
        border: 1px solid #3762bc;
        color: #fff;
        padding: 9px 14px;
        font-size: 15px;
        box-sizing: border-box;
        line-height: normal;
        border-radius: 5px;
        margin: 0;
    }
    .buta{
        display:inline-block;
        width: 100px;
        min-height: 20px;
        position:relative;
        top:17px;
        background-color: #4a77d4;
        border: 1px solid #3762bc;
        color: #fff;
        padding: 9px 14px;
        font-size: 15px;
        box-sizing: border-box;
        line-height: normal;
        border-radius: 5px;
        margin: 0;
    }
</style>

使用a标签来实现注册页面跳转

登录页面效果:

image-20201027132134554

2 登录验证代码实现

package user_servlet;

import java.sql.*;
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;


@WebServlet(name = "userLogin")
public class userLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pass = request.getParameter("passwd");
        String path;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得数据库连接对象
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/news?useSSL=FALSE&serverTimezone=UTC", "root", "password");
            //获取执行sql对象statement
            Statement st = conn.createStatement();
//            定义sql语句
            String sql = "select * from users where username=" + "'" + username + "'" + "and password='" + pass + "'";
//            使用resultset接受返回数据
            ResultSet rs = st.executeQuery(sql);
//            输出结果
            if (rs.next()) {
                System.out.println("success");
                path = "user_main.jsp";
                return;
            } else {
                path = "user_login.html";
                System.out.println("error");
            }
            response.sendRedirect(path);
            st.close();
            conn.close();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
};

response.sendRedirect(path);是实现登陆验证成功后实现页面的跳转

如果sql语句查询不到结果那么resultset接受返回数据是null或者为空集,可以使用if (rs.next())来判断是否登录成功

3 进行页面绑定

再web.xml文件中写入如下代码:

    <servlet>
        <servlet-name>userLogin</servlet-name>
        <servlet-class>user_servlet.userLogin</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>userLogin</servlet-name>
        <url-pattern>/userLogin</url-pattern>
    </servlet-mapping>

servlet-name为servlet(及后端验证)的文件名,servlet-class即为该文件路径,url-pattern即为上面form表单action属性提交的路径。

最后项目路径如下:
image-20201027132058346

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值