Servlet作为JSP Java配置

时间:2015-06-24 13:33:28

标签: java servlet-3.0 spring-java-config

我有一个servlet作为jsp,在xml中声明,我想在Java Config上进行迁移。 网络的XML:

<servlet>
   <servlet-name>App</servlet-name>
   <jsp-file>/app/App.jsp</jsp-file>
</servlet>

<servlet-mapping>
  <servlet-name>App</servlet-name>
  <url-pattern>/app/App.html</url-pattern>
</servlet-mapping>

Java配置:

@Override
public void onStartup(ServletContext servletContext){
  ...
  servletContext.addServlet("App", new AppServlet()).addMapping("/app/App.html");

}

private class AppServlet extends HttpServlet{
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp){
    req.getRequestDispatcher("/app/App.jsp").forward(req,resp);
 }
}

但是,它不起作用。

1 个答案:

答案 0 :(得分:0)

您也可以使用@Webservlet注释 -

尝试此操作
@WebServlet(
        urlPatterns = {"/url/one", "url/two"}
)
public class ImageUploadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
       ........
       ........
       ........
    }
}