manage.jsp
<%@ page language="java" import="java.util.*,org.lee.entity.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'manage.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>
进入管理页面
</h1>
<form action="search" method="post">
<table>
<tr>
<td>
请输入需要查询人的姓名
</td>
<td>
<input type="text" name="search" />
</td>
<td>
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
<table border="1">
<tr>
<td>
序号
</td>
<td>
用户名
</td>
<td>
密码
</td>
<td>
性别
</td>
<td>
年龄
</td>
<td>
地址
</td>
</tr>
<tr> <%
List<Loginentity> list = (List<Loginentity>) request.getSession()
.getAttribute("list"); for (Loginentity loginentity : list) {
%> <td><%=loginentity.lid%></td>
<td><%=loginentity.lname%></td>
<td><%=loginentity.lpassword%></td>
<td><%=loginentity.lsex%></td>
<td><%=loginentity.lage%></td>
<td><%=loginentity.laddress%></td> <%
}
%> </tr>
</table>
</body>
</html>servletpublic class Search extends HttpServlet { /**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
 * The doPost method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
String search = request.getParameter("search");
DBconnection dbconnection = new DBconnection();
Connection conn;
try {
conn = dbconnection.getDBconnection();
Statement st = conn.createStatement();
String sql = "select *from login where lname = '" + search + "'";
ResultSet rs = st.executeQuery(sql);


List list = new ArrayList();
HttpSession session = request.getSession();
if (rs.next()) {
Loginentity loginentity = new Loginentity(){};
loginentity.setLid(rs.getInt("lid"));
loginentity.setLname(rs.getString("lname"));
loginentity.setLpassword(rs.getString("lpassword"));
loginentity.setLsex(rs.getString("lsex"));
loginentity.setLage(rs.getInt("lage"));
loginentity.setLaddress(rs.getString("laddress"));
list.add(loginentity);
request.setAttribute("list",list);
getServletContext().getRequestDispatcher("/manage.jsp")
.forward(request, response);
} else {
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
 * Initialization of the servlet. <br>
 * 
 * @throws ServletException
 *             if an error occurs
 */
public void init() throws ServletException {
// Put your code here
}
}
结果每次打开页面的时候,总报错
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 81 in the jsp file: /manage.jsp
The field Loginentity.lid is not visible
78:  for (Loginentity loginentity : list) {
79:  %>
80: 
81:  <td><%=loginentity.lid%></td>
82:  <td><%=loginentity.lname%></td>
83:  <td><%=loginentity.lpassword%></td>
84:  <td><%=loginentity.lsex%></td>控制台显示:
2010-5-22 0:47:02 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 81 in the jsp file: /manage.jsp
The field Loginentity.lid is not visible
78:  for (Loginentity loginentity : list) {
79:  %>
80: 
81:  <td><%=loginentity.lid%></td>
82:  <td><%=loginentity.lname%></td>
83:  <td><%=loginentity.lpassword%></td>
84:  <td><%=loginentity.lsex%></td>

解决方案 »

  1.   

    <form action="search" method="post"><input type="text" name="search" />
    这两个名字别搞一样、、、
      

  2.   

    首先问楼主两个问题 1.Loginentity 这个类是否在页头的import 里面已经到进来
    2.Loginentity 类是否一个JavaBean ,纯包含set和get方法
      

  3.   

    Servlet(Search.class),import了吗?
      

  4.   


    这两个名字不能取一样的,
    <form action="search" method="post">
    应该写成<form action="/search.do?thisAction=xxx" method="post">
      

  5.   

    [List list = new ArrayList();
    HttpSession session = request.getSession();
    if (rs.next()) {
    Loginentity loginentity = new Loginentity(){};
    loginentity.setLid(rs.getInt("lid"));
    loginentity.setLname(rs.getString("lname"));
    loginentity.setLpassword(rs.getString("lpassword"));
    loginentity.setLsex(rs.getString("lsex"));
    loginentity.setLage(rs.getInt("lage"));
    loginentity.setLaddress(rs.getString("laddress"));
    list.add(loginentity);
    request.setAttribute("list",list);
    getServletContext().getRequestDispatcher("/manage.jsp")
    .forward(request, response);
    }]
    我不明白,你生成Loginentity loginentity = new Loginentity(){};   后面会什么加上‘{}’ ,这有什么用呢。没见过这样的写法。
    还有你没有外围的 循环,你的list   的size是1 or 0。
    还有你在jsp中 获取值的是用  request.getSession("list");而你在servlet中返回这些值 的是使用request.setAttribute("list",list);你之前已经定义了session 为什么不用它来设定呢?
    怎样怎么能获取数据啊 。