81,095
社区成员
发帖
与我相关
我的任务
分享
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ContactDao;
import dao.impl.ContactDaoImpl;
import entity.Contact;
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ContactDao dao = new ContactDaoImpl();
//获取联系人集合
List<Contact> list = dao.listAllContact();
request.setAttribute("contact", list);
//转发
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" import="java.util.*,entity.Contact" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN
">
<html>
<head>
<title>联系人列表(jsp版)</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
table td {
text-align: center;
}
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<center>
<h2>联系人列表</h2>
</center>
<table align="center" border="1" width="800px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>qq</th>
<th>Email</th>
<th>操作</th>
</tr>
<%
//接收list
List<Contact> list = (List<Contact>)request.getAttribute("contact");
//判断是否为空集合
if(list != null){
//遍历
for(Contact contact:list){
%>
<tr>
<td><%=contact.getId() %></td>
<td><%=contact.getName() %></td>
<td><%=contact.getGender() %></td>
<td><%=contact.getAge() %></td>
<td><%=contact.getQq() %></td>
<td><%=contact.getEmail() %></td>
<td><a href="">修改</a> <a href="">删除</a></td>
</tr>
<%
}
}
%>
<tr>
<td colspan="7"><a
href="">添加联系人</a></td>
</tr>
</table>
</body>
</html>
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ContactDao;
import dao.impl.ContactDaoImpl;
import entity.Contact;
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取联系人集合
ContactDao dao = new ContactDaoImpl();
List<Contact> list = dao.listAllContact();
//直接显示所有联系人
response.setContentType("text/html;charset=UTF-8");
String html = "";
html +="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
html +="<html>";
html +=" <head>";
html +=" <title>联系人列表(Servlet版)</title>";
html +=" ";
html +=" <meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>";
html +=" <meta http-equiv='description' content='this is my page'>";
html +=" <meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
html +=" ";
html += "<style type='text/css'>";
html += " table td{";
html += " /*文字居中*/";
html += " text-align:center;";
html += " }";
html += " ";
html += " /*合并表格的边框*/";
html += " table{";
html += " border-collapse:collapse;";
html += " }";
html += "</style>";
html +=" </head>";
html +=" ";
html +=" <body>";
html +=" <center><h2>联系人列表(Servlet版)</h2></center>";
html += " <table align='center' border='1' width='800px'>";
html += " <tr>";
html += " <th>编号</th>";
html += " <th>姓名</th>";
html += " <th>性别</th>";
html += " <th>年龄</th>";
html += " <th>qq</th>";
html += " <th>Email</th>";
html += " <th>操作</th>";
html += " </tr>";
//遍历
if(list != null){
for (Contact contact : list) {
html +=" <tr>";
html +=" <td>"+contact.getId()+"</td>";
html +=" <td>"+contact.getName()+"</td>";
html +=" <td>"+contact.getGender()+"</td>";
html +=" <td>"+contact.getAge()+"</td>";
html +=" <td>"+contact.getQq()+"</td>";
html +=" <td>"+contact.getEmail()+"</td>";
html +=" <td>";
html +=" <a href='"+request.getContextPath()+"/UpdateContact?id="+contact.getId()+"'>修改</a> ";
html +=" <a href='"+request.getContextPath()+"/DeleteContactServlet?id="+contact.getId()+"'>删除</a>";
html +=" </td>";
html +=" </tr>";
}
}
html +=" <tr>";
html +=" <td colspan='7' ><a href='"+request.getContextPath()+"/AddContact'>添加联系人</a></td>";
html +=" </tr>";
html +=" </table>";
html +=" </body>";
html +="</html>";
response.getWriter().write(html);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package ServletDemo;
import java.io.IOException;
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;
import entity.Student;
public class ServletDispatch extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Student student1 = new Student("James", 32, "male");
Student student2 = new Student("Kobe", 37, "male");
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
request.setAttribute("students", list);
// 转发
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
<%@ page language="java" import="java.util.*,entity.Student" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN
">
<html>
<head>
<title>联系人列表(jsp版)</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
table td {
text-align: center;
}
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<center>
<h2>联系人列表</h2>
</center>
<table align="center" border="1" width="800px">
<tr>
<th>age</th>
<th>name</th>
<th>sex</th>
<th>operation</th>
</tr>
<%
//接收list
List<Student> list = (List<Student>) request.getAttribute("students");
//判断是否为空集合
if(list != null){
//遍历
for(Student student:list){
%>
<tr>
<td><%=student.getAge() %></td>
<td><%=student.getName() %></td>
<td><%=student.getSex() %></td>
<td><a href="">修改</a> <a href="">删除</a></td>
</tr>
<%
}
}
%>
<tr>
<td colspan="4"><a
href="">添加联系人</a></td>
</tr>
</table>
</body>
</html>
public List<Contact> listAllContact() {
// 读取xml文件
Document doc = null;
doc = XMLUtils.readXML();
List<Element> list = doc.selectNodes("//contact");
List<Contact> contactList = new ArrayList<Contact>();
for (Element element : list) {
Contact contact = new Contact();
contact.setId(element.attributeValue("id"));
contact.setName(element.elementText("name"));
contact.setGender(element.elementText("gender"));
contact.setAge(Integer.valueOf(element.elementText("age")));
contact.setQq(element.elementText("qq"));
contact.setEmail(element.elementText("email"));
contactList.add(contact);
}
return contactList;
}
我这个方法可以正常读取XML返回一个list,我用Servlet页面输出是没有问题的啊。只是转发到JSP就变成空了????