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
<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>
<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>
<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>
<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>
<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>
<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>
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)!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),