Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Back-End Java Class from a JSP

I come from a .NET back ground and just started working with Java. I found Java SE to be no problem since they closely follow the same concepts. However, when working with Java EE I found it to be a little bit more complicated than ASP .NET.

I am trying to implement the N-Tier architecture in a Java EE application. So I have already written my Logic & Data layers and tested them successfully in a Java SE application (which is my Administration back-end application) and have just started working on my Display layer.

Here comes the question. I am trying to call my business logic layer from JSP (Triggered by a button event) to invoke a certain method and return the result output (boolean for example) to the JSP.

How do I do that?

like image 913
Yehia A. Khamees Avatar asked Mar 23 '26 04:03

Yehia A. Khamees


1 Answers

u can do all in jsp . use JAVA MVC Framework

even without that u can perform simple backhand operations using Servlet
or else you can use Java Beans which is simple business logic implementation example for java beans please reffer http://www.tutorialspoint.com/jsp/jsp_java_beans.htm

example for Servlet is given below

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="add" method="post">
        Value 1:<input type="text" name="val1" id="val1"/><br>
        Value 2:<input type="text" name="val2" id="val2"/><br>
        <input type="submit" value="Submit"/><br>
        </form>
        <%String sum="";
         sum = (String)request.getAttribute("val3"); %>
        <input type="text" value="<%=sum%>" />
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>add</servlet-name>
        <servlet-class>controller.add</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>add</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

add.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class add extends HttpServlet {
    String val3;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String val1=request.getParameter("val1");
        String val2=request.getParameter("val2");
        if(val1 != null && val2 != null)
        val3=""+(Integer.parseInt(val1)+Integer.parseInt(val2));
        else
        val3="";
        request.setAttribute("val3",val3);
        request.getRequestDispatcher("index.jsp").forward(request, response); 

        try {
        } finally {            
            out.close();
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
like image 115
Nidhish Krishnan Avatar answered Mar 25 '26 17:03

Nidhish Krishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!