Spring boot springbootservlet注册
我正在将传统的Servlet/JSP应用程序迁移到springboot 这是servlet类的当前结构Spring boot springbootservlet注册,spring-boot,servlets,Spring Boot,Servlets,我正在将传统的Servlet/JSP应用程序迁移到springboot 这是servlet类的当前结构 public class CreateAbsenceServlet extends CommonServlet 我能够在SpringBootConfiguratin类中将这个servlet注册为bean,如下所示 @Bean public ServletRegistrationBean CreateAbsenceServletRegistrationBean() { Servl
public class CreateAbsenceServlet extends CommonServlet
我能够在SpringBootConfiguratin类中将这个servlet注册为bean,如下所示
@Bean
public ServletRegistrationBean CreateAbsenceServletRegistrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(
new CreateAbsenceServlet(), "/createAbsence");
return bean;
}
问题是我如何注册CommonServlet,因为这个servlet没有直接映射,因为它是一个抽象类,扩展了HTTPServlet,并且有很多servlet生命周期方法
@SuppressWarnings("serial")
public abstract class CommonServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
prepareExecute(request, response);
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
prepareExecute(request, response);
}
protected abstract void execute(HttpSession session,
HttpServletRequest request, HttpServletResponse response,
PrintWriter out, BufferedReader reader) throws Exception;
protected void prepareExecute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
BufferedReader reader = request.getReader();
response.setContentType("text/html");
execute(session, request, response, out, reader);
} catch (Exception ex) {
if (ServletException.class.isInstance(ex)) {
throw (ServletException) ex;
} else {
throw new ServletException(ex);
}
}
}
public String getParameterValue(HttpServletRequest req, String parameter) {
Enumeration<String> parameterNames = req.getParameterNames();
String paramValue = "";
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
if (parameter.equalsIgnoreCase(paramName)) {
String[] paramValues = req.getParameterValues(paramName);
for (int i = 0; i < paramValues.length; i++) {
paramValue = paramValues[i];
}
}
}
return paramValue;
}
public void sendError(HttpServletResponse response, String errorMessage,
int status) throws IOException {
response.setStatus(status);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(errorMessage);
}
}
@SuppressWarnings(“串行”)
公共抽象类CommonServlet扩展了HttpServlet{
@凌驾
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
准备执行(请求、响应);
}
@凌驾
受保护的无效数据集(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
准备执行(请求、响应);
}
受保护的抽象无效执行(HttpSession会话,
HttpServletRequest请求,HttpServletResponse响应,
PrintWriter out、BufferedReader reader)引发异常;
受保护的无效准备执行(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
试一试{
HttpSession session=request.getSession();
PrintWriter out=response.getWriter();
BufferedReader reader=request.getReader();
response.setContentType(“text/html”);
执行(会话、请求、响应、输出、读取器);
}捕获(例外情况除外){
if(ServletException.class.isInstance(ex)){
抛出(ServletException)异常;
}否则{
抛出新的ServletException(ex);
}
}
}
公共字符串getParameterValue(HttpServletRequest请求,字符串参数){
枚举parameterNames=req.getParameterNames();
字符串参数值=”;
while(parameterNames.hasMoreElements()){
字符串paramName=parameterNames.nextElement();
if(parameter.equalsIgnoreCase(paramName)){
字符串[]paramValues=req.getParameterValues(paramName);
对于(int i=0;i
以下是我的观察和建议:
- 你能检查一下类中的方法是否符合条件吗
使用
转换为侦听器或筛选器;或@WebFilter
注释@WebListener
- 可以使用spring boot
和@ControllerAdvice
注释@ExceptionHandler