JSP 页面跳转中的参数传递

时间:2022-03-29 09:36:47

1. 从一个 JSP 页面跳转到另一个 JSP 页面时的参数传递

1)使用 request 内置对象获取客户端提交的信息

2)使用 session 对象获取客户端提交的信息

3)利用隐藏域传递数据

4)通过超链接传递数据

2. 从JSP 页面传递参数给 Servlet

1)使用 request 对象接受参数

2)使用 session 对象,url,form表单传递参数

3. 从 Servlet 向 JSP 页面中传递参数

从 Servlet 到 JSP 页面传递参数,可以使用 URL,request 对象,session 对象

4. 从一个 Servlet 向另一个 Servlet 传递参数

1)通过超链接和 form 表单传递数据

2)使用 setAttribute() 和 getAttribute() 方法传递参数

https://blog.csdn.net/Qiuzhongweiwei/article/details/76037324

5. 将后台数据库中查询的结果传递到JSP页面中

查询数据库后获得的结果是 ResultSet

递归 ResultSet 获得相应数据,然后将这些数据再依次存入列表中,最后将存放数据的列表以属性的方式封装到 request 或者 session 中,这样在 JSP 页面中就可以读取属性

...
try {
if (resultSet == null) {
response.sendRedirect("/pathogen/queryPathogen");
return;
}
while (resultSet.next()) {
QueryResult queryResult = new QueryResult();
queryResult.setTaxid(resultSet.getString("taxid"));
queryResult.setOrganismName(resultSet.getString("organism_name")); // 物种名称
queryResult.setGramStrain(resultSet.getString("gram_strain")); // 革兰氏分型
queryResult.setSuperkingdom(resultSet.getString("superkingdom")); // 分类地位
queryResult.setDisease(resultSet.getString("disease")); // 疾病名称
queryResult.setIsolationSource(resultSet.getString("isolation_source")); // 样本来源
queryResult.setEvidenceSupport(resultSet.getString("evidence_support")); // 收录来源
queryResultList.add(queryResult); // 将每条记录追加到列表中
};
request.setAttribute("queryPathogenResultList", queryResultList); // 将查询结果封装到 request 范围的属性中
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/JSP/queryPathogen.jsp");
requestDispatcher.forward(request, response); // 将 request 和 response 转发到相应的 JSP页面中
} catch (SQLException e) {
e.printStackTrace();
}

在 JSP 页面中读取封装到 request 中的数据

<%
if (request.getAttribute("queryPathogenResultList") != null ) {
%>
<div>
<h2>病原数据库检索结果</h2>
<%
if (((List<Object>)request.getAttribute("queryPathogenResultList")).isEmpty()) {
%>
未查询到结果!
<%
} else {
%>
<table border=1 >
<tr>
<c:if test="${requestScope.taxid == 'taxid' }" >
<th>taxId</th>
</c:if>
<c:if test="${requestScope.organismName == 'organismName' }" >
<th>物种名称</th>
</c:if>
<c:if test="${requestScope.gramStrain == 'gramStrain' }" >
<th>格兰氏分型</th>
</c:if>
<c:if test="${requestScope.rank == 'rank' }" >
<th>分类地位</th>
</c:if>
<c:if test="${requestScope.disease == 'disease' }" >
<th>疾病信息</th>
</c:if>
<c:if test="${requestScope.isolationSource == 'isolationSource' }" >
<th>样本来源</th>
</c:if>
<c:if test="${requestScope.dataSource == 'dataSource' }" >
<th>收录来源</th>
</c:if>
<c:if test="${requestScope.gcContent == 'gcContent' }" >
<th>GC含量</th>
</c:if>
<c:if test="${requestScope.refGenome == 'refGenome' }" >
<th>参考基因组</th>
</c:if>
</tr>
<c:forEach items="${queryPathogenResultList}" var="queryPathogenResult" >
<tr>
<c:if test="${requestScope.taxid == 'taxid' }" >
<td>${queryPathogenResult.taxid }</td>
</c:if>
<c:if test="${requestScope.organismName == 'organismName' }" >
<td>${queryPathogenResult.organismName }</td>
</c:if>
<c:if test="${requestScope.gramStrain == 'gramStrain' }" >
<td>${queryPathogenResult.gramStrain }</td>
</c:if>
<c:if test="${requestScope.rank == 'rank' }" >
<td>${queryPathogenResult.superkingdom }</td>
</c:if>
<c:if test="${requestScope.disease == 'disease'}" >
<td>${queryPathogenResult.disease }</td>
</c:if>
<c:if test="${requestScope.isolationSource == 'isolationSource' }" >
<td>${queryPathogenResult.isolationSource }</td>
</c:if>
<c:if test="${requestScope.dataSource == 'dataSource' }" >
<td>${queryPathogenResult.evidenceSupport }</td>
</c:if>
<c:if test="${requestScope.gcContent == 'gcContent' }" >
<td></td>
</c:if>
<c:if test="${requestScope.refGenome == 'refGenome' }" >
<td></td>
</c:if>
</tr>
</c:forEach>
</table>
<%
}
%>
</div>
<%
}
%>