How to call a servlet (doGet) in a web application at startup?

I need to call Servlet when the application starts, since it contains the application initialization logic.

I know that I can set the boot configuration at startup, but this will only call the init Servlets method. I need to call the doGet method and pass some Url parameters to it. The Servlet doGet method expects ServletRequest and ServletResponse objects.

Also, since this is a clustered application, I need to know exactly which node I am accessing it from (since one of them is just to open the socket and call the servlet).

What is the best option for this?

EDIT: as an explanation, the servlet already exists and cannot be modified. So far, someone has manually called the servlet from the browser. I need to automate this.

+3
source share
4 answers

The best option is to refactor any logic that you have in the method doGetinto a separate method that can be called both from initand doGet.

If you really can't reorganize the logic (which is really the only good option), you can use some mock library. Google says Spring mock objects are popular.

HttpServletRequest HttpServletResponse, , load-on-startup, init ServletContext doGet . (, , - .)

: WAR , , , - -.

+2

/ bootstrup ServletContextListener - ?

, , , .

+3

, java.net.URL/java.net.URLConnection .

new URL("http://localhost/yourservlet").openStream();
0

java.lang.Runtime. . init, ( - BackEndServlet) doGet doPost.

@Override
    public void init(ServletConfig config) throws ServletException {

        super.init(config);
        String url = "http://localhost:8080"+config.getServletContext().getContextPath()+"/BackEndServlet"; 
        System.out.println(url);
        String os = System.getProperty("os.name").toLowerCase();
        Runtime rt = Runtime.getRuntime();

        try{

            if (os.indexOf( "win" ) >= 0) {

                // this doesn't support showing urls in the form of "page.html#nameLink" 
                rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);

            } else if (os.indexOf( "mac" ) >= 0) {

                rt.exec( "open " + url);

            } else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {

                // Do a best guess on unix until we get a platform independent way
                // Build a list of browsers to try, in this order.
                String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
                                     "netscape","opera","links","lynx"};

                // Build a command string which looks like "browser1 "url" || browser2 "url" ||..."
                StringBuffer cmd = new StringBuffer();
                for (int i=0; i<browsers.length; i++)
                    cmd.append( (i==0  ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");

                rt.exec(new String[] { "sh", "-c", cmd.toString() });

            } else {
                    return;
            }
           }catch (Exception e){
            return;
           }
          return;
    }
0

Source: https://habr.com/ru/post/1748925/


All Articles