jsp指令项目中的运用


# **jsp指令项目中的运用**
此处是查询
jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%--导入java.util.*所有的包--%>
    <%@ page import="java.util.*" %>
    <%--导入Computer--%>
    <%@ page  import="computeServlet.Computer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="addCom.jsp">新增</a>
<form>
	<table border="1" cellpadding="0" cellspacing="0" style="font-style: center">
		<tr>
			<td>
			<input type="button"  value="全选" onclick="selAll()"/>
			<input type="button"  value="全不选" onclick="selNoAll()"/>
			</td>
			<td>品牌</td>
			<td>型号</td>
			<td>出厂日期</td>
			<td>类型</td>
			<td>适用人群</td>		
			<td>计算机颜色</td>
			<td>产品说明</td>
				
		</tr>
<%
//通过request.getAttribute获取信息
//comListJsp与servlet中的setAttribute中返回的保持一致
List<Computer> com = (List<Computer>)request.getAttribute("comListJsp");
//通过for循环查询展示所有信息
for(Computer c:com){
%>
		<tr>
		<td><input type="checkbox" name="check" value="<%=c.getcId() %>"/></td>
		<td><%=c.getcBrand() %></td>
		<td><%=c.getcProductModel()%></td>
		<td><%=c.getcMadeDate() %></td>			
		<td><%=c.getcComType() %></td>
		<td><%=c.getcApplyToPeople()%></td>
		<td><%=c.getcColor()%></td>
		<td><%=c.getcComExplain()%></td>
		 <td><input type="button" value="删除" onclick="delStu(<%=c.getcId() %>)"/></td>
		<td><input type="button" value="修改" onclick="toUpdateStu(<%=c.getcId() %>)"/></td>
	
	</tr>
<%
}
%>
</table>
</form>
</body>


**servlet**
public class ComListServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		//设置字符编码
		request.setCharacterEncoding("utf-8");
		ComputeDao cd = new ComputeDao();
		
		List<Computer> comList = cd.comList();
		//把值放到request中,这样在前台获取到的值 
		//stuListJsp页面获取值用它来获取
		//stuList 放值的list集合
		request.setAttribute("comListJsp", comList);
		request.getRequestDispatcher("comList.jsp").forward(request, resp);		
	}
}

猜你喜欢

转载自blog.csdn.net/sunrj_niu/article/details/106127383