login页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
label {
display: block;
margin-bottom: 10px;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"],
input[type="reset"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover,
input[type="reset"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<%
session.setAttribute("name", "");
%>
<form action="login_do.jsp" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" placeholder="请输入您的用户名" />
<label for="password">密码:</label>
<input type="password" id="password" name="password" placeholder="请输入您的密码" />
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
</div>
</body>
</html>
login_do页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String n = request.getParameter("username");
String p = request.getParameter("password");
//当验证通过之后
if (n.equals("admin") && p.equals("123"))
{
session.setAttribute("name", n);
response.sendRedirect("welcom.jsp");
}
else
{
out.println("用户名和密码不正确,请重新登录!");
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
welcom页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,java.util.LinkedList,java.util.List"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>
.container {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}
input[type="text"],
textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
<body>
<%
List<String> msglist =(List<String>)application.getAttribute("msglist");
if(msglist!=null){
Iterator<String> it = msglist.iterator();
while(it.hasNext()){
String msg = it.next();
String[] m = msg.split("/");
%>
<div>
<div>标题:<%=m[1] %></div>
<div>用户名:<%=m[0] %> 发表:<%=m[3] %></div>
<div>内容:<%=m[2] %></div>
<hr>
</div>
<%
}
}
Object u = session.getAttribute("name");
if(session.getAttribute("name")==null)
{
out.println("你还没登录,请先登录!");
response.sendRedirect("login.jsp");
}
else{
out.println(session.getAttribute("name")+"欢迎你的访问!");
}
%>
<div class="container">
<form action="messag.jsp" method="post">
<label for="title">标题:</label>
<input type="text" id="title" name="title" placeholder="请输入标题" />
<label for="message">留言内容:</label>
<textarea id="message" name="message" rows="4" placeholder="请输入留言内容"></textarea>
<input type="submit" value="提交" />
</form>
</div>
</body>
messag页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,java.util.LinkedList,java.util.List"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String n = request.getParameter("name");
String t = request.getParameter("title");
String c = request.getParameter("message");
Date time = new Date();
String msg = n+"/"+t+"/"+c+"/"+time.toString();
List<String> msglist=(List<String>)application.getAttribute("msglist");
if(msglist==null){
msglist = new LinkedList<String>();
}
msglist.add(msg);
application.setAttribute("msglist", msglist);
response.sendRedirect("welcom.jsp");
%>
</body>
</html>
login.jsp 代码运行效果如下: