欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java web實(shí)現(xiàn)簡(jiǎn)單留言板功能

 更新時(shí)間:2020年11月24日 11:36:41   作者:今天肝代碼了嗎  
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)簡(jiǎn)單留言板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java web實(shí)現(xiàn)簡(jiǎn)單留言板的具體代碼,供大家參考,具體內(nèi)容如下

一、目標(biāo)

用戶可以登錄并記住密碼進(jìn)入留言板,添加留言,點(diǎn)擊留言列表中的標(biāo)題可顯示該條留言全部?jī)?nèi)容。

二、相關(guān)知識(shí)

1、使用Cookie實(shí)現(xiàn)自動(dòng)登錄

用戶第一次登錄網(wǎng)站,服務(wù)器將用戶名和密碼以Cookie的形式發(fā)送到客戶端。當(dāng)客戶之后再次訪問該網(wǎng)站時(shí),瀏覽器自動(dòng)將Cookie文件中的用戶名和密碼隨請(qǐng)求一起發(fā)送到服務(wù)器,服務(wù)器從Cookie中取出用戶名和密碼并且通過驗(yàn)證。

2、java類的定義與使用

定義java類時(shí)注意屬性的定義、構(gòu)造方法的重寫,定義兩個(gè)方法,一個(gè)是訪問方法(getter),一個(gè)是修改方法(setter)。

三、實(shí)現(xiàn)思路

1、登錄

輸入用戶名、密碼登錄,對(duì)應(yīng)檢測(cè)登錄BoardLoginServlet
可實(shí)現(xiàn)記住登錄信息 Cookie實(shí)現(xiàn)
登錄成功顯示留言板標(biāo)題列表頁(yè)面board-title.jsp,登錄失敗返回登錄頁(yè)面

2、顯示留言標(biāo)題

標(biāo)題列表為上下文變量lists,其中標(biāo)題為鏈接,點(diǎn)擊可顯示留言具體信息界面(跳轉(zhuǎn)到MessageServlet處理)。

list = username+":"+"<a href='MessageServlet?title="+title+"'target='_parent'>"+title+"</a>";

頁(yè)面下方有新增留言的鏈接,點(diǎn)擊后跳轉(zhuǎn)到寫留言頁(yè)面

<a href="board-write.jsp" rel="external nofollow" rel="external nofollow" >新增留言</a>

3、添加留言

輸入標(biāo)題、內(nèi)容 post給WriteServlet

4、顯示留言具體信息

在MessageServlet中遍歷查找留言并傳遞變量,顯示留言信息在board-message.jsp

四、代碼實(shí)現(xiàn)

board-login.jsp

<form action="board.do" method="post">
 <br>
 <h1 class="text" >登錄界面</h1>
   昵稱:<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

<h2>留言板</h2>
 ${lists}
<a href="board-write.jsp" rel="external nofollow" rel="external nofollow" >新增留言</a>

boarrd-write.jsp

<form action="write.do" method="post">
 標(biāo)題:<input type="text" name="title"/><br>
 內(nèi)容:<input type="text" name="text"/><br>
 <input type="submit"/>
</form>

board-message.jsp

<h2>留言詳情</h2>
 用戶名:${name}<br>
 標(biāo)題:${title}<br>
 內(nèi)容:${text}<br>
 時(shí)間:${date}<br>

Message.java(將留言信息存儲(chǔ)在Message類中)

package board;

import java.util.Date;

public class Message {
 private String title;//標(biāo)題
 private String time;//時(shí)間
 private String username;//用戶名
 private String text;//內(nèi)容
 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類對(duì)象
 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();
  //用戶名存儲(chǔ)到session中
  String username =(String)session.getAttribute("username");
  String title = request.getParameter("title");
  String text = request.getParameter("text");
  //獲取當(dāng)前時(shí)間
  SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
  Date date = new Date(System.currentTimeMillis());
  String time = formatter.format(date);
 //在messages列表中添加留言對(duì)象
  messages.add(new Message(title,time,username,text));
  context.setAttribute("messages",messages);

  String list = new String();
  //list中存放留言用戶名+留言標(biāo)題,標(biāo)題為鏈接,可跳轉(zhuǎn)到留言具體信息,并傳參title
  list = username+":"+"<a href='MessageServlet?title="+title+"'target='_parent'>"+title+"</a>";
  String lists = new String();
  //將留言標(biāo)題列表信息存到上下文對(duì)象
  if(context.getAttribute("lists")!=null){
   lists = context.getAttribute("lists") + list + "<br>";
  }
  else{
   lists = list + "<br>";
  }
  context.setAttribute("lists", lists);
  request.setAttribute("lists", lists);
 //轉(zhuǎn)發(fā)
  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");
  //獲取鏈接中的參數(shù)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;
   }
  }
  //將留言信息轉(zhuǎn)發(fā)到board-message.jsp
  RequestDispatcher dispatcher = request.getRequestDispatcher("/board-message.jsp");
  dispatcher.forward(request, response);



 }
}

(以上僅為部分代碼)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mac安裝配置jdk環(huán)境變量

    mac安裝配置jdk環(huán)境變量

    這篇文章主要為大家介紹了mac安裝配置jdk環(huán)境變量實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 解決IDEA?2022?Translation?翻譯文檔失敗:?未知錯(cuò)誤的問題

    解決IDEA?2022?Translation?翻譯文檔失敗:?未知錯(cuò)誤的問題

    這篇文章主要介紹了IDEA?2022?Translation?翻譯文檔失敗:?未知錯(cuò)誤,本文較詳細(xì)的給大家介紹了IDEA?2022?Translation未知錯(cuò)誤翻譯文檔失敗的解決方法,需要的朋友可以參考下
    2022-04-04
  • java多線程下載實(shí)例詳解

    java多線程下載實(shí)例詳解

    這篇文章主要介紹了java多線程下載,結(jié)合實(shí)例形式詳細(xì)分析了Java多線程文件傳輸?shù)脑砼c多線程下載的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • idea如何將指定目錄打成jar包

    idea如何將指定目錄打成jar包

    這篇文章主要介紹了idea如何將指定目錄打成jar包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot中使用Knife4J的解決方案

    SpringBoot中使用Knife4J的解決方案

    knife4j是為Java?MVC框架集成Swagger生成Api文檔的增強(qiáng)解決方案,這篇文章主要介紹了SpringBoot中使用Knife4J,需要的朋友可以參考下
    2022-10-10
  • JAVA 枚舉相關(guān)知識(shí)匯總

    JAVA 枚舉相關(guān)知識(shí)匯總

    這篇文章主要介紹了JAVA 枚舉相關(guān)知識(shí),文中講解的非常詳細(xì),代碼幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Java中區(qū)別.toString() ,(String),valueOf()方法

    Java中區(qū)別.toString() ,(String),valueOf()方法

    這篇文章主要介紹了Java中區(qū)別.toString() ,(String),valueOf()方法,需要的朋友可以參考下
    2017-01-01
  • SpringMVC的概念以及快速入門示例

    SpringMVC的概念以及快速入門示例

    這篇文章主要介紹了SpringMVC的概念以及快速入門示例,SpringMVC 已經(jīng)成為目前最主流的MVC框架之一,它通過一套注解,讓一個(gè)簡(jiǎn)單的 Java 類成為處理請(qǐng)求的控制器,而無須實(shí)現(xiàn)任何接口,需要的朋友可以參考下
    2023-05-05
  • Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 探討java深拷貝

    探討java深拷貝

    這篇文章主要針對(duì)java深拷貝的相關(guān)內(nèi)容進(jìn)行解析,幫助大家學(xué)習(xí)理解java深拷貝,感興趣的小伙伴們可以參考一下
    2016-02-02

最新評(píng)論