package com.www.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 com.www.service.AnimalService;
public class AnimalServlet extends HttpServlet {
AnimalService service = new AnimalService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String m = request.getParameter("m");
if(m.equals("list")){
list(request,response);
}else if(m.equals("show")){
show(request,response);
}
}
private void show(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String id = request.getParameter("id");
try {
List list = service.show(id);
request.setAttribute("list", list);
request.getRequestDispatcher("upd.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
List list = service.list();
request.setAttribute("list", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ 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 'upd.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>
<c:forEach var="li" items="${list }">
id:<input type="hidden" value="${li.id }"><br>
name:<input type="text" value="${li.name }"><br>
age:<input type="text" value="${li.age }"><br>
color:<input type="text" value="${li.color }"><br>
</c:forEach>
<input type="button" value="修改">
</body>
</html>
一点浩然气,千里快哉风