4

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.

2
  • First thing's first. Go through our jstl and jsp wikis. Do not use scriptlets. Commented Oct 3, 2013 at 2:24
  • how stupid I am in the pass, I'm now started coding project with jstl in jsp. really appreciate... Commented Aug 29, 2014 at 7:27

2 Answers 2

2

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 ) ;
%>
Sign up to request clarification or add additional context in comments.

Comments

1

You can also set it in the session attribute by

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

Then directly access the value by using EL expression in jsp

${string} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.