How to make a JSP function to run only when called?
The events in the webpage (onclick event for example) is executed client side when someone opens your webpage and clicks the button. The JSP scriplets are executed when the webpage is opened and their result is the actual page that the client receives. So your method executes ONLY when the page is loaded (and the JSP is compiled). The java code is not even visible in the webpage you generate. If you open it in browser and check the source you will see what I mean.
How to call jsp page from java method or class
I'm not sure if I have understood correctly what you are trying to achieve, but if I have, you cannot open a jsp page from straight Java code. JSP is designed for web applications, so you would have to put your jsp file inside a project that you can deploy onto a web container, such as Tomcat or Jetty. Then you have to configure the web server properly to allow your java code and jsp code to talk to each other correctly.
If in fact all you are trying to do is open a browser page from inside some Java code and navigate to a URL, you can use a ProcessBuilder or the Runtime.exec() to open a browser to the URL you are thinking of (eg IE, Chrome, Firefox etc)
How to call jsp page from java class without using any servlet
If i am not mis understood, you are looking for JSP page to be opened in a browser via java class?
if yes, you can use Desktop API.
You can also look into following answers:
Open local html page - java
Getting java gui to open a webpage in web browser
Also keep in mind that your JSP page should be placed in a web container(Tomcat etc.) and its running when invoked OR you will be stuck finding out Why JSP is not opening.
How does jsp work?
Basically:
In your servlet container, the JSP servlet is mapped to any URL that ends in
.jsp
(usually)When one of those
.jsp
URLs is requested, the request goes to the JSP servlet. Then, this servlet checks if the JSP is already compiled.If the JSP is not compiled yet, the JSP servlet translates the JSP to some Java source code implementing the
Servlet
interface. Then it compiles this Java source code to a.class
file. This.class
file usually is located somewhere in the servlet container's work directory for the application.Once the JSP servlet has compiled the servlet class from the JSP source code, it just forwards the request to this servlet class.
The thing is, unless you specifically precompile your JSP, all this happens at runtime, and hidden in the servlet container's work directory, so it is "invisible". Also have in mind that this is what happens "conceptually", several optimizations are possible in this workflow.
Related Topics
How to Pass Jvm Options from Bootrun
Eclipse Reading Stdin (System.In) from a File
How to Retrieve the Autoincrement Id from a Prepared Statement
Java Date Cut Off Time Information
How to Use Sqoop in Java Program
When Does the Main Thread Stop in Java
Class Object of Generic Class (Java)
Mocking Time in Java 8's Java.Time API
Auto Adjust the Height of Rows in a Jtable
How to Log Spring 5 Webclient Call
Java Import VS Code Performance
How to Ensure in Java That the Current Local Time Is Correct
Put Byte Array to JSON and Vice Versa
In Which Thread Do Completablefuture's Completion Handlers Execute