Developing JavaServer Pages - How to Request a JSP (Page 7 of 11 )
After you create a JSP, you need to test it. One way to do that is to click on a link or a button on an HTML page that requests the JSP. Another way is to enter a URL into a web browser that requests the JSP. Figure 7 shows how to request a JSP either way.
Figure 7: How to request a JSP
A URL that includes parameters

How Tomcat Maps Directories to HTTP Calls
Tomcat directory URL
c:tomcatwebappsmurach http://localhost:8080/murach/
c:tomcatwebappsmurachemail4 http://localhost:8080/murach/email4
c:tomcatwebappsROOT http://localhost:8080/
c:tomcatwebappsROOTemail4 http://localhost:8080/email4
A Form Tag that Requests a JSP
<form action="show_email_entry.jsp" method="get">
Two URLs that Request a JSP
http://localhost:8080/murach/email4/show_email_entry.jsp
http://www.murach.com/email4/show_email_entry.jsp
How to Include Parameters
show_email_entry.jsp?firstName=John
show_email_entry.jsp?firstName=John&lastName=Smith
Description
When you use the Get method to request a JSP from an HTML form, the parameters are automatically appended to the URL.
When you code or enter a URL that requests a JSP, you can add a parameter list to it starting with a question mark and with no intervening spaces. Then, each parameter consists of its name, an equals sign, and its value. To code multiple parameters, use ampersands (&) to separate the parameters.
To request a JSP from an HTML form, you use the Action attribute of the form to provide a path and filename that point to the JSP. This is illustrated by the first example in this figure. Here, the assumption is that the HTML page and the JSP are in the same directory. If they weren't, you would have to supply a relative or absolute path for the JSP file.
To request a JSP by entering its URL into a browser, you enter an absolute URL as shown by the next two examples in this figure. The first example shows the URL for the JSP when it's stored on a local web server in the email4 directory of the murach directory. The second example shows the URL for the JSP if the JSP was deployed on the Internet server for www.murach.com.
When you test a JSP by entering a URL, you will often want to pass parameters to it. To do that, you can add the parameters to the end of the URL as shown by the last examples in this figure. Here, the question mark after the jsp extension indicates that one or more parameters will follow. Then, you code the parameter name, the equals sign, and the parameter value for each parameter that is passed, and you separate multiple parameters with ampersands (&). If you omit a parameter that's required by the JSP, the getParameter method will return a null value for that parameter.
When you use a Get method to request a JSP from another page, any parameters that are passed to the JSP will be displayed in the browser's URL address. In this figure, for example, you can see the first two parameters that have been attached to the URL. However, in the next figure, you'll learn that the Post method works differently.
Remember: this is from chapter four of Joel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!
Buy this book now. |
Next: Using Get and Post Methods >>
More Java Articles
More By Joel Murach