Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a string from Java to JSP with Java Class?

I need to create a string (sql statement) which might be pass to 2 or more jsp files. Recommended method is "by accessing the ServletContext attributes via Java scriptlet or the applicationScope via EL". But, is there a simple way to pass the string from java class to the jsp? Something like below?

Java

public class SharedSQL extends HttpServlet{

public String example() {

    String sqlstmt = "select ABC from ABC";

    return sqlstmt;
}

}

JSP

<%
     SharedSQL sqlStatement = new SharedSQL() ;
     String sqlstmt = sqlStatement.example();
     db4.query ( sqlstmt ) ;
%>

I am new to servlet/JSP 'things', need some hints and tips.

like image 465
薛源少 Avatar asked Sep 12 '25 13:09

薛源少


1 Answers

in Servlet do like below

public class SharedSQL extends HttpServlet{

    doGet(request ,response){
         request.setAttribute("sqlstmt", "select ABC from ABC");
    }
}

in jsp do like below

<%
     String sqlstmt = request.getAttribute("sqlstmt") 
     db4.query ( sqlstmt ) ;
%>
like image 92
Prabhakaran Ramaswamy Avatar answered Sep 14 '25 02:09

Prabhakaran Ramaswamy