servlet部分:
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.User;
import dao.UserDao;
public class UserServlet extends HttpServlet {
UserDao dao;
public UserServlet() {
// TODO Auto-generated constructor stub
dao = new UserDao();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
String cmd = request.getParameter("cmd");
if(cmd==null){
this.doFindByPage(request, response);
}
else if(cmd.equals("add")){
this.doAdd(request, response);
}else if(cmd.equals("delete")){
this.doAdd(request, response);
}
}
//增加
public void doAdd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tname = request.getParameter("tname");//获取账号
String tpwd = request.getParameter("tpwd");//获取密码
//添加到数据库
User u = new User();
u.setTname(tname);
u.setTpwd(tpwd);
dao.add(u);
}
// 修改
public void doUpdate(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 修改保存
public void doUpdateSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 删除
public void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 查询
public void doFind(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dao = new UserDao();
String sql = "select * from tuser ";
User u = (User)dao.findBySql(sql);
System.out.println(u.getTid());
System.out.println(u.getTname());
}
// 分页查询
public void doFindByPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//查数据
List data= dao.find(1, 2);
request.setAttribute("list", data);//将数据放到request
request.getRequestDispatcher("show.jsp").forward(request, response);//转发到显示界面
}
}
jsp部分:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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 'show.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>
<table width="80%">
<tr>
<td>编号</td>
<td>账号</td>
<td>密码</td>
</tr>
<c:forEach items="${list}" var="d">
<tr>
<td>${d.tid}</td>
<td>${d.tname}</td>
<td>${d.tpwd}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
帮忙看看 获取不了这个:
<c:forEach items="${list}" var="d">
<tr>
<td>${d.tid}</td>
<td>${d.tname}</td>
<td>${d.tpwd}</td>
</tr>
</c:forEach>
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.User;
import dao.UserDao;
public class UserServlet extends HttpServlet {
UserDao dao;
public UserServlet() {
// TODO Auto-generated constructor stub
dao = new UserDao();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
String cmd = request.getParameter("cmd");
if(cmd==null){
this.doFindByPage(request, response);
}
else if(cmd.equals("add")){
this.doAdd(request, response);
}else if(cmd.equals("delete")){
this.doAdd(request, response);
}
}
//增加
public void doAdd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tname = request.getParameter("tname");//获取账号
String tpwd = request.getParameter("tpwd");//获取密码
//添加到数据库
User u = new User();
u.setTname(tname);
u.setTpwd(tpwd);
dao.add(u);
}
// 修改
public void doUpdate(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 修改保存
public void doUpdateSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 删除
public void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// 查询
public void doFind(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dao = new UserDao();
String sql = "select * from tuser ";
User u = (User)dao.findBySql(sql);
System.out.println(u.getTid());
System.out.println(u.getTname());
}
// 分页查询
public void doFindByPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//查数据
List data= dao.find(1, 2);
request.setAttribute("list", data);//将数据放到request
request.getRequestDispatcher("show.jsp").forward(request, response);//转发到显示界面
}
}
jsp部分:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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 'show.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>
<table width="80%">
<tr>
<td>编号</td>
<td>账号</td>
<td>密码</td>
</tr>
<c:forEach items="${list}" var="d">
<tr>
<td>${d.tid}</td>
<td>${d.tname}</td>
<td>${d.tpwd}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
帮忙看看 获取不了这个:
<c:forEach items="${list}" var="d">
<tr>
<td>${d.tid}</td>
<td>${d.tname}</td>
<td>${d.tpwd}</td>
</tr>
</c:forEach>