Calling Back-End Java Class from JSP
I come from .NET back ground and just started with Java. I found that Java SE is not a problem as they closely follow the same concepts. However, when working with Java EE, I found it to be a little more complex than ASP.NET.
I am trying to implement N-tier architecture in a Java EE application. So I already wrote my Logic and Data layers and tested them successfully in a Java SE application (which is my internal admin application) and just started working in my display layer.
Here's the question. I am trying to call my business logic layer from a JSP (triggered by a button event) to call a specific method and return a result (like a boolean) in the JSP.
How to do it?
source to share
u can do everything in jsp. use JAVA MVC Framework
even without it u can do simple operations with Servlet
or you can use Java Beans which is a simple implementation of business logic example for java beans please reffer http://www.tutorialspoint.com/jsp/jsp_java_beans.htm
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>
}
source to share
In ASP.NET there are things that are handled by .Net (javascripts and other things inserted into a .Net page), but in java, you have to write them yourself. In .Net you have some javascript method for example do_postback
, but in java you have to write javascripts. See this code:
Script:
<script>
function doPostback() {
// calling some java method using Ajax or Http Post method or redirect to another page
}
</script>
Html:
<button type="button" onclick="doPostback()">Go</button>
source to share