Servlet封装BaseServlet类
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;
/**
*
* @Title: BaseServlet
* @Description: 封装的BaseServlet
* @Company: 山东九点连线信息技术有限公司
* @ProjectName: Summarize
* @author fupengpeng
* @date 2017年11月27日 上午11:35:15
*/
public abstract class BaseServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// 1、获得执行的方法名
String methodName = request.getParameter("method");
// 默认方法
if (methodName == null) {
methodName = "execute";
}
System.out.println("BaseServlet : 本次所执行方法 : " + methodName);
try {
// 2、通过反射获得当前运行类中指定方法,形式参数
Method executeMethod = this.getClass().getMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
// 3、反射执行方法
String result = (String) executeMethod.invoke(this, request,
response);
// 4、将json数据返回
response.getWriter().write(result);
} catch (NoSuchMethodException e) {
throw new RuntimeException("未知方法 : " + methodName);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("服务器异常", e);
}
}
/**
* 默认执行方法
*/
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
用户管理Servlet(UserServlet)继承BaseServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fpp.base.BaseServlet;
/**
*
* @Title: UserServlet
* @Description: 用户管理servlet
* @Company: 山东九点连线信息技术有限公司
* @ProjectName: Summarize
* @author fupengpeng
* @date 2017年11月27日 下午3:54:45
*/
public class UserServlet extends BaseServlet {
/**
*
* @Description: 登录 http://localhost:8080/Summarize/UserServlet?method=login
* &account=zhangsan&password=123456
* @Title: login
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
* String
*/
public String login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取到客户端传递过来的用户账号和密码
String name = request.getParameter("account");
String password = request.getParameter("password");
// 2.判断用户是否登录成功
if ("zhangsan".equals(name) && "123456".equals(password)) {
System.out.println("登录成功");
return "denglu chenggong";
} else {
System.out.println("登录失败");
return "denglu shibai";
}
}
/**
*
* @Description: 注册 http://localhost:8080/Summarize/UserServlet?method=register&account=zhangsan&phonenumber=17712345678
* @Title: register
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
* String
*/
public String register(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获取到传递过来的数据
String name = request.getParameter("account");
String phonenumber = request.getParameter("phonenumber");
//2.保存至数据,返回注册成功
return "zhuce chenggong";
}
}
web.xml中配置UserServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Summarize</display-name>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.fpp.test.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
访问登录方法
部署项目到tomcat后运行项目,浏览器访问登录方法:http://localhost:8080/Summarize/UserServlet?method=login&account=zhangsan&password=123456
访问注册方法:http://localhost:8080/Summarize/UserServlet?method=register&account=zhangsan&phonenumber=17712345678