Use Ajax and JSP technology to achieve local refresh of forms in Web pages (only refresh form data without refreshing the entire page) __jsp

Source: Internet
Author: User
Tags stmt stringbuffer

Sometimes there is a need for local refresh in web development, and the advantage of this is that it overcomes the situation that the overall refresh of the page is limited to the speed of the network.


The 1.MySQL datasheet is as follows (simple example):

Table Name: Stu_info

Stuid int PK NN

Stuname varchar (km) NN

Studesc varchar (45)


2. New wk.jsp, code as follows:

<span style= "color: #330033;" ><%@ page contenttype= "text/html; Charset=utf-8 "%>  

3. New conn.jsp, code as follows:

<span style= "color: #330033;" ></pre><p><pre name= "code" class= "HTML" ><%@ page contenttype= "text/html; Charset=utf-8 "import=" java.sql.*, Javax.naming.Context, Javax.naming.InitialContext "errorpage=" "%> <%! 
Connection conn = null; 
Statement stmt = null; 

ResultSet rs = null; 
public void Jspinit () {try {class.forname ("com.mysql.jdbc.Driver"); 
 Establish Connection conn = Drivermanager.getconnection ("Jdbc:mysql://192.168.1.106:3306/acsystem", "Ouyang", "Ouyang");
 if (conn!=null) {//system.out.println ("connect success"); 
}//context initctx = new InitialContext (); 
Context CTX = (context) initctx.lookup ("java:comp/env"); 
Gets the connection pool object//object obj = (object) ctx.lookup ("Jdbc/javasky"); 
Type conversion//javax.sql.datasource ds = (javax.sql.DataSource) obj; 
conn = Ds.getconnection (); 
stmt = Conn.createstatement (); 
catch (Exception ex) {System.out.println (ex.tostring ()); 
public void Exec (String sql) {jspinit (); try {stmt.executeupdate (sql); 
catch (Exception e) {System.out.print (e.tostring ()); 
} public ResultSet Getrs (String sql) throws SQLException {Jspinit (); 
try {rs = stmt.executequery (SQL); 
Return RS; 
catch (Exception e) {System.out.print (e); 
return null; 
} public ResultSet executequery (String sql) throws Exception {Jspinit (); 
try {sql = new String (sql.getbytes ("GBK"), "Utf-8"); 
stmt = Conn.createstatement (Java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, Java.sql.ResultSet.CONCUR_READ_ONLY); 
rs = stmt.executequery (SQL); 
catch (SQLException ex) {System.out.println ("Sql.executequery:" + ex.getmessage ()); 
} return RS; 
public string GetS (string Str) {try {byte b[] = Str.getbytes ("iso-8859-1"); 
STR = new String (b, "UTF-8"); 
catch (Exception ee) {ee.printstacktrace (); 
return Str; }%> </span>

4. New ajax.jsp, code as follows:

<span style= "color: #330033;" ><%@ page contenttype= "text/html; 
  Charset=utf-8 "import=" java.sql.*,java.util.* "errorpage=" "%> <%@ include file=" conn.jsp "%> <% try{ 
  ResultSet rs=getrs ("SELECT * from Stu_info ORDER by stuid ASC"); 
  StringBuffer content=new StringBuffer (""); 
  Response.setcontenttype ("Text/xml;charset=utf-8"); 
  Response.setheader ("Cache-control", "No-cache"); 
  Out.println ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); 
    Out.println ("<response>"); 
    while (Rs.next ()) {String id=rs.getstring ("Stuid");
    int stuid = Integer.parseint (ID); 
     String stuname=rs.getstring ("Stuname"); 
    String studesc=rs.getstring ("Studesc");
     System.out.println (STUID); 
     Out.println ("<content>"); 
     Out.println ("<id>" +stuid+ "</id>"); 
     Out.println ("<name>" +stuname+ "</name>"); 
     Out.println ("<description>" +studesc+ "</description>"); Out.println ("</contEnt> "); 
   } out.println ("</response>"); 
  Out.close (); 
   catch (Exception e) {e.printstacktrace (); }%> </span>

5. Put the above 3 pages in the project under the same folder, run wk.jsp can get the form stu_info data.

(PS: Here for a simple example, style or something can be added and modified.) You are welcome to correct your mistake. )


Related Article

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.