方法一;通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice

servlet中

String noticeId=request.getParameter("noticeId");
Notice displayEditnotice=publicnoticeservice.displayEditnotice(Integer.valueOf(noticeId));
request.setAttribute("list_displayEditnotice", displayEditnotice);
System.out.println("displayEditnotice="+displayEditnotice);
request.getRequestDispatcher("Editnotice.jsp").forward(request, response);

Editnotice.jsp页面

<form....>

<table>

<tr>
<td>标题:</td>
<td><input type="text" id="title" name="title" value="${list_displayEditnotice.getTitle()}"></td>-
</tr>
<tr>
<td>内容:</td>
<td><textarea cols="50" rows="10" name="context" style="border:#FF0000 1px solid;overflow:visible;">${list_displayEditnotice.getContext()}</textarea></td>
</tr>
<tr>
<td><input type="submit" value="保存公告"></td>
</tr>
</table>
</form>

dao中接口的实现方法

public Notice displayEditnotice(int noticeId) {
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice where noticeId=?";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, noticeId);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
super.closeAll(conn, pstmt, stmt, rs);
}

return notice;
}

方法二:将我数据库中表的所有数据显示出来,则将每一行的值封装在List集合中,在jsp页面用<c:forEach><forEach>迭代显示出来

注意要加标签库:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

servlet中

List<Notice> displaynotice=publicnoticeservice.displaypublicnotice();
request.setAttribute("list_displaynotice",displaynotice);
request.getRequestDispatcher("displaypublicnotice.jsp").forward(request, response);

displaypublicnotice.jsp

<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序号</td>
<td style="width:170px;text-align: center">标题</td>
<td style="width:400px;text-align: center">内容</td>
<td style="width:70px;text-align: center">发布人</td>
<td style="width:200px;text-align: center">发布时间</td>
<td style="width:120px;text-align: center">操作</td>
</tr>

<c:forEach items="${list_displaynotice}" var="notice" varStatus="i">
<tr style="background:#7FFFD4">
<td style="width:50px;text-align: center">${i.count} </td>
<td style="width:100px;text-align: center">${notice.title}</td>
<td style="text-align: center"><font style="font-size:12px;">${notice.context}</font></td>
<td style="text-align: center">${notice.publicer}</td>
<td style="width:100px;text-align: center">${notice.writeDate}</td>
<td style="text-align: center"><a href="PublicNoticeServlet?method=deletenotice&noticeId=${notice.noticeId}" target="middle" style="TEXT-DECORATION:none">删除</a>
|<a href="PublicNoticeServlet?method=editnotice&noticeId=${notice.noticeId}"
target="middle" style="TEXT-DECORATION:none">编辑</a>
</td>
</tr>
</c:forEach>
</table>

dao中接口的实现方法

private Connection conn=null;
private PreparedStatement pstmt=null;
private Statement stmt=null;
private ResultSet rs=null;

public List<Notice> displaypublicnotice() {
List<Notice> list=new ArrayList<Notice>();
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
list.add(notice);
}
} catch (SQLException e) {

e.printStackTrace();
}finally{ // 7。关闭连接
super.closeAll(conn, pstmt, stmt, rs);
}

return list;
}

如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值的更多相关文章

  1. jsp页面 如何通过el表达式获取request属性值

    1. 我在一个超连接后加个参数如:      http://localhost:8080/test/testjstl.jsp?pid=001    此时在jsp页面中,获取jsp传过来的pid的参数值 ...

  2. 后端model传入前端JSP页面中的值判断后再取值

    所遇到的问题后端model传入前端JSP页面中的值通过foreach循环内要满足条件才能取值给Div中,我们知道jsp页面中可以直接用EL表达式取值,格式就是${"model中传来的数据&q ...

  3. Servlet转发到JSP页面的路径问题

    一.现象与概念 1. 问题 在Servlet转发到JSP页面时,此时浏览器地址栏上显示的是Servlet的路径,而若JSP页面的超链接还是相对于该JSP页面的地址且该Servlet和该JSP页面不在同 ...

  4. JSP中的内置标记(JSP动作)和 EL表达式

    一.JSP的内置标记(JSP动作) (一)JSP的内置标记都是以<jsp: 开始标记的 一般会用到<jsp:useBean/>,<jsp:setProperty/>,&l ...

  5. uni-app中当uni.navigateTo传的参数为object时,通过传递的不同参数,在显示单页面内通过v-if判断显示出对应的内容(可实现多页面效果)

    通过uni-app中当uni.navigateTo传的参数为object时,通过传递的不同参数,在显示单页面内通过v-if判断显示出对应的内容(可实现多页面效果) 起始页跳转到对应页面,并传递参数(o ...

  6. 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)

    我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...

  7. struts2 request内幕 为什么在struts2用EL表达式可以取值

    不知道大家有没有想过这样一个问题:为什么在action中的实例变量,没有使用request.setAttribute()方法将值添加到request范围内,却能在jsp中用EL表达式取出? 众所周知, ...

  8. 小峰servlet/jsp(4)EL表达式

    一.EL表达式内置对象: 二.EL表达式访问4种范围属性: 寻找值的顺序: page-->request-->session-->application; 三.EL表达式接收请求参数 ...

  9. EL表达式获取属性值的原理

    EL表达式获取对象属性的原理是这样的:以表达式${user.name}为例EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找到与 ...

随机推荐

  1. JAVA匿名内部类(Anonymous Classes)

    1.前言 匿名内部类在我们JAVA程序员的日常工作中经常要用到,但是很多时候也只是照本宣科地用,虽然也在用,但往往忽略了以下几点:为什么能这么用?匿名内部类的语法是怎样的?有哪些限制?因此,最近,我在 ...

  2. 解决Google Play审核中的WebViewClient.onReceivedSslError问题

    Google Play应用市场提交应用审核,出现因WebViewClient.onReceivedSslError问题导致拒绝通过. Google Paly给出的详情地址:support.google ...

  3. vscode restclient 插件

    使用步骤: 1.vscode 安装restclient 扩展 2.创建  .http 或 .rest 文件 ,编写相应内容 同一个文件内 可以通过 ### 分割多个请求 可以通过 @hostname ...

  4. [转]为什么复制构造函数的参数需要加const和引用

    [转]为什么复制构造函数的参数需要加const和引用 一.引言 1.0在解答这个问题之前,我们先跑个小程序,看下调用关系. #include <iostream> using namesp ...

  5. 写个重新加载 ocelot 配置的接口

    写个重新加载 ocelot 配置的接口 Intro 我们想把 ocelot 的配置放在自己的存储中,放在 Redis 或者数据库中,当修改了 Ocelot 的配置之后希望即时生效,又不想在网关这边定时 ...

  6. SSIS-导入Excel文件时记录行号

    SSIS导入Excel时记录行号 1. "Excel源"后添加"脚本组件" 2. "脚本组件"中新增输出列,命名为"RowNumb ...

  7. Windows Server 2012 R2 安装密钥(只适用安装,不支持激活)

    标准版 = NB4WH-BBBYV-3MPPC-9RCMV-46XCB 数据中心版 = BH9T4-4N7CW-67J3M-64J36-WW98Y

  8. ipv6地址配置实验(GNS3/ENSP)

    实验拓扑: IPV6地址配置如图所示, 配置ipv6指令(以R2为例,R1类似): int e1/2 R2(config-if)#ipv6 address 2001:db08:acad:1::2/64 ...

  9. 巧妙地使用typora编辑有道云笔记

    设置方法 找到有道云笔记本地保存路径: 找到有道云笔记的保存的路径:启动有道云 - 设置 - 有道云笔记(本地文件) - 打开文件夹 使用typora打开有道云笔记目录: typora 菜单栏 - O ...

  10. AJAX快速上手和基本核心

    一.快速上手AJAX 使用ajax的过程可以类比平常我们访问网页过程 1.创建一个XMLHttpRequest类型的对象------相当于打开了浏览器 var xhr = new XMLHttpReq ...