我在 servlet 中遇到了一些麻烦。我从 Apache Tomcat 运行它。所以我在启动时运行它,我有一个控制台,一般来说一切都是标准的,但这里是代码:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
public class servletss extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
File file = new File("C:\\prov");
String list[] = file.list();
out.println("<html>");
out.println("<head>");
out.println("<title>testservlets</title>");
out.println("</head>");
out.println("<body>");
/*
* начало вывода
*/
ArrayList<String> sizel = new ArrayList<String>();
sizel = pagef();
int sizelist;
sizelist = sizel.size();
int pageint = 1;// номер страницы
ArrayList<String> sizetris = new ArrayList<String>();
int startlist = (pageint - 1) * 30;
int endlist = (sizel.size() >= (startlist + 30)) ? (startlist + 30): sizel.size();
sizetris.addAll(sizel.subList(startlist, endlist));
double startdouble = 10.0;// конец показа страниц
int endint = (int) (pageint * 10);// конечное число пользователя
double allpage = sizelist / 3;// число страниц всего
double page = allpage / startdouble;// деление страниц
double lsc = Math.ceil(page);
String separator = System.getProperty("line.separator");
if (endint > lsc * 10) {
out.println("страниц не найдено");
} else {
out.println("<div class=\"pagen\">" + pageint + "</div>");
String str = paginate(sizetris, endint * 3);
out.println("<div class=\"bloctext\">" + str + separator + "</div>");
out.println("<div class=\"pagen\">" + pageint + "</div>");
}
for (int i = 0; i < list.length; i++) {
out.println("<a href=\"http://localhost:8080/test/servletss?i=" + i + "\" str=\"" + i + "\">" + i + "</a>");
}
out.println("</body>");
out.println("</html>");
}
static <T> String paginate(List<T> pages, int pagesPerOut) {
for (int i = 0; i < pages.size(); i += pagesPerOut) {
System.out.println(paginate(pages, i, pagesPerOut));
}
return "";
}
static <T> List<T> paginate(List<T> pages, int start, int count) {
int end = start + count > pages.size() ? pages.size() : start + count;
return pages.subList(start, end);
}
/*
* метод для возвращения массива страниц
*/
static ArrayList<String> pagef() {
ArrayList<String> page = new ArrayList<String>();
BufferedReader br = null;
String s = null;
File file = new File("C:\\prov");
String list[] = file.list();
try {
for (int i = 0; i < list.length; i++) {
File fileread = new File("C:\\prov\\" + list[i]);
br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileread), "UTF-8"));
while ((s = br.readLine()) != null) {
page.add(s + "\n");
}
br.close();
}
} catch (Exception e) {
System.out.println(e);
}
return page;
}
}
我开始了它,现在这部分代码:
out.println("<div class=\"bloctext\">" + str + separator + "</div>");
她必须缝 30 针,但是当我运行代码时,我什么都看不到,但是看着控制台我看到了我的 30 针))在控制台 Tomcat))为什么那里的工作结果,而不是页面变成一个servlet?您的代码中还有其他问题,不要看,请告诉我如何解决这个问题?一切都正确,servlet在正确的文件夹中,这里是web.xml代码文件,以防万一:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>servletss</servlet-name>
<servlet-class>servletss</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletss</servlet-name>
<url-pattern>/servletss</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
提前感谢您的回答))
UPD
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
public class servletss extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out;
out = res.getWriter();
File file = new File("C:\\prov");
String list[] = file.list();
out.println("<html>");
out.println("<head>");
out.println("<title>testservlets</title>");
out.println("</head>");
out.println("<body>");
/*
* начало вывода
*/
ArrayList<String> sizel = new ArrayList<String>();
sizel = pagef();
int sizelist;
sizelist = sizel.size();
int pageint = 1;// номер страницы
ArrayList<String> sizetris = new ArrayList<String>();
int startlist = (pageint - 1) * 30;
int endlist = (sizel.size() >= (startlist + 30)) ? (startlist + 30): sizel.size();
sizetris.addAll(sizel.subList(startlist, endlist));
double startdouble = 10.0;// конец показа страниц
int endint = (int) (pageint * 10);// конечное число пользователя
double allpage = sizelist / 3;// число страниц всего
double page = allpage / startdouble;// деление страниц
double lsc = Math.ceil(page);
String separator = System.getProperty("line.separator");
if (endint > lsc * 10) {
out.println("страниц не найдено");
} else {
out.println("<div class=\"pagen\">" + pageint + "</div>");
String str = paginate(sizetris, endint * 3);
out.println("<div class=\"bloctext\">" + str + separator + "</div>");
out.println("<div class=\"pagen\">" + pageint + "</div>");
}
for (int i = 0; i < list.length; i++) {
out.println("<a href=\"http://localhost:8080/test/servletss?i=" + i + "\" str=\"" + i + "\">" + i + "</a>");
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
static <T> String paginate(List<T> pages, int pagesPerOut) {
for (int i = 0; i < pages.size(); i += pagesPerOut) {
System.out.println(paginate(pages, i, pagesPerOut));
}
return "";
}
static <T> List<T> paginate(List<T> pages, int start, int count) {
int end = start + count > pages.size() ? pages.size() : start + count;
return pages.subList(start, end);
}
/*
* метод для возвращения массива страниц
*/
static ArrayList<String> pagef() {
ArrayList<String> page = new ArrayList<String>();
BufferedReader br = null;
String s = null;
File file = new File("C:\\prov");
String list[] = file.list();
try {
for (int i = 0; i < list.length; i++) {
File fileread = new File("C:\\prov\\" + list[i]);
br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileread), "UTF-8"));
while ((s = br.readLine()) != null) {
page.add(s + "\n");
}
br.close();
}
} catch (Exception e) {
System.out.println(e);
}
return page;
}
}
更新 2
<html>
<head>
<title>testservlets</title>
</head>
<body>
<div class="pagen">1</div>
<div class="bloctext">
//There must be a conclusion, but it in a servlet
</div>
<div class="pagen">1</div>
<a href="http://localhost:8080/test/servletss?i=0" str="0">0</a>
<a href="http://localhost:8080/test/servletss?i=1" str="1">1</a>
<a href="http://localhost:8080/test/servletss?i=2" str="2">2</a>
<a href="http://localhost:8080/test/servletss?i=3" str="3">3</a>
<a href="http://localhost:8080/test/servletss?i=4" str="4">4</a>
<a href="http://localhost:8080/test/servletss?i=5" str="5">5</a>
<a href="http://localhost:8080/test/servletss?i=6" str="6">6</a>
<a href="http://localhost:8080/test/servletss?i=7" str="7">7</a>
<a href="http://localhost:8080/test/servletss?i=8" str="8">8</a>
<a href="http://localhost:8080/test/servletss?i=9" str="9">9</a>
<a href="http://localhost:8080/test/servletss?i=10" str="10">10</a>
<a href="http://localhost:8080/test/servletss?i=11" str="11">11</a>
<a href="http://localhost:8080/test/servletss?i=12" str="12">12</a>
<a href="http://localhost:8080/test/servletss?i=13" str="13">13</a>
<a href="http://localhost:8080/test/servletss?i=14" str="14">14</a>
<a href="http://localhost:8080/test/servletss?i=15" str="15">15</a>
<a href="http://localhost:8080/test/servletss?i=16" str="16">16</a>
<a href="http://localhost:8080/test/servletss?i=17" str="17">17</a>
<a href="http://localhost:8080/test/servletss?i=18" str="18">18</a>
<a href="http://localhost:8080/test/servletss?i=19" str="19">19</a>
<a href="http://localhost:8080/test/servletss?i=20" str="20">20</a>
<a href="http://localhost:8080/test/servletss?i=21" str="21">21</a>
<a href="http://localhost:8080/test/servletss?i=22" str="22">22</a>
<a href="http://localhost:8080/test/servletss?i=23" str="23">23</a>
<a href="http://localhost:8080/test/servletss?i=24" str="24">24</a>
</body>
</html>