这是做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 +
"&pages=" + nextPage + "'>下一页</a><br />");
}
if (pages != 1) {
out.println("<a href='?articleID=" + articleID +
"&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,谢谢!让我们共同学习!!