Calling a java method in jsp

前端 未结 5 447
刺人心
刺人心 2020-11-28 10:50

I have a java class which performs some operations on files. Since the java code is huge I don\'t want to write this code in jsp. I want to call the methods in jsp whenever

5条回答
  •  孤城傲影
    2020-11-28 11:14

    Depending on the kind of action you'd like to call, there you normally use taglibs, EL functions or servlets for. Java code really, really doesn't belong in JSP files, but in Java classes.

    If you want to preprocess a request, use the Servlet doGet() method. E.g.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Preprocess request here.
        doYourThingHere();
        // And forward to JSP to display data.
        request.getRequestDispatcher("page.jsp").forward(request, response);
    }
    

    If you want to postprocess a request after some form submit, use the Servlet doPost() method instead.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Postprocess request here.
        doYourThingHere();
        // And forward to JSP to display results.
        request.getRequestDispatcher("page.jsp").forward(request, response);
    }
    

    If you want to control the page flow and/or HTML output, use a taglib like JSTL core taglib or create custom tags.

    If you want to execute static/helper functions, use EL functions like JSTL fn taglib or create custom functions.

提交回复
热议问题