消息关闭
    暂无新消息!
显示页面:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>show</title>
</head>
<body>
<form action="" method="post">
<table border="1">
<tr>
<th>编号</th><th>名字</th><th>年龄</th><th>电话</th><th>地址</th><th>操作</th>
</tr>
<c:forEach var="a" items="${list}">
<tr>
<td>${a.id}</td> 
<td>${a.name}</td>
<td>${a.age}</td>
<td>${a.tel}</td>
<td>${a.address}</td>
<td><a href="">修改</a>
<a href="">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
servlet
package comcn.query;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowServlet  extends HttpServlet{
public void destory(){
super.destroy();
}
public void doGet(HttpServletRequest request,HttpServletResponse response){

response.setContentType("text/html");
try {
PrintWriter out=response.getWriter();
this.doPost(request, response);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
List<Uservo>list=new ArrayList<Uservo>();
Query query=new Query();
list=query.showUser();
request.setAttribute("list", list);
request.getRequestDispatcher("/NewFile.jsp").forward(request,response);

out.flush();
out.close();
}
public void init()throws ServletException{

}
链接数据库
package comcn.query;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Query {
public List<Uservo>showUser(){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
    List<Uservo> list=new ArrayList<Uservo>();
    
    conn=jdbc.getConnection();
    try {
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from users");

while(rs.next()){
System.out.println("00000");
Uservo uservo=new Uservo();
uservo.setId(rs.getInt("id"));
System.out.println(rs.getInt("id"));
uservo.setName(rs.getString("name"));
uservo.setAge(rs.getInt("age"));
    uservo.setTel(rs.getString("tel"));
    uservo.setAdress(rs.getString("address"));   
    System.out.println(rs.getString("address"));
    list.add(uservo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
 
return list;

}
}

XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>aa</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>ShowServlet</servlet-name>
  <servlet-class>comcn.query.ShowServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>ShowServlet</servlet-name>
  <url-pattern>/ShowServlet</url-pattern>
  </servlet-mapping>
  
</web-app>

检测过后台能读到值,但JSP页面无法显示。求指导。

1个回答

︿ 2
你好像发错板块了,还有你这个好像要提交之后才有数据,默认是不运行doPost的