JSP message board source code operation implementation

Source: Internet
Author: User

Many JSP programmers aim to develop websites. The prvalue of a website is called PageRank, which is an integral part of google's ranking algorithm, the level ranges from 1 to 10, and the Level 10 is the full score. The higher the PR value, the more important the page is in the search ranking. That is to say, when other conditions are the same, websites with a high PR value have priority in the ranking of google search results.

So how to create a JSP message board to improve the PR value of your website and enhance interaction? Let's get started!

I. JSP message board: Create a database

1) Open Access2000 and create a new database. I name this database as foxdb. mdb, and C: \ tomcat \ fox \ global \ foxdb. mdb exists. Next, create a table in eagle. mdb named foxtable. The table has five fields in the text format:

The URL is used to record the IP address of the message recipient. As for the length of each field, I set "message" to 200, and the other four to 20.

2) Specify the ODBC data source named foxdb, pointing to C: \ tomcat \ fox \ global \ foxdb. mdb.

Ii. JSP message board: Compile the user's message interface

Foxnote.html, stored in C: \ tomcat \ fox \ foxnote.html:

 
 
  1. <Html>
  2. <Body>
  3. <FormMethod="Post" Action="Foxnoteinsert. jsp">
  4. <Br> Name:
  5. <InputName=Username Size=15 value= "">
  6. <Br> Email:
  7. <InputName=Email Size=15 value= "">
  8. <Br> message:
  9. <Br>
  10. <TextareaName=Doc Rows="5" Cols="40">
  11. </Textarea>
  12. <Br>
  13. <InputType=Submit Value="Submit">
  14. <InputType=Reset Value="Refill">
  15. </Form>
  16. </Bocy>
  17. </Html>
  18.  

In IE, type http: // ip/fox/foxnote.html to check whether the normal ip address is the ip address of your machine)

Iii. JSP message board: Compile foxnoteinsert. jsp

Write users' messages into the database table:

 
 
  1. ﹤body bgcolor="#FFFFFF"﹥  
  2. ﹤%@ page import="java.sql.*,MyUtil,java.util.*"%﹥   
  3. ﹤%  
  4. Connection con=null;   
  5. String username=MyUtil.gb2312ToUnicode(request.getParameter("username"));   
  6. String email=MyUtil.gb2312ToUnicode(request.getParameter("email"));   
  7. String doc=MyUtil.gb2312ToUnicode(request.getParameter("doc"));   
  8. String url=request.getRemoteAddr();   
  9. try {   
  10. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbcdbc:foxdb","","");   
  11. String str="insert into foxtable values(?,?,?,?);";   
  12. PreparedStatement pstmt=con.prepareStatement(str);   
  13. pstmt.setString(1,username);  
  14. pstmt.setString(2,email);  
  15. pstmt.setString(3,doc);   
  16. pstmt.setString(4,url);   
  17. pstmt.executeUpdate();   
  18. pstmt.close();  
  19. con.close();  
  20. }   
  21. catch(Exception e) {   
  22. out.println(e.getMessage());   
  23. }  
  24. %﹥   
  25.  

In this program, a JavaBean: MyUtil. class is used.

MyUtil is used to convert strings. Note that the JSP string is represented by Unicode code, while the form on the message board interface is represented by gb2312 code. Therefore, writing users' messages into the database also requires code conversion. If messages are directly written to the database table without conversion, garbled characters are generated. The following is the original code of MyUtil, stored in C: \ tomcat \ fox \ WEB-INF \ classes \ MyUtil. java, the compiled MyUtil. class file is also stored here.

 
 
  1. import java.io.*;   
  2. public class MyUtil{  
  3. public static String gb2312ToUnicode(String s){  
  4. try{  
  5. return new String(s.getBytes("ISO8859_1"),"gb2312");   
  6. }   
  7. catch(UnsupportedEncodingException uee){  
  8. return s;  
  9. }   
  10. }  
  11. public static String unicodeTogb2312(String s){  
  12. try{  
  13. return new String(s.getBytes("gb2312"),"ISO8859_1");  
  14. }   
  15. catch(UnsupportedEncodingException uee){  
  16. return s;  
  17. }  
  18. }  
  19. }   
  20.  

4. JSP message board: Compile foxnoteview. jsp

Used to browse existing messages in the database table and store them in C: \ tomcat \ fox \ foxnoteview. jsp. The Code is as follows:

 
 
  1. <Html>
  2. <Body>
  3. <%@ Page cLanguage="Java" Import="Java. SQL .*"%>
  4. <%
  5. ConnectionCon=Null;
  6. Try
  7. {
  8. Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  9. Con=DriverManager. GetConnection ("jdbcdbc: foxdb ","","");
  10. StatementStatement=Con. CreateStatement ();
  11. ResultSetRs=Statement. ExecuteQuery ("select * from foxtable ");
  12.  
  13. %>
  14. <TableBorder="1" Width="100%" Cellspacing="0" Cellpadding= "0"Align= "Center"Bordercolorlight="# CCCCFF" Bordercolordark="# FFFFFF">
  15. <TrBgcolor="# FFFFFF">
  16. <TdWidth="15%" Height="25" Align="Center"> <I> author </I> </td>
  17. <TdWidth="28%" Height="25" Align="Center">< I> posting time </I> </td>
  18. <TdWidth="22%" Height="25" Align="Center"> <I> Email </I> </td>
  19. <TdWidth="35%" Height="25" Align="Center"> <I> message content </I> </td>
  20. <%
  21. While (rs. next ()){
  22. Out. println ("<TR> <tdAlign=Center> <FontSize=2 color= #999999> "+ rs. getString (" author ") +" </TD> ");
  23. Out. println ("<TD> <fontSize=2 color= #999999> "+ rs. getString (" Email ") +" </font> </TD> ");
  24. Out. println ("<TD> <fontSize=2 color= #999999> "+ rs. getString (" message ") +" </font> </TD> ");
  25. Out. println ("<TD> <fontSize=2 color= #999999> "+ rs. getString (" URL ") +" </font> </TD> </TR> ");
  26. }
  27. Rs. close ();
  28. Con. close ();
  29. }
  30. Catch (Exception e)
  31. {
  32. Out. println (e. getMessage ());
  33. }
  34. %>
  35. </Table>
  36. </Body>
  37. </Html>
  38.  

By now, the entire JSP message board program has been completed. The message board is relatively simple in database applications, but it can write a variety of applications and operate a variety of databases.

  1. Analysis of JSP Design Mode
  2. JSP tutorial basics-HTML form demonstration
  3. JSP tutorials-basic articles-simple homepage Creation
  4. Specific algorithms for implementing the tree structure of JSP forums
  5. JSP entry-level website environment setup steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.