您好,登錄后才能下訂單哦!
怎么java web項目中實現一個留言板功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一、目標
用戶可以登錄并記住密碼進入留言板,添加留言,點擊留言列表中的標題可顯示該條留言全部內容。
二、相關知識
1、使用Cookie實現自動登錄
用戶第一次登錄網站,服務器將用戶名和密碼以Cookie的形式發送到客戶端。當客戶之后再次訪問該網站時,瀏覽器自動將Cookie文件中的用戶名和密碼隨請求一起發送到服務器,服務器從Cookie中取出用戶名和密碼并且通過驗證。
2、java類的定義與使用
定義java類時注意屬性的定義、構造方法的重寫,定義兩個方法,一個是訪問方法(getter),一個是修改方法(setter)。
三、實現思路
1、登錄
輸入用戶名、密碼登錄,對應檢測登錄BoardLoginServlet
可實現記住登錄信息 Cookie實現
登錄成功顯示留言板標題列表頁面board-title.jsp,登錄失敗返回登錄頁面
2、顯示留言標題
標題列表為上下文變量lists,其中標題為鏈接,點擊可顯示留言具體信息界面(跳轉到MessageServlet處理)。
list = username+":"+"<a href='MessageServlet?title="+title+"'target='_parent'>"+title+"</a>";
頁面下方有新增留言的鏈接,點擊后跳轉到寫留言頁面
<a href="board-write.jsp" rel="external nofollow" rel="external nofollow" >新增留言</a>
3、添加留言
輸入標題、內容 post給WriteServlet
4、顯示留言具體信息
在MessageServlet中遍歷查找留言并傳遞變量,顯示留言信息在board-message.jsp
四、代碼實現
board-login.jsp
<form action="board.do" method="post"> <br> <h2 class="text" >登錄界面</h2> 昵稱:<input type="text" name="username" value="${cookie.username.value}"/><br> 密碼:<input type="password" name="password" value="${cookie.password.value}"/><br><br> <input type="checkbox" name="check" value="check"/>記住密碼<br> <br> <input type="submit" value="登錄"/> <input type="reset" value="取消"/> </form>
board-title.jsp
<h3>留言板</h3> ${lists} <a href="board-write.jsp" rel="external nofollow" rel="external nofollow" >新增留言</a>
boarrd-write.jsp
<form action="write.do" method="post"> 標題:<input type="text" name="title"/><br> 內容:<input type="text" name="text"/><br> <input type="submit"/> </form>
board-message.jsp
<h3>留言詳情</h3> 用戶名:${name}<br> 標題:${title}<br> 內容:${text}<br> 時間:${date}<br>
Message.java(將留言信息存儲在Message類中)
package board; import java.util.Date; public class Message { private String title;//標題 private String time;//時間 private String username;//用戶名 private String text;//內容 public Message(){} //初始化 public Message(String title,String time,String username,String text){ this.title = title; this.time = time; this.username = username; this.text = text; } public String getTitle(){ return title; } public String getUsername(){ return username; } public String getTime(){ return time; } public String getText(){ return text; } }
BoardLoginServlet.java(用Cookie登錄)
package board; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; @javax.servlet.annotation.WebServlet(name = "BoardLoginServlet",urlPatterns = "/board.do") public class BoardLoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset = utf-8"); request.setCharacterEncoding("UTF-8"); HttpSession session=request.getSession(); String username = request.getParameter("username").trim(); String password = request.getParameter("password").trim(); session.setAttribute("username",username); if (!(username.equals("admin") &&password.equals("admin"))&&!(username.equals("123") &&password.equals("123"))) { response.sendRedirect("board-login.jsp"); } else { if ((request.getParameter("check") != null) && (request.getParameter("check").equals("check"))) { Cookie nameCookie = new Cookie("username", username); Cookie pswdCookie = new Cookie("password", password); nameCookie.setMaxAge(60 * 60); pswdCookie.setMaxAge(60 * 60); nameCookie.setPath("/"); pswdCookie.setPath("/"); response.addCookie(nameCookie); response.addCookie(pswdCookie); String value1 = "", value2 = ""; Cookie cookie = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if (cookie.getName().equals("username")) value1 = cookie.getValue(); if (cookie.getName().equals("password")) value2 = cookie.getValue(); } if (value1.equals("admin") && value2.equals("admin")||value1.equals("123") && value2.equals("123")) { response.sendRedirect("board-title.jsp"); } else { response.sendRedirect("board-login.jsp"); } } } else { response.sendRedirect("board-title.jsp"); } } } }
WriteServlet.java(新增留言)
package board; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @WebServlet(name = "WriteServlet",urlPatterns = "/write.do") public class WriteServlet extends HttpServlet { //messages列表中存放Message類對象 public List<Message> messages = new ArrayList<Message>(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); ServletContext context = request.getServletContext(); HttpSession session = request.getSession(); //用戶名存儲到session中 String username =(String)session.getAttribute("username"); String title = request.getParameter("title"); String text = request.getParameter("text"); //獲取當前時間 SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); String time = formatter.format(date); //在messages列表中添加留言對象 messages.add(new Message(title,time,username,text)); context.setAttribute("messages",messages); String list = new String(); //list中存放留言用戶名+留言標題,標題為鏈接,可跳轉到留言具體信息,并傳參title list = username+":"+"<a href='MessageServlet?title="+title+"'target='_parent'>"+title+"</a>"; String lists = new String(); //將留言標題列表信息存到上下文對象 if(context.getAttribute("lists")!=null){ lists = context.getAttribute("lists") + list + "<br>"; } else{ lists = list + "<br>"; } context.setAttribute("lists", lists); request.setAttribute("lists", lists); //轉發 RequestDispatcher dispatcher = request.getRequestDispatcher("/board-title.jsp"); dispatcher.forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
MessageServlet.java
package board; import com.sun.net.httpserver.HttpContext; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; 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.io.PrintWriter; import java.util.ArrayList; import java.util.List; @WebServlet(name = "MessageServlet",urlPatterns = "/MessageServlet") public class MessageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); ServletContext context = request.getServletContext(); List<Message> messages = new ArrayList<Message>(); //獲取列表 messages = (List<Message>)context.getAttribute("messages"); //獲取鏈接中的參數title String title =request.getParameter("title"); String name = new String(); String date = new String(); String text = new String(); //遍歷列表,查找該title的具體信息 for(Message message : messages){ if((message.getTitle()).equals(title)){ name = message.getUsername(); date = message.getTime(); text = message.getText(); title = message.getTitle(); request.setAttribute("name",name); request.setAttribute("title",title); request.setAttribute("text",text); request.setAttribute("date",date); break; } } //將留言信息轉發到board-message.jsp RequestDispatcher dispatcher = request.getRequestDispatcher("/board-message.jsp"); dispatcher.forward(request, response); } }
看完上述內容,你們掌握怎么java web項目中實現一個留言板功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。