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标签来实现注册页面跳转
登录页面效果:
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
属性提交的路径。
最后项目路径如下: