Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How load servlet on index.jsp

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?

like image 310
DarkVision Avatar asked Apr 05 '13 17:04

DarkVision


People also ask

How does JSP and servlet work together?

The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine. A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format.

Can we include servlet in JSP?

You cannot include or forward to a servlet in Apache/JServ or other servlet 2.0 environments; you would have to write a JSP wrapper page instead. For information, see "Dynamic Includes and Forwards in Apache/JServ".

How can I call servlet without form and submit button?

How can I call servlet without <form> and submit button? You can use the same URL for the servlets as you use for a form action. P.S. Your URLs should not be page-relative. Use server-relative URLs that start with the context path.


1 Answers

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
like image 189
BalusC Avatar answered Sep 17 '22 12:09

BalusC