可以在jsp中对一个变量赋予不同的值来实现不同的功能。
如下面这个例子。利用表单标签给op赋予不同的值findOne和findAll来实现根据id查询或者查所有的用户。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
UserServlet.java
package cn.itcast.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.bean.UserBean;
import cn.itcast.biz.UserBiz;
import cn.itcast.uitil.ServletUtil;
/**
* Servlet implementation class UserServlet
*/
@WebServlet("/user.s")
public class UserServlet extends BasicServlet {
private UserBiz ubiz = new UserBiz();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String op = req.getParameter("op");
if("findOne".equals(op)){
try {
findOne(req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.getRequestDispatcher("/WEB-INF/error.jsp").forward(req, resp);
}
}else if("findAll".equals(op)){
try {
findAll(req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.getRequestDispatcher("/WEB-INF/error.jsp").forward(req, resp);
}
}else if("delete".equals(op)){
try {
delete(req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.getRequestDispatcher("/WEB-INF/error.jsp").forward(req, resp);
}
}
}
/**
* 根据id值来删除指定用户
* @param req
* @param resp
* @throws Exception
* @throws Exception
*/
private void delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
// TODO Auto-generated method stub
System.out.println("delete");
String id = req.getParameter("id");
System.out.println(id);
ubiz.delete(id);
findAll(req,resp);
}
/**
* 查所有的用户
* @param req
* @param resp
* @throws Exception
* @throws Exception
*/
private void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception, Exception {
// TODO Auto-generated method stub
System.out.println("findAll");
List list = ubiz.selectAll();
req.setAttribute("list", list);
req.getRequestDispatcher("user1.jsp").forward(req, resp);
}
/**
* 根据id查询指定用户
* @param req
* @param resp
* @throws ServletException
* @throws Exception
*/
private void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, Exception {
// TODO Auto-generated method stub
System.out.println("findOne");
Integer id = ServletUtil.getParamter(req, Integer.class, "id");
UserBean ub = ubiz.selectByid(id);
req.setAttribute("user", ub);
req.getRequestDispatcher("user.jsp").forward(req, resp);
}
}
在jsp中根据不同的后缀来区分即将跳转到的Servlet。在Servlet中获取ServletPath。如下
url: http://localhost:8080/MVC1/findOne.do?id=3
ServletPath:就为findOne
接下来就根据ServletPath来创建相应的方法 例如(findOne).
之后利用反射机制获取Servlet中的findOne方法并执行这个findOne()方法
web.xml来配置Serclet(下面表示所有.do结尾的都跳转到com.atguigu.mvcapp.servlet.CustomerServlet进行处理)
CustomerServlet
CustomerServlet
com.atguigu.mvcapp.servlet.CustomerServlet
CustomerServlet
*.do
CustomerServlet.java(下面是利用反射机制)
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//1. 获取 ServletPath: /edit.do 或 /addCustomer.do
String servletPath = req.getServletPath();
//2. 去除 / 和 .do, 得到类似于 edit 或 addCustomer 这样的字符串
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length() - 3);
try {
//3. 利用反射获取 methodName 对应的方法 后面两个参数是method.invoke需要携带的类型
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);
//4. 利用反射调用对应的方法
method.invoke(this, req, resp);
} catch (Exception e) {
e.printStackTrace();
//可以有一些响应.
resp.sendRedirect("error.jsp");
}
}
findOne方法
private void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1. 获取表单参数: name, address, phone
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
//2. 检验 name 是否已经被占用:
//2.1 调用 CustomerDAO 的 getCountWithName(String name) 获取 name 在数据库中是否存在
long count = customerDAO.getCountWithName(name);
//2.2 若返回值大于 0, 则响应 newcustomer.jsp 页面:
//通过转发的方式来响应 newcustomer.jsp
if(count > 0){
//2.2.1 要求再 newcustomer.jsp 页面显示一个错误消息: 用户名 name 已经被占用, 请重新选择!
//在 request 中放入一个属性 message: 用户名 name 已经被占用, 请重新选择!,
//在页面上通过 request.getAttribute("message") 的方式来显示
request.setAttribute("message", "用户名" + name + "已经被占用, 请重新选择!");
//2.2.2 newcustomer.jsp 的表单值可以回显.
//通过 value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>"
//来进行回显
//2.2.3 结束方法: return
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
return;
}
//3. 若验证通过, 则把表单参数封装为一个 Customer 对象 customer
Customer customer = new Customer(name, address, phone);
//4. 调用 CustomerDAO 的 save(Customer customer) 执行保存操作
customerDAO.save(customer);
//5. 重定向到 success.jsp 页面: 使用重定向可以避免出现表单的重复提交问题.
response.sendRedirect("success.jsp");
//request.getRequestDispatcher("/success.jsp").forward(request, response);
}
index.jsp