Smile Mr bird 2015-09-10 03:52 采纳率: 0%
浏览 5875
已结题

在servlet中怎么传集合显示在jsp页面

jsp页面:
<%@ page language="java" import="java.util.*" 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">















联系人列表

新增联系人
姓名 电话 邮箱 关系 操作
删除
删除
删除
删除



servlet页面:
package Servlet;

import impl.ContactsDaoImpl;
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 javax.servlet.http.HttpSession;

import dao.ContactsDao;
import entity.Contacts;

public class SelectAll 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 {
    //处理乱码
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");

    //得到httpsession对象
    HttpSession session=request.getSession();

    //得到数组的值
    String[] name=request.getParameterValues("name");
    String[] phone=request.getParameterValues("phone");
    String[] email=request.getParameterValues("email");
    String[] contact=request.getParameterValues("contact");
    //调用接口的实现方法; 
    ContactsDao contactsdao=new ContactsDaoImpl();
    List<Contacts> list=contactsdao.selectAll();
    if (list!=null) {
        session.setAttribute("name", name);
        response.sendRedirect("show.jsp");
    }
}

}

接口的实现方法类页面:
package impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.ContactsDao;
import entity.Contacts;

public class ContactsDaoImpl extends BaseDao implements ContactsDao {
//查询所有
public List selectAll() {
List list=new ArrayList();
String sql="select * from Contacts";
Contacts contacts=null;
//调用basedao中的公共方法;
rs=this.executeQuery(sql, null);
try {
while (rs.next()) {
contacts=new Contacts(rs.getString("name"), rs.getString("phone"), rs.getString("email"), rs.getString("contact"));
list.add(contacts);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(con, pstm, rs);
}
return list;
}

}
图片说明

  • 写回答

7条回答 默认 最新

  • baidu_28019731 2015-09-10 03:57
    关注

    你试试我的
    Arraylist<> a =.....;//你的处理结果
    PrintWriter pw = response.getWriter();
    pw.println(a);// 前台servlet请求之后 无返回值就这样。 有返回值可以用ajax

        希望对你有用
    
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条