本文承接上文你品,你細品留言板功能的總結,本人新手有代碼可以優化的地方法,歡迎大家指出。
已上傳github,有需要的可以看一下
目錄展示
dao層:數據庫連接層
domain:實體類
service層:業務層
test層:測試代碼層
util層:工具層
web.servlet:主要是用來轉發頁面的
代碼邏輯
用戶登錄
login.jsp---> LoginServlet(獲取用戶的賬號及密碼進行校驗)-->
1.登錄成功,進入NewsListServlet
2.登錄失敗,進入login.jsp進行回顯
用戶註冊
regist.jsp --> RegistServlet(獲取用戶的賬號及密碼進行註冊)-->可以選擇進行登錄
留言板顯示
通過NewListServlet獲取到數據庫的list信息--> 在list.jsp中進行顯示
刪除留言
點擊刪除按鈕-->DelNewsservlet(調用service數據庫的刪除)-->NewsListServlet-->list.jsp
添加留言
點擊添加按鈕-->填充留言板內容-->AddNewsServlet-->NewsListServlet-->list.jsp
修改留言
這個相對來說就比較複雜一點了,首先要進行信息的回顯
更改按鈕--> FindNewsList-->update.jsp
修改留言 : 確定按鈕-->UpdateNewsServlet-->NewsListServlet-->list..jsp
代碼實現
login.jsp
<%--
Created by IntelliJ IDEA.
User: 張振東
Date: 2020/4/12
Time: 21:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>留言板---登錄界面</title>
<style>
.td_left{
padding-top: 50px;
width: 100px;
text-align: right;
height: 45px;
}
.td_right{
/* 本身就是表格,就是內邊距*/
padding-left:50px ;
padding-top: 50px;
}
#username,#password{
width: 251px;
height: 33px;
border: 1px solid ;
/* 邊框圓角*/
border-radius: 5px;
padding-left: 10px;
}
.rg_layout {
width: 1000px;
height: 700px;
background: white;
border: 8px solid #EEEEEE;
/*div居中*/
margin: auto;
margin-top: 20px;
}
.rg_left {
float: left;
margin-top: 15px;
}
.rg_center {
margin-left: 200px;
margin-top: 10px;
/*border: 1px solid #FF0000;*/
float: left;
width: 450px;
}
.rg_right {
margin: 15px;
float: right;
}
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.rg_left >p:first-child{
color: red;
font-size: 20px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 20px;
}
.rg_right p{
font-size: 15px;
}
.rg_right p a{
color: pink;
}
#btn_submit{
width: 150px;
height: 50px;
margin-top: 50px;
border:1px solid yellow;
background-color: yellow;
}
</style>
<script>
var error='<%=request.getParameter("error")%>';
if(error=='yes'){
alert("登錄失敗");
}
</script>
</head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p>留言板用戶登錄</p>
<p>USER LOGIN</p>
</div>
<div class="rg_center">
<div class="rg_from">
<form action="loginServlet" method="post">
<table border="0">
<tr>
<td class="td_left"><label for="username">用戶名</label></td>
<td class="td_right"><input type="text" name="username" id="username" placeholder="請輸入賬號名稱"></td>
</tr>
<tr>
<td class="td_left"><label for="password">密碼</label></td>
<td class="td_right"><input type="password" name="password" id="password" placeholder="請輸入密碼"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="登錄" id="btn_submit"></td>
</tr>
</table>
</form>
</div>
</div>
<div class="rg_right">
<p>還未註冊?<a href="regist.jsp">趕快去註冊</a></p></div>
</div>
<footer>
<center>
<font size=5px" color="#a9a9a9" face="楷體">©️1813004745張振東 </font><font color="red" size="5px"><a href="admin.jsp">管理後臺</a></font>
</center>
</footer>
</body>
</html>
regist.jsp
<%--
Created by IntelliJ IDEA.
User: 張振東
Date: 2020/4/12
Time: 21:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>留言板---註冊界面</title>
<style>
.td_left{
padding-top: 50px;
width: 100px;
text-align: right;
height: 45px;
}
.td_right{
/* 本身就是表格,就是內邊距*/
padding-left:50px ;
padding-top: 50px;
}
#username,#password{
width: 251px;
height: 33px;
border: 1px solid ;
/* 邊框圓角*/
border-radius: 5px;
padding-left: 10px;
}
.rg_layout {
width: 1000px;
height: 700px;
background: white;
border: 8px solid #EEEEEE;
/*div居中*/
margin: auto;
margin-top: 20px;
}
.rg_left {
float: left;
margin-top: 15px;
}
.rg_center {
margin-left: 200px;
margin-top: 10px;
/*border: 1px solid #FF0000;*/
float: left;
width: 450px;
}
.rg_right {
margin: 15px;
float: right;
}
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.rg_left >p:first-child{
color: red;
font-size: 20px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 20px;
}
.rg_right p{
font-size: 15px;
}
.rg_right p a{
color: pink;
}
#btn_submit{
width: 150px;
height: 50px;
margin-top: 50px;
border:1px solid yellow;
background-color: yellow;
}
</style>
<script>
var error1='<%=request.getParameter("error")%>';
if(error1=='no'){
alert("註冊成功");
}
if(error1=='yes'){
alert("註冊失敗");
}
</script>
</head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p>留言板用戶註冊</p>
<p>USER REGISTER</p>
</div>
<div class="rg_center">
<div class="rg_from">
<form action="registServlet" method="post">
<table border="0">
<tr>
<td class="td_left"><label for="username">用戶名</label></td>
<td class="td_right"><input type="text" name="username" id="username" placeholder="請輸入賬號"></td>
</tr>
<tr>
<td class="td_left"><label for="password">密碼</label></td>
<td class="td_right"><input type="password" name="password" id="password" placeholder="請輸入密碼"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="註冊" id="btn_submit" ></td>
</tr>
</table>
</form>
</div>
</div>
<div class="rg_right">
<p>註冊成功?<a href="login.jsp">立即登錄</a></p></div>
</div>
</body>
</html>
LoginServlet
package cn.zzd.web.servlet;
import cn.zzd.dao.impl.UserDaoImpl;
import cn.zzd.domain.User;
import cn.zzd.service.UserService;
import cn.zzd.service.impl.UserServiceImpl;
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;
/**
* @author 張振東
* @version V1.0
* @Title:
* @Package
* @Description: (用一句話描述該文件做什麼)
* @date:
*/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設置編碼
req.setCharacterEncoding("utf-8");
//獲取請求參數
String username=req.getParameter("username");
String password=req.getParameter("password");
//封裝user對象
User loginUser = new User();
loginUser.setPassword(password);
loginUser.setUsername(username);
//獲取所有請求參數
UserService userService = new UserServiceImpl();
User user =userService.login(loginUser);
if(user==null){
//登錄失敗
resp.sendRedirect("login.jsp?error=yes");
}else{
//登錄成功
//存儲數據
req.setAttribute("username",username);
req.getRequestDispatcher("/newsListServlet?username="+username).forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
RegistServlet:
package cn.zzd.web.servlet;
import cn.zzd.dao.impl.UserDaoImpl;
import cn.zzd.domain.User;
import cn.zzd.service.UserService;
import cn.zzd.service.impl.UserServiceImpl;
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;
/**
* @author 張振東
* @version V1.0
* @Title:
* @Package
* @Description: (用一句話描述該文件做什麼)
* @date:
*/
@WebServlet("/registServlet")
public class RegistServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設置編碼
request.setCharacterEncoding("utf-8");
//獲取請求參數
String username=request.getParameter("username");
String password=request.getParameter("password");
//封裝user對象
User registUser = new User();
registUser.setPassword(password);
registUser.setUsername(username);
//獲取所有請求參數
UserService userService = new UserServiceImpl();
int result =userService.regist(registUser);
if(result!=0){
response.sendRedirect("regist.jsp?error=no");
}else{
response.sendRedirect("regist.jsp?error=yes");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
NewsListServlet:
package cn.zzd.web.servlet;
import cn.zzd.domain.News;
import cn.zzd.service.NewsService;
import cn.zzd.service.impl.NewsServiceImpl;
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;
import java.util.List;
/**
* @author 張振東
* @version V1.0
* @Title:
* @Package
* @Description: (用一句話描述該文件做什麼)
* @date:
*/
@WebServlet("/newsListServlet")
public class NewsListServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
NewsService service = new NewsServiceImpl();
List<News> news = service.findall();
request.setAttribute("news", news);
//System.out.println(request.getParameter("username"));
if ("1813004745".equals(request.getParameter("username"))||request.getParameter("username")==null) {
request.getRequestDispatcher("/list2.jsp").forward(request, response);
} else {
request.getRequestDispatcher("/list.jsp?username=" + request.getParameter("username")).forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
更多的代碼可以去我的github看看!!!
長路漫漫,JAVA爲伴!!!