Pace CS 396S - Java Server Pages and Java Beans

Unformatted text preview:

Java Server Pages and Java Beans Java server pages (JSP) and Java beans work together to create a web application. Java server pages are html pages that also contain regular Java code, which is included between special tags. Java beans are Java programs that follow some specific rules. Together they make up part of a web application. There are several advantages to using Java server pages and beans. One is that the html and the Java code can be kept separate. Another is that the JSP file is a special kind of html file so that it can be placed anywhere on the server and can include any html tags, including references to other pages, images, applets, etc. Java server pages are not only html pages. They are also Java programs, so they must be compiled. This is done the first time that they are accessed. After that, the compiled code resides on the server and can be used as is by any succeeding requests. They are first translated into Java servlets, and then the servlets are compiled into class files. On a stand-alone system, you can find both in the folder work/Standalone/localhost/_. Java Server Pages In a JSP file the Java code is contained between tags that begin with <% and end with %>. These tags are also used in active server pages (asp). There are several different kinds of JSP tags depending upon their use. • <%= … %> is used for expressions. • <%! … %> is used for declarations. • <% … %> is used for straight Java code. • <%@ … %> is used to include another file such as an html file or a package such as java.sql.*. There are some reserved words that may be used by JSP files without further definition. These should be familiar from similar terms used with Java servlets. • request – an instance of HttpServletRequest. • response – an instance of HttpServletResponse. • out – a PrintWriter object for the response. • session – the HttpSession object associated with the session. Java Beans Java beans are regular Java programs with a few specific restrictions. The constructor may not have any parameters, and the variables all have get and set accessor and mutator methods. The Java server page uses the accessor and mutator methods of the bean to send values to the bean and to get resulting data back from the bean. In a Java bean, you can instantiate other classes, access databases, create lists, tables, and anything else you might want to do. You can also have methods that receive request data from the JSP file. They have the usual request parameter as in the following example: public void processRequest (HttpServletRequest request) { … } JSP files must declare which bean they are going to use. This is done with tags that follow xml syntax. They are case sensitive and must have closing tags or a closing ‘/’. A bean declaration for a simple hello world example follows: <jsp:useBean id = "hello" scope = "session" class = "greetings.HelloBean" />The id for the bean is used in place of a name. The declaration links it to the bean class, HelloBean.class. The Java server page may be located anywhere, but the class file goes in the same classes folder as the servlet classes. HelloBean Example The bean, HelloBean.java, is very simple. We will see more complicated beans later on. It doesn’t have a separate constructor, only the default one without parameters. It also does not do anything other than provide a storage place for the data, the name and the email address. Notice it is in a package called greetings. package greetings; public class HelloBean { private String name = ""; private String email = ""; public String getName() {return name;} public String getEmail() {return email;} public void setName (String n) {name = n;} public void setEmail (String e) {email = e;} } // HelloBean The Java server page must also set the properties of the bean, i.e. the bean’s instance data. This is done with jsp:setProperty tags. These too use xml syntax. <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>' /> <jsp:setProperty name="hello" property="email" value='<%= request.getParameter ("email") %>' /> The name attribute is the same as the bean id, hello. The property is the name of the instance variable in the bean. And the value is read from the html input data. The html page used for this example is also very simple. <html> <head><title>Hello</title></head> <body bgcolor="white"> <font size=5 color="blue"> <form method="get" action="hello.jsp"> <br />Enter your name and email address: <br /><input type="text" name="name" value="" size="20"> Name <br /><input type="text" name="email" value="" size="20"> Email <p> <input type="submit" name="Send" value="Send"></p> </form> </font></body></html> The JSP file combines both html and Java code. One that can tie together the html and bean files above follows: <html> <head><title>Hello JSP</title></head><body bgcolor="white"> <font size=5 color="blue"> <%! String name, email; %> <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /> <jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>' /> <jsp:setProperty name="hello" property="email" value='<%= request.getParameter ("email") %>' /> <% name = hello.getName(); email = hello.getEmail(); %> Hello, your name is <% out.println (name); %> <br />And your email address is <% out.println (email); %> </font></body></html> The result looks like: Hello, your name is Alice Lee And your email address is [email protected] where the request data sent by the html file was Alice Lee for the name and [email protected] for the e-mail address. The setProperty tags in the Java server page can be replaced by <jsp:setProperty name = "hello" property = "*" /> if the names in the JSP and bean files are the same. This doesn’t always work, but in most cases it saves a lot of writing. However, when using this shortcut, there are several conventions that must be followed. First, the accessor and mutator methods must be of the form getEmail () setEmail (String e) where the identifier in the web page and bean is ‘email’. If instead, the variable was called ‘eMail’, the get and set methods would be getEMail () setEMail (String e) The first letter after the get or set must be capitalized. The rest of the letters follow the capitalization in the variable. This also means that all variables


View Full Document
Download Java Server Pages and Java Beans
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Server Pages and Java Beans and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Server Pages and Java Beans 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?