在实际的项目开发中,如果没有用到struts 而是选择了Servlet。这个时候不免要创建很多的Servlet,并且在servlet中实现doget(),doPost()方法,然后再根据需求创建方法。而方法则需要经过很多的if else 语句。这不免有点让人烦感。下面这三段代码则实现了,项目仅创建一个Servlet轻松搞定所有的业务。也更加体现了面向对象的思想。
首先创建一个基类:
package com.heavan.base;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取类的全路径以及名称
String className = request.getParameter("className");
//获取方法名
String methodName = request.getParameter("method");
try {
//获取class文件
Class<?> class1 = Class.forName(className);
//获取该类所需求的方法
Method method = class1.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(class1.newInstance(), request,response);//方法的实现
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
创建一个用于Book类 (不是Servlet)
package com.heavan.book;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Book {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addBook(HttpServletRequest request ,HttpServletResponse response){
System.out.println("haha");
System.out.println(request.getParameter("username"));
}
}
现在要实现图书的添加功能(如何实现那?)
一个灵活的方法解决问题:
前台页面:
<%@ page language="java" import="java.util.*" 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 'book' 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">
</head>
<body>
<form action="<%=path %>/servlet/BaseServlet" method="post">
<input type="hidden" name="className" value="com.heavan.book.Book"><!--这个要注意噢!-->
<input type="hidden" name="method" value="addBook">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>
首先创建一个基类:
package com.heavan.base;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取类的全路径以及名称
String className = request.getParameter("className");
//获取方法名
String methodName = request.getParameter("method");
try {
//获取class文件
Class<?> class1 = Class.forName(className);
//获取该类所需求的方法
Method method = class1.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(class1.newInstance(), request,response);//方法的实现
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
创建一个用于Book类 (不是Servlet)
package com.heavan.book;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Book {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addBook(HttpServletRequest request ,HttpServletResponse response){
System.out.println("haha");
System.out.println(request.getParameter("username"));
}
}
现在要实现图书的添加功能(如何实现那?)
一个灵活的方法解决问题:
前台页面:
<%@ page language="java" import="java.util.*" 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 'book' 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">
</head>
<body>
<form action="<%=path %>/servlet/BaseServlet" method="post">
<input type="hidden" name="className" value="com.heavan.book.Book"><!--这个要注意噢!-->
<input type="hidden" name="method" value="addBook">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>