The implementation of report printing in Web application

Source: Internet
Author: User
Tags implement mdb database odbc stmt root directory tomcat
Implementation of report printing in web| report printing Web application

With the formation of Internet environment worldwide, the world is entering a network-centric computing age. Web model is a new Browser/server computing model developed on the basis of traditional C/S model. The Browser/server model simplifies the client in C/s, the client only needs to install the operating system, network protocol software and browser, while the server side concentrates all the application logic, development, maintenance and so on almost all the work is concentrated on the server side. The client is called a thin client, and the server is called the FAT server. And the client's report print in Web application is not as easy to implement as C/S model, how to implement the settings of printing font, font color, table width, paper type, etc. without using ActiveX control. Here is a small example to illustrate.

First, create a database

Use Access to create a database named User.mdb, and then build a table named user with the following table structure:

Field name data type

Username text

Name text

Sex text

II. implementation in ASP

Create a file named Aspreport.asp with the following source code:

<%

' Wait until text is buffered to show

Response.Buffer = True

Dim Accessdb, CN, RS, SQL

' Database name

accessdb= "User.mdb"

' Connect to the database

cn= "Driver={microsoft Access DRIVER (*.mdb)};"

CN=CN & "dbq=" & Server.MapPath (ACCESSDB)

' Create a recordset

Set rs = Server.CreateObject ("ADODB.") Recordset ")

' Query statement

sql = "SELECT * from User"

' Start query

Rs. Open SQL, CN

%>

<%

' Tell the browser to use Word to display the contents of the document

Response.ContentType = "Application/msword"

' Document setting

Response. AddHeader "Content-disposition", "inline"; Filename=report.doc "

%>



<title> Report </title>


<body bgcolor= "#FFFFFF" >

<center> Report </center>

<table border=1 cellspacing=0 cellpadding=0 bordercolor= #000000 align=center>

<tr>

<td> User name </td>

<td> real name </td>

<td> Gender </td>

</tr>

<%

' Move to the first record

Rs.movefirst

Do as not rs.eof

%>

<tr>

<td> <%= RS ("username")%> </td>

<td> <%= rs ("name")%> </td>

<td> <%= rs ("Sex")%> </td>

</tr>

<%

' Move to the next record

Rs.movenext

Loop%>

<%

' Close the recordset

Rs.close

Set rs=nothing

Response.Flush

Response.End

%>

</table>

</body>


Put the above report.asp file and the User.mdb database in the same directory, then use IIS or PWS to publish their directory, open the browser, as long as the client machine on the machine word, it will automatically invoke word embedded in the browser to display, at this time can be used as word to adjust and print.

Third, in the JSP realization

Create a file named jspreport.jsp with the following source code:

<%@ page contenttype= "application/msword;charset=gb2312"%>

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



<title> Report </title>


<body bgcolor= "#FFFFFF" >

<center> Report </center>

<table border=1 cellspacing=0 cellpadding=0 bordercolor= #000000 align=center>

<tr>

<td> User name </td>

<td> real name </td>

<td> Gender </td>

</tr>

<%

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

String url= "Jdbc:odbc:user";

Connecting to the user database

Connection con=drivermanager.getconnection (URL, "", "");

Statement stmt=con.createstatement ();

ResultSet rs=stmt.executequery ("SELECT * from user");

Query User table

while (Rs.next ()) {

%>

<tr>

<td> <%= rs.getstring ("username")%> </td>

<td> <%= rs.getstring ("name")%> </td>

<td> <%= rs.getstring ("Sex")%> </td>

</tr>

<%

}

%>

<%

Rs.close ();

Stmt.close ();

Con.close ();

%>

</table>

The JSP server takes Tomcat as an example, placing the above jspreport.jsp and User.mdb under the Tomcat root directory. Set up the corresponding DSN in the Control Panel ODBC, type MDB database, the data source name is user. Start Tomcat, enter the appropriate URL, you can see the same as the above ASP effect.

The above is just a simple example, in making a report, you can use Word design, and then save as a Web page, and then use this page adapted to the corresponding script, you can achieve better results. One limitation of this approach is that the client has word software, but it is more secure and convenient than using ActiveX controls, and the platform and webserver used on the server side can be more flexible, especially for web-based OA systems.



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.