How load servlet on index.jsp
Asked Answered
H

4

8

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 index.jsp is opened.

I tried to set <load-on-startup> in web.xml, but it didn't have any effect. How do I get the welcome file index.jsp to call the servlet?

Harbard answered 5/4, 2013 at 17:24 Comment(0)
S
15

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:

Spender answered 5/4, 2013 at 17:27 Comment(7)
I believe he's thinking about it in reverse. As in, once you're in index.jsp, "call" a servlet to populate dropdown, instead of using the servlet to fill in request attributes which you would then reference in the .jsp being rendered.Waterage
@Sotirios: this is wrong design. You should not invoke a view without a controller. True, you could hack it around with <jsp:include> on servlet's URL somewhere in top of JSP, but this is plain wrong and the servlet won't act as a controller. OP want to invoke a servlet to prepopulate dropdown values before displaying the JSP which is registered as welcome file. My answer answers that correctly.Spender
I think he incorrectly thinks you do it the other way around as opposed to the correct way you are describing.Waterage
@Sotirios: hm, that could indeed be the case. In that case, DarkVision, please carefully go through our servlets wiki page to learn the proper basic concepts: stackoverflow.com/tags/servlets/infoSpender
As an alternative, he could use your solution, but as a <url-pattern> for the indexServlet still use /index.jsp (and for the <welcome-file> index.jsp). Of course, in this case, he must change the name of the view (JSP file) to something else. This way, after forwarding the request from the servlet to the view, in the address bar he would see the desired index.jsp ))Thorner
you right Sotirios i did in reverse i invoke my view before invoking my controller. But isnt my main page index.jsp ? i cant invoke my servlet.java has my main when index.jsp load first that why im little confuse over there i know my servlet do a dispatch to index.jsp and populate all my dropdown but my main page should be like www.mysite.com/index.jsp and not www.mysite.com/myservlet.java maybe i missing the point somewhereHarbard
Just get rid of .jsp extension: www.mysite.com/indexSpender
A
0

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

Ananna answered 5/4, 2013 at 17:40 Comment(0)
F
0

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.

Frear answered 4/8, 2014 at 5:3 Comment(0)
A
0

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>
Athirst answered 29/10, 2018 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.