分享
 
 
 

servlet与javabean实现长文本数据分页显示

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

这是做wap的时候遇到的问题,您遇到类似的问题可以参考一下!

javabean文件:

DataConnection.java;//数据库连接

BasicInfo.java;//数据bean容器

DataWork.java;//数据操作

servlet文件:

OrderInfoServlet.java;//servlet实现长文本数据分页显示

xml配置文件:

web.xml;//servlet配置

DataConnection.java文件内容为:

package wap_cctv_film;

import java.sql.*;

public class DataConnection {

public static Connection getDataConnection() {

Connection conn = null;

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=wapvod","cctv_sql_user","cctv_sql_user");

}

catch (Exception ex) {

ex.printStackTrace();

}

return conn;

}

}

BasicInfo.java文件内容:

package wap_cctv_film;

import java.io.*;

public class BasicInfo

implements Serializable {

private String basicName;

private int basicHits;

private int basicID;

private String basicText;

private int basicIsShow;

private int basicType;

public String getBasicName() {

return basicName;

}

public void setBasicName(String basicName) {

this.basicName = basicName;

}

public int getBasicHits() {

return basicHits;

}

public void setBasicHits(int basicHits) {

this.basicHits = basicHits;

}

public int getBasicID() {

return basicID;

}

public void setBasicID(int basicID) {

this.basicID = basicID;

}

public String getBasicText() {

return basicText;

}

public void setBasicText(String basicText) {

this.basicText = basicText;

}

public int getBasicIsShow() {

return basicIsShow;

}

public void setBasicIsShow(int basicIsShow) {

this.basicIsShow = basicIsShow;

}

public int getBasicType() {

return basicType;

}

public void setBasicType(int basicType) {

this.basicType = basicType;

}

}

DataWork.java文件内容:

package wap_cctv_film;

import java.sql.*;

import java.util.*;

public class DataWork {

private Connection conn;

public DataWork() {

this.conn = DataConnection.getDataConnection();

}

public BasicInfo getBasciInfo(String sql) throws Exception {

Statement stmt = conn.createStatement();

ResultSet rst = stmt.executeQuery(sql);

BasicInfo basicinfo = null;

while (rst.next()) {

basicinfo = new BasicInfo();

basicinfo.setBasicID(rst.getInt("basicID"));

basicinfo.setBasicType(rst.getInt("basicType"));

basicinfo.setBasicName(rst.getString("basicName"));

basicinfo.setBasicText(rst.getString("basicText"));

basicinfo.setBasicHits(rst.getInt("basicHits"));

basicinfo.setBasicIsShow(rst.getInt("basicIsShow"));

}

return basicinfo;

}

OrderInfoServlet.java文件内容:

package wap_cctv_film;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class OrderInfoServlet

extends HttpServlet {

private static final String CONTENT_TYPE = "text/vnd.wap.wml;charset=gb2312";

//Initialize global variables

public void init() throws ServletException {

}

//Process the HTTP Get request

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

int articleID;

if (request.getParameter("articleID") == null) {

articleID = 0;

}

else {

articleID = Integer.parseInt(request.getParameter("articleID"));

}

try {

DataWork getInfo = new DataWork();

BasicInfo basicinfo = getInfo.getBasciInfo(

"select * from BasicInfo where basicID=" + articleID);

out.println("<?xml version='1.0'?>");

out.println("<wml>");

out.println("<head>");

out.println("<meta http-equiv='Cache-Control' content='max-age=0' />");

out.println("</head>");

out.println("<card id='MAIN' title='梦幻掌上剧场--剧情简介--" +

basicinfo.getBasicName() +

"'>");

out.println("<p>");

out.println("《" + basicinfo.getBasicName() + "》剧情简介<br />");

int pagewords = 199; //每一页显示的字数

int pageCount; //所有的页数

int pages; //当前页

//若pages为空,则付值1;否则等于

if (request.getParameter("pages") == null) {

pages = 1;

}

else {

pages = Integer.parseInt(request.getParameter("pages"));

}

int textLen = basicinfo.getBasicText().length();//文本长度

//求出文本分段得出的总页数;

if (textLen % pagewords == 0) {

pageCount = (int) Math.ceil(textLen / pagewords);

}

else {

pageCount = (int) Math.ceil(textLen / pagewords) + 1;

}

//当前页面总文本剩余的长度,得出两种条件选择;

int lastTextLen = textLen - pages * pagewords;

//一种是剩余长度大于一页文字的长度,即大于199个字;

if (lastTextLen > pagewords) {

out.println(basicinfo.getBasicText().substring( (pages - 1) *

pagewords,

pages * pagewords) + "(第" + pages + "/" + pageCount + "页)<br />");

}

//一种是剩余文本长度小于一页文字的长度;即是最后一页;

else if (lastTextLen < pagewords) {

out.println(basicinfo.getBasicText().substring( (pages - 1) *

pagewords, textLen - 1) + "(第" + pages + "/" + pageCount +

"页)<br />");

}

//分页

int lastPage = pages - 1; //上一页

int nextPage = pages + 1; //下一页

//总页数大于一则显示分页

if (pageCount > 1) {

if (pages < pageCount) {

out.println("<a href='?articleID=" + articleID +

"&amp;pages=" + nextPage + "'>下一页</a><br />");

}

if (pages != 1) {

out.println("<a href='?articleID=" + articleID +

"&amp;pages=" + lastPage + "'>上一页</a><br />");

}

}

out.println("<a href='indexservlet'>[返回首页]</a><br />");

out.println("<a href='http://wap.monternet.com'>[梦网首页]</a><br />");

out.println("</p>");

out.println("</card>");

out.println("</wml>");

}

catch (Exception ex) {

}

}

//Clean up resources

public void destroy() {

}

}

web.xml文件中新增:

<servlet>

<servlet-name>orderinfoservlet</servlet-name>

<servlet-class>wap_cctv_film.OrderInfoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>orderinfoservlet</servlet-name>

<url-pattern>/orderinfoservlet</url-pattern>

</servlet-mapping>

其他编译配置就不多说了,这里面有什么错误或者您有好的建议请mail:webmaster@aliang.org,谢谢!让我们共同学习!!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有