点击一个href。
调用Servlet <li ><a href="<%=request.getContextPath() %>/ProviderServlet">privilegeManagement</a></li>
此代码包含在direct.jsp中包含的navigation.jsp中。
然后它就是我的servlet。@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
ProviderDao pd = new ProviderDao();
List<ProviderArchives> list = pd.getArchives();
String str = "chenfeng";
req.setAttribute("list", list);
req.setAttribute("hu" , str);
getServletContext().getRequestDispatcher("/jsp/main.jsp").forward(req,resp);
}
然后它是我的main.jsp。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="com.chenfeng.javabean.ProviderArchives"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>main</title>
</head>
<body>
<%@ include file="navigation.jsp"%>
<div>
<%@ include file="management.jsp"%>
</div>
<div>
<%
String k = (String)request.getAttribute("hu");
out.println(k);
%>
<c:forEach items="${list}" var="item">
<tr>
<td>${item.provideID() }</td>
<td>${item.GID }</td>
<td>${item.Gname }</td>
<td>${item.PID }</td>
<td>${item.TEL }</td>
<td>${item.ADDR }</td>
<td>
<a href="QueryUserByIdServlet?id=${item.provideID }">Modify</a>
<a href="DeleteUserServlet?id=${item.provideID }">Delete</a>
</td>
</tr>
</c:forEach>
</div>
</body>
</html>
答案 0 :(得分:0)
实际上,这个代码是没有理由的。但是eclipse会遇到问题。在构建目录中删除类文件并重新构建之后,真正的问题日志输出。
答案 1 :(得分:-1)
很清楚。在转发attribute
时设置Request Object
Servlet Context Object
。
而不是:
getServletContext().getRequestDispatcher("/jsp/main.jsp").forward(req,resp);
写:
req.getRequestDispatcher("/jsp/main.jsp").forward(req,resp);