Java Servlet Programming, 2nd Edition > 18. JavaServer Pages > 18.1 Using JavaServer Pages |
18.1 Using JavaServer PagesAt its most basic, JSP allows for the direct insertion of servlet code into an otherwise static HTML file.[1]
Each block of servlet code (called a scriptlet ) is surrounded by a leading <% tag and a closing %> tag. For convenience, a scriptlet can use a number of predefined variables. The six most frequently used variables are:
You'll notice that core JSP classes reside under the javax.servlet.jsp package. Example 18-1 shows a simple JSP page that says a personalized "Hello" using the predefined request and out variables. If you have a server that supports Java-Server Pages and want to test this page, you should place the file under the server's document root and save it with a .jsp extension. Assuming you have saved the page as hello1.jsp, you can then access it at the URL http://server:port/hello1.jsp, or if you put the file in a context path of jsp, then the URL would be http://server:port/jsp/hello1.jsp. Example 18-1. Saying "Hello" with JSP<HTML> <HEAD><TITLE>Hello</TITLE></HEAD> <BODY> <H1> <% if (request.getParameter("name") == null) { out.println("Hello World"); } else { out.println("Hello, " + request.getParameter("name")); } %> </H1> </BODY></HTML> A screen shot is shown in Figure 18-1. Figure 18-1. Saying Hello using JavaServer Pages
|