Home Java javaTutorial Java simple user interface-implementing Java message board function

Java simple user interface-implementing Java message board function

Jan 20, 2017 am 10:59 AM

JSP+JavaBean technology was used in a message board I made before. It was a relatively complete development, so let’s learn from it! It may be used in this internship project, so I thought of it Reviewed it.

JSP+JavaBean message board technology

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<span style="font-size:16px;">Messages.html 

<HTML></span><span style="font-size:16px;"><HEAD> <TITLE> message board </TITLE></HEAD> 

<BODY> <center>留言板</center> 

<FORM action="addMessage.jsp"

 <TABLE  border=1 align="center"

 <TR><TD>姓名:</TD><TD><input type="text" name="name" size=25> 

    </TD></TR> 

 <TR><TD>E-mail:</TD> 

    <TD><input type="text" name="email" size=25></TD></TR> 

 <TR><TD>主题:</TD> 

    <TD><input type="text" name="title" size=25></TD></TR> 

   <TR><TD>留言:</TD> 

    <TD><textarea name="content" rows=7 cols=25></textarea> 

    </TD></TR> 

   <TR><TD colspan=3> 

<TABLE align="center" width="100%" cellspacing="0" cellpadding="0"

   <TR> 

     <TD align="center"><input type="submit" value="提交留言"></TD> 

   <TD align="center"

    <a href="viewMessages.jsp"><font size=2>查看留言</font></a></TD> 

   <TD align="center"><input type="reset" value="重新填写"></TD> 

   </TR></TABLE></TD>  </TR></TABLE></FORM></BODY></HTML></span>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

<span style="font-size:16px;">MessageData.java 

package message; 

public class MessageData  

 private String name,email,title,content; 

    //setter或者getter方法 

    public void setName(String name){this.name=name;} 

    public void setEmail(String email){ this.email=email;} 

    public void setTitle(String title){ this.title=title;} 

    public void setContent(String content){this.content=content;} 

    public String getName(){ return this.name;} 

    public String getContent(){ return this.content;} 

    public String getTitle(){ return this.title;} 

    public String getEmail(){ return this.email;}}</span>

Copy after login
Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<span style="font-size:16px;">viewMessages.jsp 

    <%@ page contentType="text/html; charset=GBK" import="message.MessageData" %> 

<%@ page import="java.util.*"%> 

<jsp:useBean id="myBean" class="message.MessageBean" scope="page"/> 

<HTML><HEAD><TITLE> show the message in the table </TITLE></HEAD> 

<BODY><p align="center">所有留言</p> 

 <TABLE  align="center" width="80%" border=1 > 

 <%     

   int message_count=0; 

   Collection <MessageData> messages=myBean.getAllMessage(); 

   Iterator <MessageData> it=messages.iterator(); 

   while(it.hasNext()){ MessageData mg=(MessageData)it.next();    

 %> 

   <tr> 

        <td width="20%">留言人:</td> 

        <td width="23%"><%=mg.getName()%></td> 

        <td width="58%" align="center"><% out.println( 

    "<a href=mailto:"+mg.getEmail()+">"+mg.getEmail()+"</a>"); 

%></td></tr> 

    <tr> 

        <td width="20%">主题:</td> 

        <td colspan="3"><%=mg.getTitle()%></td> 

    </tr> 

    <tr> 

        <td width="20%">内容:</td> 

        <td colspan="3"><%=mg.getContent()%></td> 

    </tr> 

   <%  message_count++; 

  }  

   %> 

 </Table> 

<p align="center"><a href="Messages.html">我要留言</a></p> 

</body></html></span>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

<span style="font-size:16px;">addMessage.jsp 

<%@ page language="java" contentType="text/html; charset=GBK"

    pageEncoding="GBK"%> 

<jsp:useBean id="Mdata" class="message.MessageData" scope="page"

    <jsp:setProperty name="Mdata" property="*"/></jsp:useBean> 

<jsp:useBean id="myBean" class="message.MessageBean" scope="page"/> 

<HTML><HEAD><TITLE> message into table </TITLE></HEAD> 

<BODY> 

<% try {  myBean.setMessage(Mdata);   myBean.addMessage(); } 

   catch(Exception e) { e.printStackTrace();} 

%> 

<jsp:forward page="viewMessages.jsp" /> 

</body></html></span>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

<span style="font-size:16px;">MessageData.java 

package message; 

public class MessageData  

 private String name,email,title,content; 

    //setter或者getter方法 

    public void setName(String name){this.name=name;} 

    public void setEmail(String email){ this.email=email;} 

    public void setTitle(String title){ this.title=title;} 

    public void setContent(String content){this.content=content;} 

    public String getName(){ return this.name;} 

    public String getContent(){ return this.content;} 

    public String getTitle(){ return this.title;} 

    public String getEmail(){ return this.email;}}</span>

Copy after login
Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

<span style="font-size:16px;">MessageBean.java 

package message; 

import java.sql.*;   //引入java.sql包 

import java.util.*; 

public class  MessageBean { 

    private Connection con;     MessageData msg; 

    public MessageBean() 

    {  String JDriver="com.mysql.jdbc.Driver"; //定义驱动程序对象  

     String userName="root"; //定义数据库用户名   

     String userPasswd=""; //定义数据库存取密码   

     String dbName="message"; //定义数据库名   

     String conURL="jdbc:mysql://localhost:3306/"+dbName; 

     try{Class.forName(JDriver).newInstance(); //加载JDBC驱动程序 

   con=DriverManager.getConnection(conURL,userName,userPasswd);  

    //连接数据库 

        

    catch(Exception e){System.err.println(e.getMessage());} 

    

   public  void  setMessage(MessageData msg) {this.msg=msg;} 

   //   添加一条留言消息 

    public void addMessage()throws Exception 

    try{ byte b1[]=msg.getTitle().getBytes("ISO-8859-1"); 

            String ti=new String(b1); 

            byte b2[]=msg.getName().getBytes("ISO-8859-1"); 

            String na=new String(b2); 

            byte b3[]=msg.getEmail().getBytes("ISO-8859-1"); 

            String em=new String(b3); 

            byte b4[]=msg.getContent().getBytes("ISO-8859-1"); 

            String c=new String(b4); 

            PreparedStatement stm=con.prepareStatement( 

    "insert into messagetable values(?,?,?,?)"); 

            stm.setString(1,ti);  stm.setString(2,na); 

           if((msg.getEmail()).length()==0)stm.setString(3,""); 

           else stm.setString(3,em);     

           stm.setString(4,c); 

           try  {stm.execute();   stm.close();  }  

           catch(Exception e) { }            

           con.close();  //关闭数据库连接 

        

       catch(Exception e){  e.printStackTrace(); throw e;} 

    

   //   获得所有留言消息,并返回结果到JSP页面 

  public Collection<MessageData> getAllMessage()throws Exception 

    { Collection<MessageData> ret=new ArrayList<MessageData>(); 

    try{ Statement stm=con.createStatement(); 

         ResultSet result=stm.executeQuery( 

    "select count(*) from messagetable");        

        int message_count=0; 

        if(result.next()){  message_count=result.getInt(1); 

                result.close(); } 

        if(message_count>0) 

        { result=stm.executeQuery("select * from messagetable "); 

      while(result.next()) 

    { String title=result.getString("title"); 

          String name=result.getString("name"); 

          String email=result.getString("email"); 

          String content=result.getString("content"); 

          MessageData message=new MessageData(); 

          message.setTitle(title); message.setName(name); 

    message.setEmail(email); message.setContent(content); 

    ret.add(message);                        

        

        result.close();     stm.close(); 

      }          

    con.close();         

    

    catch(Exception e) 

     { e.printStackTrace(); throw e; } 

    return ret; 

    

}</span>

Copy after login

A message board with simple functions, but clearly illustrates the use of JSP+JavaBean technology. I believe this example can help us grasp the technology more clearly. Principles of technology.

The above is the java simple user interface-implementing the java message board function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles