JSP/Servlet Correspondence
Original JSP
<%= foo() %>
<% bar(); %>
Possible resulting servlet code
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setContentType("text/html");
HttpSession session = request.getSession(true);
JspWriter out = response.getWriter();
out.println(foo());
bar();
...
}
27
JSP
www.corewebprogramming.com
Example Using JSP Scriptlets
" //W3C//DTD HTML 4.0 Transitional//EN">
Color Testing
<%
String bgColor = request.getParameter("bgColor");
boolean hasExplicitColor;
if (bgColor != null) {
hasExplicitColor = true;
} else {
hasExplicitColor = false;
bgColor = "WHITE";
}
%>
"<%= bgColor %>"
>
28
JSP
www.corewebprogramming.com
14