In this excerpt from chapter 4 of Joel Murach's Java Servlets and JSP, you'll learn how to develop a web application that consists of HTML pages and JavaServer Pages (JSPs). As you will see, JSPs work fine as long as the amount of processing that's required for each page is limited. When you complete this chapter, you should be able to use JSPs to develop simple web applications of your own.
Although a JSP looks much like an HTML page, a JSP contains embedded Java code. To code a scriptlet that contains one or more Java statements, you use the <% and %> tags. To display any expression that can be converted to a string, you use the <%= and %> tags.
When you code a JSP, you can use the implicit request object. This object is named request. You can use the getParameter method of the request object to get the values of the parameters that are passed to the JSP.
When you code a JSP, you can use the methods of the request object in your scriptlets or expressions. Since you don't have to explicitly create this object when you code JSPs, this object is sometimes referred to as the implicit request object. The scriptlet in this figure contains three statements that use the getParameter method of the request object. Each of these statements returns the value of the parameter that is passed to the JSP from the HTML page. Here, the argument for each getParameter method is the name of the textbox on the HTML page.
Once the scriptlet is executed, the values for the three parameters are available as variables to the rest of the page. Then, the three expressions can display these variables. Since these expressions are coded within the HTML tags for a table, the browser will display these expressions within a table.
After the table, the JSP contains some HTML that defines a form. This form contains only one control, a submit button named Return. When it is clicked, it takes the user back to the first page of the application. If you have any trouble visualizing how this button or the rest of the page will look when displayed by a browser, please refer back to Figure 1.
As you read this book, remember that it assumes that you already know the basics of Java programming. If you have any trouble understanding the Java code in this chapter, you may need a refresher course on Java coding. To quickly review the basics of Java coding, we recommend that you use Murach's Beginning Java 2 because it contains all the Java skills that you'll need for working with this book.
Remember: this is from chapter four ofJoel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!