JSP显示数据库表格
- 格式:pdf
- 大小:91.31 KB
- 文档页数:3
Jsp详解1.简介2.Jsp的运行原理3.Jsp的语法1.Jsp模板元素2.Jsp中的脚本表达式3.Jsp中的脚本片段4.Jsp的声明5.Jsp注释6.Jsp指令1.首先我们来看一下page指令的用法2.下面在来看一下include指令3.最后来看一下taglib指令7.Jsp中内置的9个隐式对象8.JSP标签库1.jspinclude标签2.jspforward标签3.jspparam或者jspparams标签4.jspuseBean标签jspsetProperty标签jspgetProperty标签9.Jsp中怎么排查错误简介:JSP全称是JavaServer Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术。
JSP这门技术的最大的特点在于,写jsp就像在写html,但:它相比html而言,html只能为用户提供静态数据,而Jsp技术允许在页面中嵌套java代码,为用户提供动态数据。
相比servlet而言,servlet很难对数据进行排版,而jsp除了可以用java代码产生动态数据的同时,也很容易对数据进行排版。
不管是JSP还是Servlet,虽然都可以用于开发动态web资源。
但由于这2门技术各自的特点,在长期的软件实践中,人们逐渐把servlet作为web应用中的控制器组件来使用,而把JSP技术作为数据显示模板来使用。
其原因为,程序的数据通常要美化后再输出:让jsp既用java代码产生动态数据,又做美化会导致页面难以维护。
让servlet既产生数据,又在里面嵌套html代码美化数据,同样也会导致程序可读性差,难以维护。
因此最好的办法就是根据这两门技术的特点,让它们各自负责各的,servlet只负责响应请求产生数据,并把数据通过转发技术带给jsp,数据的显示jsp来做。
Jsp的运行原理:目标:Web服务器是如何调用并执行一个jsp页面的?Jsp页面中的html排版标签是如何被发送到客户端的?Jsp页面中的java代码服务器是如何执行的?Web服务器在调用jsp时,会给jsp提供一些什么java对象?思考:JSP为什么可以像servlet一样,也可以叫做动态web资源的开发技术?其实Jsp就是一个Servlet,所以我们要先介绍Servlet的相关技术,当我们第一次访问Jsp 的时候,Jsp引擎都会将这个Jsp翻译成一个Servlet,这个文件存放在Tomcat中的work目录中,这里,我们新建一个MyJsp.jsp页面,然后访问以下,我们看一下翻译后的源码:1.<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>2.3.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN">4.<html>5.<head>6.7.<title>My JSP 'MyJsp.jsp' starting page</title>8.9.</head>10.11.<body>12. This is my JSP page. <br>13.</body>14.</html>1.package org.apache.jsp;2.3.import javax.servlet.*;4.import javax.servlet.http.*;5.import javax.servlet.jsp.*;6.import java.util.*;7.8.public final class MyJsp_jsp extends org.apache.jasper.runtime.HttpJspBase9.implements org.apache.jasper.runtime.JspSourceDependent {10.11.private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();12.13.private static java.util.List _jspx_dependants;14.15.private javax.el.ExpressionFactory _el_expressionfactory;16.private org.apache.AnnotationProcessor _jsp_annotationprocessor;17.18.public Object getDependants() {19.return _jspx_dependants;20. }21.22.public void _jspInit() {23. _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();24. _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class .getName());25. }26.27.public void _jspDestroy() {28. }29.30.public void _jspService(HttpServletRequest request, HttpServletResponse response)31.throws java.io.IOException, ServletException {32.33. PageContext pageContext = null;34. HttpSession session = null;35. ServletContext application = null;36. ServletConfig config = null;37. JspWriter out = null;38. Object page = this;39. JspWriter _jspx_out = null;40. PageContext _jspx_page_context = null;41.42.43.try {44. response.setContentType("text/html;charset=utf-8");45. pageContext = _jspxFactory.getPageContext(this, request, response,46.null, true, 8192, true);47. _jspx_page_context = pageContext;48. application = pageContext.getServletContext();49. config = pageContext.getServletConfig();50. session = pageContext.getSession();51. out = pageContext.getOut();52. _jspx_out = out;53.54. out.write("\r\n");55. out.write("\r\n");56. out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");57. out.write("<html>\r\n");58. out.write(" <head>\r\n");59. out.write(" \r\n");60. out.write(" <title>My JSP 'MyJsp.jsp' starting page</title>\r\n");61. out.write(" \r\n");62. out.write(" </head>\r\n");63. out.write(" \r\n");64. out.write(" <body>\r\n");65. out.write(" This is my JSP page. <br>\r\n");66. out.write(" </body>\r\n");67. out.write("</html>\r\n");68. } catch (Throwable t) {69.if (!(t instanceof SkipPageException)){70. out = _jspx_out;71.if (out != null && out.getBufferSize() != 0)72.try { out.clearBuffer(); } catch (java.io.IOException e) {}73.if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);74. }75. } finally {76. _jspxFactory.releasePageContext(_jspx_page_context);77. }78. }79.}我们看到,这个类继承了org.apache.jasper.runtime.HttpJspBase,要想看到这个类的源码,我1./*2. * Licensed to the Apache Software Foundation (ASF) under one or more3. * contributor license agreements. See the NOTICE file distributed with4. * this work for additional information regarding copyright ownership.5. * The ASF licenses this file to You under the Apache License, Version 2.06. * (the "License"); you may not use this file except in compliance with7. * the License. You may obtain a copy of the License at8. *9. * /licenses/LICENSE-2.010. *11. * Unless required by applicable law or agreed to in writing, software12. * distributed under the License is distributed on an "AS IS" BASIS,13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14. * See the License for the specific language governing permissions and15. * limitations under the License.16. */17.18.package org.apache.jasper.runtime;19.20.import java.io.IOException;21.22.import javax.servlet.ServletConfig;23.import javax.servlet.ServletException;24.import javax.servlet.http.HttpServlet;25.import javax.servlet.http.HttpServletRequest;26.import javax.servlet.http.HttpServletResponse;27.import javax.servlet.jsp.HttpJspPage;28.import javax.servlet.jsp.JspFactory;29.30.import piler.Localizer;31.32./**33. * This is the super class of all JSP-generated servlets.34. *35. * @author Anil K. Vijendran36. */37.public abstract class HttpJspBase38.extends HttpServlet39.implements HttpJspPage40.41.42.{43.44.protected HttpJspBase() {45. }46.47.public final void init(ServletConfig config)48.throws ServletException49. {50.super.init(config);51. jspInit();52. _jspInit();53. }54.55.public String getServletInfo() {56.return Localizer.getMessage("");57. }58.59.public final void destroy() {60. jspDestroy();61. _jspDestroy();62. }63.64./**65. * Entry point into service.66. */67.public final void service(HttpServletRequest request, HttpServletResponse response)68.throws ServletException, IOException69. {70. _jspService(request, response);71. }72.73.public void jspInit() {74. }75.76.public void _jspInit() {77. }78.79.public void jspDestroy() {80. }81.82.protected void _jspDestroy() {83. }84.85.public abstract void _jspService(HttpServletRequest request,86. HttpServletResponse response)87.throws ServletException, IOException;88.}好吧,看到了,继承了HttpServlet类,所以说其实Jsp就是一个ServletJsp的语法:1.JSP模版元素2.JSP表达式3.JSP脚本片段4.JSP注释5.JSP指令6.JSP标签7.JSP内置对象8.如何查找JSP页面中的错误Jsp模板元素JSP页面中的HTML内容称之为JSP模版元素。
JSP 分页显示记录通过Web进行查询时,有时候由于得到的结果集太大,如果一次性全部通过网络传输过来,不仅浪费网络资源,还会导致客户端的响应时间很长,因此有必要把得到的结果集分页传递给客户端。
数据库分页显示信息是Web应用程序中经常遇到的问题,当用户的数据查询结果太多而超过计算机屏幕显示的范围时,为了方便用户的访问,往往采用数据库分页显示的方式。
对于Web编程的老手来说,编写这种代码实在是和呼吸一样自然,但是对于初学者来说,常常对这个问题摸不着头绪,因此需要对分页显示的原理和实现方法进行介绍。
所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数:●每页多少条记录($PageSize)●当前是第几页($CurrentPageID)现在只要再给一个结果集,就可以显示某段特定的结果出来。
至于其他的参数,比如:上一页($PreviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到。
首先获取记录集中记录的数目,假设总记录数为m,每页显示数量是n,那么总页数的计算公式是:如果m/n的余数大于0,总页数=m/n的商+1;如果m/n的余数等于0,总页数=m/n的商。
即:如果要显示第P页的内容,应当把游标移动到第(P-1)*n+1条记录处。
下面还是以图书信息表bookInfo为例,来实现一个数据库分页显示的案例。
在这之前需要向表bookInfo添加多条记录(具体如何添加,读者可以参考前面所学的将上述保存到Tomcat服务器运行目录下。
打开IE浏览器,在地址栏中输入ht tp://localhost:8080/JSPExample/MySQLFen.jsp,单击【转到】,会显示如图6-18所示窗口:图6-18 分页显示数据。
jsp页面显示数据导出到excel表中发表时间:2007-01-26内容来源:CSDN作者:佚名Excel报表的方法,一个过於简单,一个只能用於window平台(因为使用jdbc-odbc bridge),且无法使用到Excel内部的各种公式或是方法,因此,今天介绍一个apache出的元件叫POI,它可以在UNIX或window平台处理word或Excel档案,而不需要依靠window的com,并且可设定储存格格式、列印格式等等;今天我来介绍其中有关资料读取、新增、修改及删除的功能,若各位网友研究好其他的功能,麻烦Email给我(ljj@.tw),分享给大家!一、需要用的档案:jakarta-poi-1.8.0-dev-20020917.jar几乎每天都有1.8.0的最新版(但非正式版),正式的版本是1.5.0/builds/jakarta-poi/nightly/将档案复制到classpath所指到的地方二、有兴趣的朋友可以参考/poi/三、先建立一个叫做book1.xls的Excel档,内容如下----------------------------------项目单价数量合计CPU7000535000硬碟250025000记忆体160034800----------------------------------其中合计的栏位是设定公式,单价*数量四、资料读取範例<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %><%@ page import="org.apache.poi.poifs.filesystem.*,ermodel.*" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=MS950"><title>读取Excel档案</title></head><body><table border="1" width="100%"><%FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" );//设定FileINputStream读取Excel档POIFSFileSystem fs = new POIFSFileSystem( finput );HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFSheet sheet = wb.getSheetAt(0);//读取第一个工作表,宣告其为sheetfinput.close();HSSFRow row=null;//宣告一列HSSFCell cell=null;//宣告一个储存格short i=0;short y=0;//以巢状迴圈读取所有储存格资料for (i=0;i<=sheet.getLastRowNum();i++){out.println("<tr>");row=sheet.getRow(i);for (y=0;y<row.getLastCellNum();y++){cell=row.getCell(y);out.print("<td>");//判断储存格的格式switch ( cell.getCellType() ){case HSSFCell.CELL_TYPE_NUMERIC:out.print(cell.getNumericCellValue());//getNumericCellValue()会回传double值,若不希望出现小数点,请自行转型为intbreak;case HSSFCell.CELL_TYPE_STRING:out.print( cell.getStringCellValue());break;case HSSFCell.CELL_TYPE_FORMULA:out.print(cell.getNumericCellValue());//读出公式储存格计算後的值//若要读出公式内容,可用cell.getCellFormula()break;default:out.print( "不明的格式");break;}out.println("</td>");}out.println("</tr>");}%></table></body></html>五、资料新增範例<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %><%@ page import="org.apache.poi.poifs.filesystem.*,ermodel.*" %><html><meta http-equiv="Content-Type" content="text/html; charset=MS950"><title>插入资料至Excel档案</title></head><body><%FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" ); //设定FileINputStream读取Excel档POIFSFileSystem fs = new POIFSFileSystem( finput );HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFSheet sheet = wb.getSheetAt(0);//读取第一个工作表,宣告其为sheetfinput.close();HSSFRow row=null;//宣告一列HSSFCell cell=null;//宣告一个储存格short i=4;row=sheet.createRow(i);//建立一个新的列,注意是第五列(列及储存格都是从0起算)cell=row.createCell((short)0);cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设定这个储存格的字串要储存双位元cell.setCellValue("显示卡");cell=row.createCell((short)1);cell.setCellValue(1700);cell=row.createCell((short)2);cell.setCellValue(8);cell=row.createCell((short)3);//设定这个储存格为公式储存格,并输入公式cell.setCellFormula("B"+(i+1)+"*C"+(i+1));try{FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");wb.write(fout);//储存fout.close();out.println("储存成功<a href='book1.xls'>book1.xls</a>");}catch(IOException e){out.println("产生错误,错误讯息:"+e.toString());}%></html>六、资料删除、修改範例<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %><%@ page import="org.apache.poi.poifs.filesystem.*,ermodel.*" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=MS950"><title>删除、修改资料至Excel档案</title></head><body><%FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" ); //设定FileINputStream读取Excel档POIFSFileSystem fs = new POIFSFileSystem( finput );HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFSheet sheet = wb.getSheetAt(0);//读取第一个工作表,宣告其为sheetfinput.close();HSSFRow row=null;//宣告一列HSSFCell cell=null;//宣告一个储存格row=sheet.getRow((short)4);//取出第五列if (row!=null)sheet.removeRow(row);//先侦测第五列存不存在,若在的话将第五列删除row=sheet.getRow((short)3);//取出第四列cell=row.getCell((short)2);//取出第三个储存格cell.setCellValue(7);//设定该储存格值为7cell=row.getCell((short)3);cell.setCellFormula(cell.getCellFormula());//上两行为取出公式储存格,并重新计算(因为刚才更新过计算公式的值)//如果不做,公式计算後的值不会更新try{FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");wb.write(fout);//储存fout.close();out.println("储存成功<a href='book1.xls'>book1.xls</a>");}catch(IOException e){out.println("产生错误,错误讯息:"+e.toString());}%></body></html>/gqm1982/archive/2007/01/25/1493229.aspx本篇文章来源于站长资讯网原文链接:/0701/jsp-116977784110490.html。
JSP JSP报表企业的信息系统中,报表处理一直占比较重要的作用,本节将介绍一种生成PDF 报表的Java组件——iText。
通过在服务器端使用JSP或JavaBean生成PDF报表,客户端采用超级链接显示或下载得到生成的报表,这样就很好地解决了B/S系统的报表处理问题。
1 iText组件的配置iText组件一个能够快速产生PDF文件的Java类库,是著名的开放源码站点sourceforge的一个项目。
通过iText提供的Java类可以生成包含文本、表格、图形等只读文档,还可以将XML、HTML文件转化为PDF文件。
它的类库与java Servlet 有很好的给合。
使用iText与PDF能够使用户正确地控制Servlet的输出。
iText组件可以到/projects/itext/files/iText/iText5.0.6/网站下载,该插件有两个版本:●iText-5.0.6.jar .zip,该版本适用于Windows系统;●iText-src-5.0.6.tar.gz ,该版本适用于UNIX/Linux系统。
下载iText-5.0.6.jar文件后,需要把itext-2.0.7.jar包放入项目目录下的WEB-INF/lib路径中,这样在程序中就可以使用iText类库了。
如果生成的PDF文件中需要出现中文、日文、韩文字符,则需要访问/download.php下载iTextAsian.jar包。
2 通过iText组件生成JSP报表iText作为一个文本输出的Java开源代码,提供了PDF、HTML、RTF等多种文件格式的输出功能。
为输出各种文本提供了一个比较好的封装。
1.PDF文档输出的基本组成部分作为最基本的程序设计实践,下面通过一个经典的范例“I Love JSP”,来说明PDF文档的基本组成部分,代码如下所示。
文件名:easyReport.jsp在上述代码中,可以看出一个PDF文件的输出,总共只需要以下5个步骤。
在这里柱形图显示的数据是在jsp页面查询后台得到的数据。
因为网页的整体需要才这么做的,最好是在后台获取数据,然后传到前台。
例如把后台查询到的结果通过ResultSet传到前台,在前台拿到:request.getAttribute(“rs”,rs);1、在项目中导入相关的jar包gnujaxp.jar jfreechart-1.0.13.jar jcommon-1.0.17.jar2、在jsp页面中引入相关的jar包:<%@page import="org.jfree.chart.JFreeChart"%><%@page import="org.jfree.chart.ChartRenderingInfo"%><%@page import="org.jfree.chart.servlet.ServletUtilities"%><%@page import="org.jfree.chart.urls.StandardPieURLGenerator"%><%@page import="org.jfree.chart.entity.StandardEntityCollection"%> <%@page import="org.jfree.data.DefaultKeyedValues2D"%><%@page import="org.jfree.data.DataUtilities"%><%@page import="org.jfree.chart.ChartFactory"%><%@pageimport="bels.StandardPieSectionLabelGenerator"%> <%@page import="java.awt.Font"%><%@page import="org.jfree.chart.plot.PlotOrientation"%><%@page import="org.jfree.data.category.DefaultCategoryDataset"%> <%@page import="org.jfree.chart.axis.CategoryAxis"%><%@page import="org.jfree.chart.axis.ValueAxis"%><%@page import="org.jfree.chart.plot.CategoryPlot"%><%@page import="org.jfree.data.category.CategoryDataset"%><%@page import="org.jfree.chart.renderer.category.BarRenderer"%> <%@pageimport="org.jfree.chart.renderer.category.BarRenderer3D"%>3、在jsp页面添加如下代码,并进行适当的修改。