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

时间:2022-07-14 06:01:45

方法一;通过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;
}