Mapeo de servlets de primavera

friends. I'm not good at English. Please Understand it.

I was asked to make a xml file.

When I access below address with a browser. I should see below xml. http://localhost:8080/update/update.jsp

<update_info app_name="ktp_app" version="2012031001" count="">
  <apache_start version="20120310011255" type="bat" reg="123123" size="423">
  <remote>http://192.168.0.1/ka/apache_start.bat</remote>
  <remote>apache_start.bat</remote>
  </apache_start>
</update_info>

but, in web.xml I have this setting.

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.byto</url-pattern>
</servlet-mapping>

with above, I could process all my other request with spring controller which look like this.

@Controller
public class DownloadManageController {

    @Autowired
    private DownloadManageService downService;

    @RequestMapping("/ajax/add_download_hour")
    public void addDownloadHour(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, SecurityException, InvalidHourRangeException, IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        downService.addDownloadLimit(
            request.getParameter("app_cmd"),
            request.getParameter("start_hour"),
            request.getParameter("end_hour"), 
            request.getParameter("limit_count"));
    }
    ...
}

But I also have this controller.

@Controller
public class UpdateXmlController extends HttpServlet {

    @Autowired
    private UpdateXmlService updateService;

    @RequestMapping("/update")
    protected ModelAndView getUpdateData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        Map resultMap = null;
        PrintWriter writer = response.getWriter();
        try {
            resultMap = updateService.getUpdateData(cmd);
            request.setAttribute("server", resultMap.get("server"));
            request.setAttribute("package", resultMap.get("package"));
            request.setAttribute("file", resultMap.get("file"));
        } catch(NoCmdException ex) {
            return new ModelAndView("error_page", "errorMsg", ex.getMessage());
        } catch(DownloadCountExceedException ex) {
            return new ModelAndView("error_page", "errorMsg", ex.getMessage());
        } catch(Exception ex) {
            //System.out.println(ex.getMessage());
            //response.getWriter().write();
            ex.printStackTrace();
        }

        return new ModelAndView("update", "result", resultMap);
    }

}

which i need to access with http://localhost:8080/update/update.jsp .

But, I only way to access that controller is with http://localhost:8080/update/update.byto

So, here's my QUESTION

how could I access that controller with .jsp address.

muchas gracias.

preguntado el 09 de marzo de 12 a las 16:03

Por qué quieres to access it with a .jsp ¿sufijo? -

I see your controllers contain too many *request.getParameter()**s, I believe you are not utilizing Spring MVC well. -

I think so, i knew Spring a few days ago and I started programming a few months ago. -

Mr. skaffman, I need that because my senior wants -

3 Respuestas

Could you add another servlet-mapping?

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.byto</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

Why does the URL need to be http://localhost:8080/update/update.jsp?

EDITAR - As skaffman points out, this will result in an infinite loop. I'll leave this answer in place as an example of what no ¡que hacer!

As an example of what happens in Tomcat:

09-Mar-2012 16:49:11 org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet Test threw exception
java.lang.StackOverflowError
    at java.util.HashMap.getEntry(HashMap.java:344)
    at java.util.HashMap.containsKey(HashMap.java:335)
    at org.apache.catalina.connector.Request.removeAttribute(Request.java:1335)
    at org.apache.catalina.connector.RequestFacade.removeAttribute(RequestFacade.java:514)
    at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(ApplicationHttpRequest.java:256)
    at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(ApplicationHttpRequest.java:256)

...

at test.Test.doGet(Test.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at test.Test.doGet(Test.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

respondido 09 mar '12, 17:03

If you do that, then when the controller forwards to the JSP, it'll get caught in an infinite loop. - skaffman

some program which my senior already made has static update path "localhost:8080/update/update.jsp"- jeon

Cambia esto:

@Controller("/update")
...

  @RequestMapping("/update.jsp")

respondido 09 mar '12, 16:03

Eso no funcionará, porque el web.xml defines the URL pattern as *.byto, asi que .jsp won't be directed to Spring. - skaffman

Temporarily, I solved this problem with JSP file.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    getServletContext().getRequestDispatcher("/update.byto").forward(request, response);
%>

above source will dispatch all information to Spring controller.

respondido 11 mar '12, 13:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.