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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DarkVision
  • 1,373
  • 3
  • 20
  • 33

4 Answers4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    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. – Sotirios Delimanolis Apr 05 '13 at 17:51
  • 1
    @Sotirios: this is wrong design. You should not invoke a view without a controller. True, you could hack it around with `` 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. – BalusC Apr 05 '13 at 18:11
  • I think he incorrectly thinks you do it the other way around as opposed to the correct way you are describing. – Sotirios Delimanolis Apr 05 '13 at 18:14
  • 2
    @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: http://stackoverflow.com/tags/servlets/info – BalusC Apr 05 '13 at 18:14
  • As an alternative, he could use your solution, but as a `` for the *indexServlet* still use `/index.jsp` (and for the `` *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** )) – informatik01 Apr 05 '13 at 22:55
  • 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 somewhere – DarkVision Apr 06 '13 at 03:04
  • Just get rid of `.jsp` extension: `www.mysite.com/index` – BalusC Apr 06 '13 at 03:10
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

NullPointerException
  • 3,732
  • 5
  • 28
  • 62
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.

Ranjith
  • 1
  • 3
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>
Mr. Kulak
  • 21
  • 3