jsp中如何获取servlet的值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp中如何获取servlet的值?相关的知识,希望对你有一定的参考价值。
参考技术A servlet中你的这行代码的意思分别是:request.getSession()代表的是获取session对象,setAttribute("tf",
tf)表示像session中存入名为"tf"的对象tf
那么你在JSP页面中可以直接调用JSP内置对象-session来获取tf的值,具体代码为:
<%=session.getAttribute("tf")
%>就可以了
如果你是使用JSTL标签标签,那么具体代码为:$sessionScope.tf
如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值
方法一;通过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¬iceId=${notice.noticeId}" target="middle" style="TEXT-DECORATION:none">删除</a>
|<a href="PublicNoticeServlet?method=editnotice¬iceId=${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;
}
以上是关于jsp中如何获取servlet的值?的主要内容,如果未能解决你的问题,请参考以下文章