How load servlet on index.jsp

前端 未结 4 1535
广开言路
广开言路 2020-12-29 15:27

Is there is any way to call a servlet on index.jsp? My welcome file is index.jsp. I need to populate dropdown list values by a servlet when i

相关标签:
4条回答
  • 2020-12-29 15:56

    Just change the welcome file URL to be the one of the servlet.

    Given this servlet mapping,

    <servlet-mapping>
        <servlet-name>indexServlet</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>
    

    just have this welcome file list:

    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>
    

    Don't forget to move the /index.jsp into /WEB-INF folder to prevent it from being accessed directly by endusers guessing its URL (and don't forget to alter the forward call in the index servlet to point to /WEB-INF/index.jsp).

    Or if you solely intend to have a "home page servlet" and not an "index servlet", then map the servlet to the empty string URL pattern instead of as welcome file.

    <servlet-mapping>
        <servlet-name>indexServlet</servlet-name>
        <url-pattern></url-pattern>
    </servlet-mapping>
    

    See also:

    • How to call a servlet on jsp page load?
    • Difference between / and /* in servlet mapping url pattern
    0 讨论(0)
  • 2020-12-29 16:10

    There are multiple ways to achieve this depending on what frameworks you are using.

    In simple terms you can either call the servlet first and set up the data into the form and then redirect to your JSP.

    Or

    If you are familiar with Ajax you can make an ajax call from your jsp to fetch the data for you

    If you can tell me the frame work you are using for you project I can provide you an example

    0 讨论(0)
  • 2020-12-29 16:14

    Use JQuery Ajax

    <body onload="functionName()">
    <script>
        function functionName(){
    
        $.ajax({
           url : 'YourServlet',
           type: "GET",
           async: false,
           success:function(response){
    
           },
           error: function (event) {
    
               console.log("ERROR: ", event);
           }
        });
    }
    </script>
    
    0 讨论(0)
  • 2020-12-29 16:22

    just Create an empty dummy index page...In that page just add the following line...

    <%request.getRequestDispatcher("Your Servlet name").include(request,response);%> e.g: <%request.getRequestDispatcher("Alumni_Servlet?option=first").include(request,response);%>

    Then in that Servlet class, Just implement the logics and redirect your original Home or index page.

    0 讨论(0)
提交回复
热议问题