How to call Java class, Java Method from JSP Page in eclipse - onlyxcodes

Monday, 11 March 2019

How to call Java class, Java Method from JSP Page in eclipse

This tutorial will teach you how to use eclipse IDE to call Java class, Java method from a JSP page. 

Some time to create secret logic for business application more effort in JSP thousands of codes to store in JSP scriptlet element, the Java class helps this issue by generating Java class on the same JSP page to store your secret logic. 

Following the tutorial, Java class is generated on a JSP page, and this Java class is accessible in certain Java packages. OK, I began the little piece of code and followed the steps to recognize how to call Java class from JSP. 

How to call Java class, Java Method from JSP Page in eclipse

Table Content

1. MyClass.java

2. index.jsp

3. welcome.jsp

4. Output

5. Full Codes Of index.jsp Page


1. MyClass.java

See creating quick Java class in com.hamid package separately. And within this class, we create a string method of return type that sends Java class calling message successfully.

package com.hamid;

public class MyClass 
{
 public String check() 
 {    
    return "Java Class Call Successfully";     
 }
 
}

2. index.jsp

I created an index page and store a simple form with the submit button on this page.

<form method="post" class="form-horizontal">
                    
    <div class="form-group">
    <div class="col-sm-offset-5 col-sm-9 m-t-15">
    <input type="submit" name="btn_check" class="btn btn-success" value="Check">
    </div>
    </div>
                    
</form>

Java Class Calling Codes :


See creating codes separately on the index page below and beginning above <! DOCTYPE tag html>. When you click the submit button, it shows the Java class calling successfully message on the welcome page.

<%@page import="com.hamid.MyClass"%>

<%
    if(request.getParameter("btn_check")!=null) 
    {  
        MyClass mc=new MyClass();  
        
        String validate=mc.check(); 
        
        if(validate.equals("Java Class Call Successfully"))  
        {
            request.setAttribute("SuccessMsg",validate); 
            RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp"); 
            rd.forward(request, response);
        }
    }
%>

Explanation :

Below is the JSP page directive tag. Use this to include Java class from the package on the JSP page. The import is attribute and com.hamid.MyClass value of this attribute.

JSP page directive tag with import attribute

See next we create Java class MyClass mc = new MyClass(); within this page. The mc is an object of this class.

If you click the submit button, our Java class returns a Java class calling successfully message, And the RequestDispatcher will forward this message to the welcome page.

3. welcome.jsp

This page displays the Java class calling successfully message by output stream.

<center>
    <h3 style="color:green">
    <%
    if(request.getAttribute("SuccessMsg")!=null)
    {
        out.println(request.getAttribute("SuccessMsg")); 
    }
    %>
    </h3>
            
    <h4><a href="index.jsp">Back</a></h4>
            
</center>   



4. Output

To invoke the eclipse tool project, see the default index page is opened. 

the default index page is opened with submit button

See the message is displayed.

Java class calling successfully message displays

5. Full Codes Of index.jsp Page

You will find all the codes that I mentioned location quickly. 

Codes Location :

Row no 2 – See the page directives tag to include Java class on this page.

Row no 4 to 25 – Here I establish the Java class calling codes.

Row no 45 – Simple form with submit button available. 

<%@page import="com.hamid.MyClass"%>

<%
    if(request.getParameter("btn_check")!=null) 
    { 
        
        MyClass mc=new MyClass(); 
        
        String validate=mc.check(); 
        
        if(validate.equals("Java Class Call Successfully")) 
        {
            request.setAttribute("SuccessMsg",validate); 
            RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp"); 
            rd.forward(request, response);
        }
    }
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Java Class Call In JSP Example : www.onlyxcodes.com</title>
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
        <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
        <script src="js/jquery-1.12.4-jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <h3 align="center"><a href="http://www.onlyxcodes.com/2019/03/how-to-java-class-call-in-jsp_10.html" target="_blank">Java Class Call In JSP Example </a></h3></br>
               
                <form method="post" class="form-horizontal">
                    
                    <div class="form-group">
                    <div class="col-sm-offset-5 col-sm-9 m-t-15">
                    <input type="submit" name="btn_check" class="btn btn-success" value="Check">
                    </div>
                    </div>
                    
                </form>
            
                <h3 style="color:red">
                <%
                    if(request.getAttribute("ErroMsg")!=null)
                    {
                        out.println(request.getAttribute("ErrorMsg")); //get login error message from LoginController
                    }
                %>
                </h3> 
        </div>
        
    </body>
</html>

Download Codes

No comments:

Post a Comment