Lab 1 Solutions
CS320, Spring 2004
[Q&A] (5pt) Please write down your answers to the following questions on a piece of paper.
About cs.calstatela.edu
1. What's the full path (or absolute path) of your home directory on the server?
/tomcat-5.0.19/webapps/ROOT/csun
2. If we use $HOME to represent your home directory, in what directory do you put your servlet .class files?
$HOME/public_html/WEB-INF/classes
3. Suppose you put a Foo.class under the directory $HOME/public_html/WEB-INF/classes, what url do you use to access this servlet in a browser?
http://cs.calstatela.edu:8280/~csun/Foo
4. If we use $HOME to represent your home directory, in what directory do you put your JSP .jsp files?
$HOME/public_html
5. Suppose you put a Foo.jsp under the directory $HOME/public_html, what url do you use to access this JSP page in a browser?
http://cs.calstatela.edu:8280/~csun/Foo.jsp
6. Do you need a web.xml file?
A. Yes, in $HOME/public_html
B. Yes, in $HOME/public_html/WEB-INF
C. Yes, in $HOME/public_html/WEB-INF/classes
D. No, if you received less than 20pt in the midterm. In fact, if you have a web.xml file, you should remove it immediately.
About Java Beans
1. What are the three characteristics of Bean for Dummies?
- A constructor that takes no parameters
- No public class variables
- get and set methods
2. Suppose a bean FooBean has a property called user, what are the method headers for the get and set methods of this property.
public String getUser() // return type could be other primitive or class type
public void setUser( String ) // parameter type could be other primitive or class type
3. Why do you need to put a bean in a package, and how do you do that?
When Tomcat converts a JSP into a servlet, it automatically puts the
servlet in a package (in our case, the package would be something like
org.apache.jsp.csun.jsp). Any class used in a packaged class must be in
a package. To put a bean (or a Java class in general) in a package,
include the following as the first line of the java code:
package your.package.name;
4. Suppose you put your beans in the package edu.csula.studentx and
complied them. In which directory should you put the .class files of
the beans so your JSP files can use them?
$HOME/public_html/WEB-INF/classes/edu/csula/studentx
5. How do you declare a bean in a JSP file. Give an example.
<jsp:useBean id="fb" class="edu.calstatela.cs.csun.FormBean" scope="session"/>
6. Explain the four scopes of a bean in a JSP file. Which scope is the default?
- page - a new instance of the bean is created for each page load. No sharing. Default.
- request - a new instance of the bean is created for each request and shared by all JSPs handling the same request.
- session
- a new instance of the bean is created for each session and shared by
all JSPs handling the same session. The bean will only be de-allocated
(garbage-collected) when the session is invalidated or expired.
- application - a new instance of the bean is created when the bean
is referenced for the first time, and is shared by all JSPs. The bean
will only be de-allocated when it's explicitly unloadedor when the server is shut down.
7. In a JSP file, how do you set the value of a property in a bean? and how do you set the values of all properties in a bean?
<jsp:setProperty name="bean_id" property="property_name" value="some_value"/>
<jsp:setProperty name="bean_id" property="*"/>
8. In a JSP file, how do you read the value of a property in a bean?
- <jsp:getProperty name="bean_id" property="property_name"/>
- <%= bean_id.getPropertyName() %>
- ${bean_id.property_name}
9. What's the difference between the following two lines of code:
- <jsp:setProperty name="fb" property="user" value="user" />
assign string "user" to the bean property "user".
- <jsp:setProperty name="fb" property="user" param="user" />
assign the value of the request parameter "user" to the bean property "user".
About Expression Language (EL)
1. How to you access the value of a request parameter user in a JSP using EL?
${param.user}
2. How to you access the value of a bean property user in a JSP using EL?
${bean_id.user}
About JSTL
1. What's the url to the JSTL tag documentation?
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
2. Give an example of using <c:if>.
see sample JSP on the class home page.
3. Give an example of using <c:choose>, <c:when>, and <c:otherwise>
see sample JSP on the class home page.
[Programming] (5pt) Use a JSP Validate.jsp to validate the user input from User.html.
If the length of the user name is less than 4 characters or the course
name is not CS320, Validate.jsp should display "invalid user or
course"; otherwise display the user name and the course name. You must
use a bean in Validate.jsp. The bean should have at least three
properties: user, course, and valid. Both files have to be placed on the server and work properly there.