3

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.

4

2 回答 2

2

在 Servlet 中执行如下操作

public class SharedSQL extends HttpServlet{

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

在jsp中做如下

<%
     String sqlstmt = request.getAttribute("sqlstmt") 
     db4.query ( sqlstmt ) ;
%>
于 2013-10-03T02:27:41.507 回答
1

您也可以通过以下方式在会话属性中设置它

HttpSession session = request.getSession();
session.setAttribute("string",value) //you can set string, object in the value

然后在jsp中使用EL表达式直接访问值

${string} 
于 2013-10-03T07:01:02.540 回答